浏览代码

Merge branch 'python_cleanup' into merge

Conflicts:
	attic/archiver.py
	attic/remote.py
Thomas Waldmann 10 年之前
父节点
当前提交
b7cefacb69

+ 13 - 9
attic/_version.py

@@ -41,6 +41,7 @@ import sys
 import re
 import os.path
 
+
 def get_expanded_variables(versionfile_source):
     # the code embedded in _version.py can just fetch the value of these
     # variables. When used from setup.py, we don't want to import
@@ -48,7 +49,7 @@ def get_expanded_variables(versionfile_source):
     # used from _version.py.
     variables = {}
     try:
-        for line in open(versionfile_source,"r").readlines():
+        for line in open(versionfile_source, "r").readlines():
             if line.strip().startswith("git_refnames ="):
                 mo = re.search(r'=\s*"(.*)"', line)
                 if mo:
@@ -61,12 +62,13 @@ def get_expanded_variables(versionfile_source):
         pass
     return variables
 
+
 def versions_from_expanded_variables(variables, tag_prefix, verbose=False):
     refnames = variables["refnames"].strip()
     if refnames.startswith("$Format"):
         if verbose:
             print("variables are unexpanded, not using")
-        return {} # unexpanded, so not in an unpacked git-archive tarball
+        return {}  # unexpanded, so not in an unpacked git-archive tarball
     refs = set([r.strip() for r in refnames.strip("()").split(",")])
     for ref in list(refs):
         if not re.search(r'\d', ref):
@@ -87,13 +89,14 @@ def versions_from_expanded_variables(variables, tag_prefix, verbose=False):
             r = ref[len(tag_prefix):]
             if verbose:
                 print("picking %s" % r)
-            return { "version": r,
-                     "full": variables["full"].strip() }
+            return {"version": r,
+                    "full": variables["full"].strip()}
     # no suitable tags, so we use the full revision id
     if verbose:
         print("no suitable tags, using full revision id")
-    return { "version": variables["full"].strip(),
-             "full": variables["full"].strip() }
+    return {"version": variables["full"].strip(),
+            "full": variables["full"].strip()}
+
 
 def versions_from_vcs(tag_prefix, versionfile_source, verbose=False):
     # this runs 'git' from the root of the source tree. That either means
@@ -110,7 +113,7 @@ def versions_from_vcs(tag_prefix, versionfile_source, verbose=False):
         here = os.path.abspath(__file__)
     except NameError:
         # some py2exe/bbfreeze/non-CPython implementations don't do __file__
-        return {} # not always correct
+        return {}  # not always correct
 
     # versionfile_source is the relative path from the top of the source tree
     # (where the .git directory might live) to this file. Invert this to find
@@ -157,7 +160,7 @@ def versions_from_parentdir(parentdir_prefix, versionfile_source, verbose=False)
             here = os.path.abspath(__file__)
         except NameError:
             # py2exe/bbfreeze/non-CPython don't have __file__
-            return {} # without __file__, we have no hope
+            return {}  # without __file__, we have no hope
         # versionfile_source is the relative path from the top of the source
         # tree to _version.py. Invert this to find the root from __file__.
         root = here
@@ -183,8 +186,9 @@ tag_prefix = ""
 parentdir_prefix = "Attic-"
 versionfile_source = "attic/_version.py"
 
+
 def get_versions(default={"version": "unknown", "full": ""}, verbose=False):
-    variables = { "refnames": git_refnames, "full": git_full }
+    variables = {"refnames": git_refnames, "full": git_full}
     ver = versions_from_expanded_variables(variables, tag_prefix, verbose)
     if not ver:
         ver = versions_from_vcs(tag_prefix, versionfile_source, verbose)

+ 10 - 7
attic/archive.py

@@ -141,7 +141,7 @@ class Archive:
             i = 0
             while True:
                 self.checkpoint_name = '%s.checkpoint%s' % (name, i and ('.%d' % i) or '')
-                if not self.checkpoint_name in manifest.archives:
+                if self.checkpoint_name not in manifest.archives:
                     break
                 i += 1
         else:
@@ -211,6 +211,7 @@ class Archive:
             count, size, csize = self.cache.chunks[id]
             stats.update(size, csize, count == 1)
             self.cache.chunks[id] = count - 1, size, csize
+
         def add_file_chunks(chunks):
             for id, _, _ in chunks:
                 add(id)
@@ -555,7 +556,7 @@ class ArchiveChecker:
         self.repository = repository
         self.init_chunks()
         self.key = self.identify_key(repository)
-        if not Manifest.MANIFEST_ID in self.chunks:
+        if Manifest.MANIFEST_ID not in self.chunks:
             self.manifest = self.rebuild_manifest()
         else:
             self.manifest, _ = Manifest.load(repository, key=self.key)
