Skip to content

Commit

Permalink
Merge pull request #88 from ThomasWaldmann/py3style
Browse files Browse the repository at this point in the history
style and cosmetic fixes, no semantic changes
  • Loading branch information
ThomasWaldmann committed Jul 11, 2015
2 parents 13f4006 + 0580f2b commit 4b81f38
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 61 deletions.
8 changes: 4 additions & 4 deletions borg/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def is_full(self):
class CacheChunkBuffer(ChunkBuffer):

def __init__(self, cache, key, stats, chunker_params=CHUNKER_PARAMS):
super(CacheChunkBuffer, self).__init__(key, chunker_params)
super().__init__(key, chunker_params)
self.cache = cache
self.stats = stats

Expand All @@ -125,7 +125,6 @@ class AlreadyExists(Error):
class IncompatibleFilesystemEncodingError(Error):
"""Failed to encode filename "{}" into file system encoding "{}". Consider configuring the LANG environment variable."""


def __init__(self, repository, key, manifest, name, cache=None, create=False,
checkpoint_interval=300, numeric_owner=False, progress=False,
chunker_params=CHUNKER_PARAMS):
Expand Down Expand Up @@ -230,9 +229,11 @@ def add(id):
count, size, csize = cache.chunks[id]
stats.update(size, csize, count == 1)
cache.chunks[id] = count - 1, size, csize

def add_file_chunks(chunks):
for id, _, _ in chunks:
add(id)

# This function is a bit evil since it abuses the cache to calculate
# the stats. The cache transaction must be rolled back afterwards
unpacker = msgpack.Unpacker(use_list=False)
Expand Down Expand Up @@ -549,7 +550,7 @@ class RobustUnpacker():
item_keys = [msgpack.packb(name) for name in ('path', 'mode', 'source', 'chunks', 'rdev', 'xattrs', 'user', 'group', 'uid', 'gid', 'mtime')]

def __init__(self, validator):
super(RobustUnpacker, self).__init__()
super().__init__()
self.validator = validator
self._buffered_data = []
self._resync = False
Expand Down Expand Up @@ -804,4 +805,3 @@ def verify_chunks(self):
self.repository.delete(id_)
self.manifest.write()
self.repository.commit()

1 change: 0 additions & 1 deletion borg/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Cache:
class RepositoryReplay(Error):
"""Cache is newer than repository, refusing to continue"""


class CacheInitAbortedError(Error):
"""Cache initialization aborted"""

Expand Down
2 changes: 1 addition & 1 deletion borg/fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FuseOperations(llfuse.Operations):
"""Export archive as a fuse filesystem
"""
def __init__(self, key, repository, manifest, archive):
super(FuseOperations, self).__init__()
super().__init__()
self._inode_count = 0
self.key = key
self.repository = cache_if_remote(repository)
Expand Down
20 changes: 11 additions & 9 deletions borg/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ def release(self):

def check_extension_modules():
from . import platform
if (hashindex.API_VERSION != 2 or
chunker.API_VERSION != 2 or
crypto.API_VERSION != 2 or
platform.API_VERSION != 2):
if hashindex.API_VERSION != 2:
raise ExtensionModuleError
if chunker.API_VERSION != 2:
raise ExtensionModuleError
if crypto.API_VERSION != 2:
raise ExtensionModuleError
if platform.API_VERSION != 2:
raise ExtensionModuleError


Expand Down Expand Up @@ -529,9 +532,9 @@ def canonical_path(self):
else:
path = self.path
return 'ssh://{}{}{}{}'.format('{}@'.format(self.user) if self.user else '',
self.host,
':{}'.format(self.port) if self.port else '',
path)
self.host,
':{}'.format(self.port) if self.port else '',
path)


def location_validator(archive=None):
Expand Down Expand Up @@ -606,7 +609,7 @@ def daemonize():
class StableDict(dict):
"""A dict subclass with stable items() ordering"""
def items(self):
return sorted(super(StableDict, self).items())
return sorted(super().items())


