浏览代码

rewrite converter to avoid using attic code

the unit tests themselves still use attic to generate an attic
repository for testing, but the converter code should now be
standalone
Antoine Beaupré 9 年之前
父节点
当前提交
312c3cf738
共有 1 个文件被更改,包括 36 次插入6 次删除
  1. 36 6
      borg/testsuite/convert.py

+ 36 - 6
borg/testsuite/convert.py

@@ -1,4 +1,4 @@
-import binascii
+from binascii import hexlify
 import os
 import pytest
 import shutil
@@ -20,6 +20,37 @@ class AtticKeyfileKey(KeyfileKey):
     '''backwards compatible Attick key file parser'''
     FILE_ID = 'ATTIC KEY'
 
+    # verbatim copy from attic
+    @staticmethod
+    def get_keys_dir():
+        """Determine where to repository keys and cache"""
+        return os.environ.get('ATTIC_KEYS_DIR',
+                              os.path.join(os.path.expanduser('~'), '.attic', 'keys'))
+
+    @classmethod
+    def find_key_file(cls, repository):
+        '''copy of attic's `find_key_file`_
+
+        this has two small modifications:
+
+        1. it uses the above `get_keys_dir`_ instead of the global one,
+           assumed to be borg's
+
+        2. it uses `repository.path`_ instead of
+           `repository._location.canonical_path`_ because we can't
+           assume the repository has been opened by the archiver yet
+        '''
+        get_keys_dir = cls.get_keys_dir
+        id = hexlify(repository.id).decode('ascii')
+        keys_dir = get_keys_dir()
+        for name in os.listdir(keys_dir):
+            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[10:] == id:
+                    return filename
+        raise KeyfileNotFoundError(repository.path, get_keys_dir())
+
 class ConversionTestCase(BaseTestCase):
 
     class MockArgs:
@@ -116,11 +147,10 @@ class ConversionTestCase(BaseTestCase):
         no key is found. whether that exception is from Borg or Attic
         is unclear.
 
-        this is split in a separate function in case we want to
-        reimplement the attic code here.
-        '''
-        self.repository._location = attic.helpers.Location(self.tmppath)
-        return attic.key.KeyfileKey().find_key_file(self.repository)
+        this is split in a separate function in case we want to use
+        the attic code here directly, instead of our local
+        implementation.'''
+        return AtticKeyfileKey.find_key_file(self.repository)
 
     def convert_keyfiles(self, keyfile):