@@ -606,11 +607,11 @@ class ArchiveChecker:
             # Some basic sanity checks of the payload before feeding it into msgpack
             if len(data) < 2 or ((data[0] & 0xf0) != 0x80) or ((data[1] & 0xe0) != 0xa0):
                 continue
-            if not b'cmdline' in data or not b'\xa7version\x01' in data:
+            if b'cmdline' not in data or b'\xa7version\x01' not in data:
                 continue
             try:
                 archive = msgpack.unpackb(data)
-            except:
+            except Exception:
                 continue
             if isinstance(archive, dict) and b'items' in archive and b'cmdline' in archive:
                 self.report_progress('Found archive ' + archive[b'name'].decode('utf-8'), error=True)
@@ -655,7 +656,7 @@ class ArchiveChecker:
             offset = 0
             chunk_list = []
             for chunk_id, size, csize in item[b'chunks']:
-                if not chunk_id in self.chunks:
+                if chunk_id not in self.chunks:
                     # If a file chunk is missing, create an all empty replacement chunk
                     self.report_progress('{}: Missing file chunk detected (Byte {}-{})'.format(item[b'path'].decode('utf-8', 'surrogateescape'), offset, offset + size), error=True)
                     data = bytes(size)
@@ -676,11 +677,13 @@ class ArchiveChecker:
             """
             unpacker = RobustUnpacker(lambda item: isinstance(item, dict) and b'path' in item)
             _state = 0
+
             def missing_chunk_detector(chunk_id):
                 nonlocal _state
-                if _state % 2 != int(not chunk_id in self.chunks):
+                if _state % 2 != int(chunk_id not in self.chunks):
                     _state += 1
                 return _state
+
             for state, items in groupby(archive[b'items'], missing_chunk_detector):
                 items = list(items)
                 if state % 2:
@@ -701,7 +704,7 @@ class ArchiveChecker:
         for i, (name, info) in enumerate(archive_items[:end]):
             self.report_progress('Analyzing archive {} ({}/{})'.format(name, num_archives - i, num_archives))
             archive_id = info[b'id']
-            if not archive_id in self.chunks:
+            if archive_id not in self.chunks:
                 self.report_progress('Archive metadata block is missing', error=True)
                 del self.manifest.archives[name]
                 continue

+ 7 - 8
attic/archiver.py

@@ -304,9 +304,10 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
                         extra = ' link to %s' % item[b'source']
                 else:
                     extra = ''
-                print('%s%s %-6s %-6s %8d %s %s%s' % (type, mode, item[b'user'] or item[b'uid'],
-                                                  item[b'group'] or item[b'gid'], size, format_time(mtime),
-                                                  remove_surrogates(item[b'path']), extra))
+                print('%s%s %-6s %-6s %8d %s %s%s' % (
+                    type, mode, item[b'user'] or item[b'uid'],
+                    item[b'group'] or item[b'gid'], size, format_time(mtime),
+                    remove_surrogates(item[b'path']), extra))
         else:
             for archive in sorted(Archive.list_archives(repository, key, manifest), key=attrgetter('ts')):
                 print(format_archive(archive))
@@ -459,11 +460,9 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
                     """).lstrip())
         common_parser = argparse.ArgumentParser(add_help=False)
         common_parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
-                            default=False,
-                            help='verbose output')
-        common_parser.add_argument('--no-files-cache', dest='cache_files', action='store_false',
-                            default=True,
-                            help='do not use the "files" cache')
+                                   default=False,
+                                   help='verbose output')
+        common_parser.add_argument('--no-files-cache', dest='cache_files', action='store_false')
 
         # We can't use argparse for "serve" since we don't want it to show up in "Available commands"
         if args:

+ 1 - 1
attic/cache.py

@@ -10,7 +10,7 @@ from .helpers import Error, get_cache_dir, decode_dict, st_mtime_ns, unhexlify,
 from .hashindex import ChunkIndex
 
 
-class Cache(object):
+class Cache:
     """Client Side cache
     """
     class RepositoryReplay(Error):

+ 1 - 3
attic/fuse.py

@@ -234,7 +234,5 @@ class AtticOperations(llfuse.Operations):
             daemonize()
         try:
             llfuse.main(single=True)
-        except:
+        finally:
             llfuse.close()
-            raise
-        llfuse.close()

+ 3 - 2
attic/helpers.py

@@ -101,7 +101,7 @@ class Manifest:
         m = msgpack.unpackb(data)
         if not m.get(b'version') == 1:
             raise ValueError('Invalid manifest version')
-        manifest.archives = dict((k.decode('utf-8'), v) for k,v in m[b'archives'].items())
+        manifest.archives = dict((k.decode('utf-8'), v) for k, v in m[b'archives'].items())
         manifest.timestamp = m.get(b'timestamp')
         if manifest.timestamp:
             manifest.timestamp = manifest.timestamp.decode('ascii')