if sys.version < '3.3':
Expand Down Expand Up @@ -642,4 +645,3 @@ def int_to_bigint(value):
if value.bit_length() > 63:
return value.to_bytes((value.bit_length() + 9) // 8, 'little', signed=True)
return value

4 changes: 2 additions & 2 deletions borg/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class UnsupportedPayloadError(Error):
"""Unsupported payload type {}. A newer version is required to access this repository.
"""


class KeyfileNotFoundError(Error):
"""No key file for repository {} found in {}.
"""
Expand Down Expand Up @@ -231,8 +232,7 @@ def find_key_file(cls, repository):
filename = os.path.join(keys_dir, name)
with open(filename, 'r') as fd:
line = fd.readline().strip()
if (line and line.startswith(cls.FILE_ID) and
line[len(cls.FILE_ID)+1:] == id):
if line and line.startswith(cls.FILE_ID) and line[len(cls.FILE_ID)+1:] == id:
return filename
raise KeyfileNotFoundError(repository._location.canonical_path(), get_keys_dir())

Expand Down
10 changes: 5 additions & 5 deletions borg/lrucache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class LRUCache(dict):

def __init__(self, capacity):
super(LRUCache, self).__init__()
super().__init__()
self._lru = []
self._capacity = capacity

Expand All @@ -13,29 +13,29 @@ def __setitem__(self, key, value):
self._lru.append(key)
while len(self._lru) > self._capacity:
del self[self._lru[0]]
return super(LRUCache, self).__setitem__(key, value)
return super().__setitem__(key, value)

def __getitem__(self, key):
try:
self._lru.remove(key)
self._lru.append(key)
except ValueError:
pass
return super(LRUCache, self).__getitem__(key)
return super().__getitem__(key)

def __delitem__(self, key):
try:
self._lru.remove(key)
except ValueError:
pass
return super(LRUCache, self).__delitem__(key)
return super().__delitem__(key)

def pop(self, key, default=None):
try:
self._lru.remove(key)
except ValueError:
pass
return super(LRUCache, self).pop(key, default)
return super().pop(key, default)

def _not_implemented(self, *args, **kw):
raise NotImplementedError
Expand Down
27 changes: 14 additions & 13 deletions borg/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ class ConnectionClosed(Error):
class PathNotAllowed(Error):
"""Repository path not allowed"""


class InvalidRPCMethod(Error):
"""RPC method is not valid"""


class RepositoryServer:
rpc_methods = (
'__len__',
'check',
'commit',
'delete',
'get',
'list',
'negotiate',
'open',
'put',
'repair',
'rollback',
)
'__len__',
'check',
'commit',
'delete',
'get',
'list',
'negotiate',
'open',
'put',
'repair',
'rollback',
)

def __init__(self, restrict_to_paths):
self.repository = None
Expand Down Expand Up @@ -71,7 +72,7 @@ def serve(self):
type, msgid, method, args = unpacked
method = method.decode('ascii')
try:
if not method in self.rpc_methods:
if method not in self.rpc_methods:
raise InvalidRPCMethod(method)
try:
f = getattr(self, method)
Expand Down
2 changes: 0 additions & 2 deletions borg/testsuite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,3 @@ def loadTestsFromName(self, pattern, module=None):
if pattern.lower() in test.id().lower():
tests.addTest(test)
return tests


16 changes: 8 additions & 8 deletions borg/testsuite/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

try:
import llfuse
has_llfuse = True
has_llfuse = True or llfuse # avoids "unused import"
except ImportError:
has_llfuse = False

