瀏覽代碼

add dry run support to converter

Antoine Beaupré 9 年之前
父節點
當前提交
f35e8e17f2
共有 2 個文件被更改,包括 18 次插入15 次删除
  1. 17 14
      borg/converter.py
  2. 1 1
      borg/testsuite/convert.py

+ 17 - 14
borg/converter.py

@@ -9,7 +9,7 @@ class NotImplementedException(Exception):
     pass
 
 class AtticRepositoryConverter(Repository):
-    def convert(self):
+    def convert(self, dryrun=True):
         '''convert an attic repository to a borg repository
 
         those are the files that need to be converted here, from most
@@ -23,13 +23,13 @@ class AtticRepositoryConverter(Repository):
         except KeyfileNotFoundError:
             print("no key file found for repository")
         else:
-            self.convert_keyfiles(keyfile)
+            self.convert_keyfiles(keyfile, dryrun)
         self.close()
-        self.convert_segments(segments)
-        self.convert_cache()
+        self.convert_segments(segments, dryrun)
+        self.convert_cache(dryrun)
 
     @staticmethod
-    def convert_segments(segments):
+    def convert_segments(segments, dryrun):
         '''convert repository segments from attic to borg
 
         replacement pattern is `s/ATTICSEG/BORG_SEG/` in files in
@@ -39,6 +39,8 @@ class AtticRepositoryConverter(Repository):
         replace the 8 first bytes of all regular files in there.'''
         for filename in segments:
             print("converting segment %s in place" % filename)
+            if dryrun:
+                continue
             with open(filename, 'r+b') as segment:
                 segment.seek(0)
                 segment.write(MAGIC)
@@ -63,7 +65,7 @@ class AtticRepositoryConverter(Repository):
         return AtticKeyfileKey.find_key_file(self)
 
     @staticmethod
-    def convert_keyfiles(keyfile):
+    def convert_keyfiles(keyfile, dryrun):
 
         '''convert key files from attic to borg
 
@@ -85,13 +87,14 @@ class AtticRepositoryConverter(Repository):
         keyfile = os.path.join(get_keys_dir(),
                                os.path.basename(keyfile))
         print("writing borg keyfile to %s" % keyfile)
-        with open(keyfile, 'w') as f:
-            f.write(data)
-        with open(keyfile, 'r') as f:
-            data = f.read()
-        assert data.startswith(KeyfileKey.FILE_ID)
-
-    def convert_cache(self):
+        if not dryrun:
+            with open(keyfile, 'w') as f:
+                f.write(data)
+            with open(keyfile, 'r') as f:
+                data = f.read()
+            assert data.startswith(KeyfileKey.FILE_ID)
+
+    def convert_cache(self, dryrun):
         '''convert caches from attic to borg
 
         those are all hash indexes, so we need to
@@ -109,7 +112,7 @@ class AtticRepositoryConverter(Repository):
           `Cache.open()`, edit in place and then `Cache.close()` to
           make sure we have locking right
         '''
-        raise NotImplementedException('not implemented')
+        raise NotImplementedException('cache conversion not implemented, next borg backup will take longer to rebuild those caches')
 
 class AtticKeyfileKey(KeyfileKey):
     '''backwards compatible Attick key file parser'''

+ 1 - 1
borg/testsuite/convert.py

@@ -59,7 +59,7 @@ class ConversionTestCase(BaseTestCase):
         self.repository.close()
         print("opening attic repository with borg and converting")
         with pytest.raises(NotImplementedException):
-            self.open(self.tmppath, repo_type = AtticRepositoryConverter).convert()
+            self.open(self.tmppath, repo_type = AtticRepositoryConverter).convert(dryrun=False)
         # check that the new keyfile is alright
         keyfile = os.path.join(get_keys_dir(),
                                os.path.basename(self.key.path))