@@ -144,7 +144,8 @@ def prune_split(archives, pattern, n, skip=[]):
             last = period
             if a not in skip:
                 keep.append(a)
-                if len(keep) == n: break
+                if len(keep) == n:
+                    break
     return keep
 
 

+ 2 - 1
attic/key.py

@@ -17,6 +17,7 @@ class UnsupportedPayloadError(Error):
     """Unsupported payload type {}. A newer version is required to access this repository.
     """
 
+
 class HMAC(hmac.HMAC):
     """Workaround a bug in Python < 3.4 Where HMAC does not accept memoryviews
     """
@@ -44,7 +45,7 @@ def key_factory(repository, manifest_data):
         raise UnsupportedPayloadError(manifest_data[0])
 
 
-class KeyBase(object):
+class KeyBase:
 
     def __init__(self):
         self.TYPE_STR = bytes([self.TYPE])

+ 1 - 0
attic/platform.py

@@ -13,5 +13,6 @@ else:
 
     def acl_get(path, item, numeric_owner=False):
         pass
+
     def acl_set(path, item, numeric_owner=False):
         pass

+ 5 - 3
attic/remote.py

@@ -25,7 +25,8 @@ class PathNotAllowed(Error):
 class InvalidRPCMethod(Error):
     """RPC method is not valid"""
 