Expand Down Expand Up @@ -143,7 +143,7 @@ def create_test_files(self):
self.create_regular_file('empty', size=0)
# next code line raises OverflowError on 32bit cpu (raspberry pi 2):
# 2600-01-01 > 2**64 ns
#os.utime('input/empty', (19880895600, 19880895600))
# os.utime('input/empty', (19880895600, 19880895600))
# thus, we better test with something not that far in future:
# 2038-01-19 (1970 + 2^31 - 1 seconds) is the 32bit "deadline":
os.utime('input/empty', (2**31 - 1, 2**31 - 1))
Expand All @@ -157,9 +157,9 @@ def create_test_files(self):
os.chmod('input/file1', 0o7755)
os.chmod('input/dir2', 0o555)
# Block device
os.mknod('input/bdev', 0o600 | stat.S_IFBLK, os.makedev(10, 20))
os.mknod('input/bdev', 0o600 | stat.S_IFBLK, os.makedev(10, 20))
# Char device
os.mknod('input/cdev', 0o600 | stat.S_IFCHR, os.makedev(30, 40))
os.mknod('input/cdev', 0o600 | stat.S_IFCHR, os.makedev(30, 40))
# Hard link
os.link(os.path.join(self.input_path, 'file1'),
os.path.join(self.input_path, 'hardlink'))
Expand All @@ -172,7 +172,7 @@ def create_test_files(self):
# same for newer ubuntu and centos.
# if this is supported just on specific platform, platform should be checked first,
# so that the test setup for all tests using it does not fail here always for others.
#xattr.setxattr(os.path.join(self.input_path, 'link1'), 'user.foo_symlink', b'bar_symlink', follow_symlinks=False)
# xattr.setxattr(os.path.join(self.input_path, 'link1'), 'user.foo_symlink', b'bar_symlink', follow_symlinks=False)
# FIFO node
os.mkfifo(os.path.join(self.input_path, 'fifo1'))
if has_lchflags:
Expand Down Expand Up @@ -253,7 +253,7 @@ def test_repository_swap_detection(self):
self.cmd('init', '--encryption=none', self.repository_location)
self._set_repository_id(self.repository_path, repository_id)
self.assert_equal(repository_id, self._extract_repository_id(self.repository_path))
self.assert_raises(Cache.EncryptionMethodMismatch, lambda :self.cmd('create', self.repository_location + '::test.2', 'input'))
self.assert_raises(Cache.EncryptionMethodMismatch, lambda: self.cmd('create', self.repository_location + '::test.2', 'input'))

def test_repository_swap_detection2(self):
self.create_test_files()
Expand All @@ -263,7 +263,7 @@ def test_repository_swap_detection2(self):
self.cmd('create', self.repository_location + '_encrypted::test', 'input')
shutil.rmtree(self.repository_path + '_encrypted')
os.rename(self.repository_path + '_unencrypted', self.repository_path + '_encrypted')
self.assert_raises(Cache.RepositoryAccessAborted, lambda :self.cmd('create', self.repository_location + '_encrypted::test.2', 'input'))
self.assert_raises(Cache.RepositoryAccessAborted, lambda: self.cmd('create', self.repository_location + '_encrypted::test.2', 'input'))

def test_strip_components(self):
self.cmd('init', self.repository_location)
Expand Down Expand Up @@ -524,7 +524,7 @@ def test_aes_counter_uniqueness_passphrase(self):
class ArchiverCheckTestCase(ArchiverTestCaseBase):

def setUp(self):
super(ArchiverCheckTestCase, self).setUp()
super().setUp()
with patch.object(ChunkBuffer, 'BUFFER_SIZE', 10):
self.cmd('init', self.repository_location)
self.create_src_archive('archive1')
Expand Down
25 changes: 13 additions & 12 deletions borg/testsuite/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import msgpack

from ..helpers import adjust_patterns, exclude_path, Location, format_timedelta, IncludePattern, ExcludePattern, make_path_safe, UpgradableLock, prune_within, prune_split, to_localtime, \
from ..helpers import adjust_patterns, exclude_path, Location, format_timedelta, ExcludePattern, make_path_safe, UpgradableLock, prune_within, prune_split, \
StableDict, int_to_bigint, bigint_to_int, parse_timestamp
from . import BaseTestCase

Expand Down Expand Up @@ -96,7 +96,7 @@ def test(self):
['/etc/passwd', '/etc/hosts', '/home', '/var/log/messages', '/var/log/dmesg'])
self.assert_equal(self.evaluate(['/home/u'], []), [])
self.assert_equal(self.evaluate(['/', '/home', '/etc/hosts'], ['/']), [])
self.assert_equal(self.evaluate(['/home/'], ['/home/user2']),
self.assert_equal(self.evaluate(['/home/'], ['/home/user2']),
['/home', '/home/user/.profile', '/home/user/.bashrc'])
self.assert_equal(self.evaluate(['/'], ['*.profile', '/var/log']),
['/etc/passwd', '/etc/hosts', '/home', '/home/user/.bashrc', '/home/user2/public_html/index.html'])
Expand All @@ -118,6 +118,7 @@ def test(self):
self.assert_equal(make_path_safe('/'), '.')
self.assert_equal(make_path_safe('/'), '.')


class UpgradableLockTestCase(BaseTestCase):

def test(self):
Expand Down Expand Up @@ -161,7 +162,7 @@ def dotest(test_archives, n, skip, indices):
for ta in test_archives, reversed(test_archives):
self.assert_equal(set(prune_split(ta, '%Y-%m', n, skip)),
subset(test_archives, indices))

test_pairs = [(1, 1), (2, 1), (2, 28), (3, 1), (3, 2), (3, 31), (5, 1)]
test_dates = [local_to_UTC(month, day) for month, day in test_pairs]
test_archives = [MockArchive(date) for date in test_dates]
Expand All @@ -185,24 +186,24 @@ def dotest(test_archives, within, indices):
for ta in test_archives, reversed(test_archives):
self.assert_equal(set(prune_within(ta, within)),
subset(test_archives, indices))

# 1 minute, 1.5 hours, 2.5 hours, 3.5 hours, 25 hours, 49 hours
test_offsets = [60, 90*60, 150*60, 210*60, 25*60*60, 49*60*60]
now = datetime.now(timezone.utc)
test_dates = [now - timedelta(seconds=s) for s in test_offsets]
test_archives = [MockArchive(date) for date in test_dates]

dotest(test_archives, '1H', [0])
dotest(test_archives, '2H', [0, 1])
dotest(test_archives, '3H', [0, 1, 2])
dotest(test_archives, '1H', [0])
dotest(test_archives, '2H', [0, 1])
dotest(test_archives, '3H', [0, 1, 2])
dotest(test_archives, '24H', [0, 1, 2, 3])
dotest(test_archives, '26H', [0, 1, 2, 3, 4])
dotest(test_archives, '2d', [0, 1, 2, 3, 4])
dotest(test_archives, '2d', [0, 1, 2, 3, 4])
dotest(test_archives, '50H', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '3d', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '1w', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '1m', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '1y', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '3d', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '1w', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '1m', [0, 1, 2, 3, 4, 5])
dotest(test_archives, '1y', [0, 1, 2, 3, 4, 5])


class StableDictTestCase(BaseTestCase):
Expand Down
1 change: 0 additions & 1 deletion borg/testsuite/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,3 @@ def test_access_acl(self):
self.set_acl(file2.name, b'!#acl 1\ngroup:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000000:staff:0:allow:read\nuser:FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000000:root:0:allow:read\n', numeric_owner=True)
self.assert_in(b'group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000000:wheel:0:allow:read', self.get_acl(file2.name)[b'acl_extended'])
self.assert_in(b'group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000000::0:allow:read', self.get_acl(file2.name, numeric_owner=True)[b'acl_extended'])

4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
python_files = testsuite/*.py

[flake8]
ignore = E123,E126,E127,E129,E203,E221,E226,E231,E241,E265,E301,E302,E303,E713,F401,F403,W291,W293,W391
ignore = E226,F403
max-line-length = 250
exclude = versioneer.py,docs/conf.py,borg/_version.py
exclude = versioneer.py,docs/conf.py,borg/_version.py,build,dist,.git,.idea,.cache
max-complexity = 100
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def make_distribution(self):
'borg/platform_freebsd.c',
'borg/platform_darwin.c',
])
super(Sdist, self).make_distribution()
super().make_distribution()

except ImportError:
class Sdist(versioneer.cmd_sdist):
Expand Down

0 comments on commit 4b81f38

Please sign in to comment.