-class RepositoryServer(object):
+
+class RepositoryServer:
     rpc_methods = (
             '__len__',
             'check',
@@ -99,7 +100,7 @@ class RepositoryServer(object):
         return self.repository.id
 
 
-class RemoteRepository(object):
+class RemoteRepository:
     extra_test_args = []
 
     class RPCError(Exception):
@@ -151,6 +152,7 @@ class RemoteRepository(object):
     def call_many(self, cmd, calls, wait=True, is_preloaded=False):
         if not calls:
             return
+
         def fetch_from_cache(args):
             msgid = self.cache[args].pop(0)
             if not self.cache[args]:
@@ -319,7 +321,7 @@ class RepositoryCache:
         return next(self.get_many([key]))
 
     def get_many(self, keys):
-        unknown_keys = [key for key in keys if not key in self.index]
+        unknown_keys = [key for key in keys if key not in self.index]
         repository_iterator = zip(unknown_keys, self.repository.get_many(unknown_keys))
         for key in keys:
             try:

+ 8 - 7
attic/repository.py

@@ -18,7 +18,7 @@ TAG_DELETE = 1
 TAG_COMMIT = 2
 
 
-class Repository(object):
+class Repository:
     """Filesystem based transactional key value store
 
     On disk layout:
@@ -32,19 +32,19 @@ class Repository(object):
     DEFAULT_SEGMENTS_PER_DIR = 10000
 
     class DoesNotExist(Error):
-        """Repository {} does not exist"""
+        """Repository {} does not exist."""
 
     class AlreadyExists(Error):
-        """Repository {} already exists"""
+        """Repository {} already exists."""
 
     class InvalidRepository(Error):
-        """{} is not a valid repository"""
+        """{} is not a valid repository."""
 
     class CheckNeeded(Error):
         """Inconsistency detected. Please run "attic check {}"."""
 
     class ObjectNotFound(Error):
-        """Object with key {} not found in repository {}"""
+        """Object with key {} not found in repository {}."""
 
     def __init__(self, path, create=False, exclusive=False):
         self.path = path
@@ -106,7 +106,7 @@ class Repository(object):
             raise self.DoesNotExist(path)
         self.config = RawConfigParser()
         self.config.read(os.path.join(self.path, 'config'))
-        if not 'repository' in self.config.sections() or self.config.getint('repository', 'version') != 1:
+        if 'repository' not in self.config.sections() or self.config.getint('repository', 'version') != 1:
             raise self.InvalidRepository(path)
         self.lock = UpgradableLock(os.path.join(path, 'config'), exclusive)
         self.max_segment_size = self.config.getint('repository', 'max_segment_size')
@@ -240,6 +240,7 @@ class Repository(object):
         the index is consistent with the data stored in the segments.
         """
         error_found = False
+
         def report_error(msg):
             nonlocal error_found
             error_found = True
@@ -377,7 +378,7 @@ class Repository(object):
         """
 
 
-class LoggedIO(object):
+class LoggedIO:
 
     header_fmt = struct.Struct('<IIB')
     assert header_fmt.size == 9

+ 6 - 7
attic/testsuite/archiver.py

@@ -205,8 +205,8 @@ class ArchiverTestCase(ArchiverTestCaseBase):
     def test_exclude_caches(self):
         self.attic('init', self.repository_location)
         self.create_regular_file('file1', size=1024 * 80)
-        self.create_regular_file('cache1/CACHEDIR.TAG', contents = b'Signature: 8a477f597d28d172789f06886806bc55 extra stuff')
-        self.create_regular_file('cache2/CACHEDIR.TAG', contents = b'invalid signature')
+        self.create_regular_file('cache1/CACHEDIR.TAG', contents=b'Signature: 8a477f597d28d172789f06886806bc55 extra stuff')
+        self.create_regular_file('cache2/CACHEDIR.TAG', contents=b'invalid signature')
         self.attic('create', '--exclude-caches', self.repository_location + '::test', 'input')
         with changedir('output'):
             self.attic('extract', self.repository_location + '::test')
@@ -286,10 +286,9 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         self.attic('extract', '--dry-run', self.repository_location + '::test')
         self.attic('check', self.repository_location)
         name = sorted(os.listdir(os.path.join(self.tmpdir, 'repository', 'data', '0')), reverse=True)[0]
-        fd = open(os.path.join(self.tmpdir, 'repository', 'data', '0', name), 'r+')
-        fd.seek(100)
-        fd.write('XXXX')
-        fd.close()
+        with open(os.path.join(self.tmpdir, 'repository', 'data', '0', name), 'r+') as fd:
+            fd.seek(100)
+            fd.write('XXXX')
         self.attic('check', self.repository_location, exit_code=1)
 
     def test_readonly_repository(self):
@@ -381,7 +380,7 @@ class ArchiverTestCase(ArchiverTestCaseBase):
             for key, _ in repository.open_index(repository.get_transaction_id()).iteritems():
                 data = repository.get(key)
                 hash = sha256(data).digest()
-                if not hash in seen:
+                if hash not in seen:
                     seen.add(hash)
                     num_blocks = num_aes_blocks(len(data) - 41)
                     nonce = bytes_to_long(data[33:41])

+ 3 - 3
attic/testsuite/crypto.py

@@ -14,11 +14,11 @@ class CryptoTestCase(AtticTestCase):
 
     def test_pbkdf2_sha256(self):
         self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 1, 32)),
-                         b'120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b')
+                          b'120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b')
         self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 2, 32)),
-                         b'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43')
+                          b'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43')
         self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 4096, 32)),
-                         b'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a')
+                          b'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a')
 
     def test_get_random_bytes(self):
         bytes = get_random_bytes(10)

+ 2 - 2
attic/testsuite/helpers.py

@@ -125,7 +125,7 @@ class UpgradableLockTestCase(AtticTestCase):
         lock.release()
 
 
-class MockArchive(object):
+class MockArchive:
 
     def __init__(self, ts):
         self.ts = ts
@@ -151,7 +151,7 @@ class PruneSplitTestCase(AtticTestCase):
                 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_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]
 

+ 3 - 3
attic/testsuite/key.py

@@ -11,7 +11,7 @@ from attic.helpers import Location, unhexlify
 
 class KeyTestCase(AtticTestCase):
 
-    class MockArgs(object):
+    class MockArgs:
         repository = Location(tempfile.mkstemp()[1])
 
     keyfile2_key_file = """
@@ -37,8 +37,8 @@ class KeyTestCase(AtticTestCase):
     def tearDown(self):
         shutil.rmtree(self.tmppath)
 
-    class MockRepository(object):
-        class _Location(object):
+    class MockRepository:
+        class _Location:
             orig = '/some/place'
 
         _location = _Location()

+ 0 - 1
attic/testsuite/repository.py

@@ -159,7 +159,6 @@ class RepositoryCommitTestCase(RepositoryTestCaseBase):
             self.assert_raises(UpgradableLock.WriteLockFailed, lambda: len(self.repository))
             upgrade.assert_called_once()
 
-
     def test_crash_before_write_index(self):
         self.add_keys()
         self.repository.write_index = None

+ 1 - 0
attic/testsuite/xattr.py

@@ -4,6 +4,7 @@ import unittest
 from attic.testsuite import AtticTestCase
 from attic.xattr import is_enabled, getxattr, setxattr, listxattr
 
+
 @unittest.skipUnless(is_enabled(), 'xattr not enabled on filesystem')
 class XattrTestCase(AtticTestCase):
 

+ 1 - 1
setup.py

@@ -7,7 +7,7 @@ import versioneer
 versioneer.versionfile_source = 'attic/_version.py'
 versioneer.versionfile_build = 'attic/_version.py'
 versioneer.tag_prefix = ''
-versioneer.parentdir_prefix = 'Attic-' # dirname like 'myproject-1.2.0'
+versioneer.parentdir_prefix = 'Attic-'  # dirname like 'myproject-1.2.0'
 
 platform = os.uname()[0]