Răsfoiți Sursa

debug-get-obj command

Thomas Waldmann 9 ani în urmă
părinte
comite
37c8aa2d42
2 a modificat fișierele cu 43 adăugiri și 2 ștergeri
  1. 36 0
      borg/archiver.py
  2. 7 2
      borg/testsuite/archiver.py

+ 36 - 0
borg/archiver.py

@@ -515,6 +515,26 @@ class Archiver:
         print('Done.')
         return EXIT_SUCCESS
 
+    def do_debug_get_obj(self, args):
+        """get object contents from the repository and write it into file"""
+        repository = self.open_repository(args.repository)
+        manifest, key = Manifest.load(repository)
+        hex_id = args.id
+        try:
+            id = unhexlify(hex_id)
+        except ValueError:
+            print("object id %s is invalid." % hex_id)
+        else:
+            try:
+                data =repository.get(id)
+            except repository.ObjectNotFound:
+                print("object %s not found." % hex_id)
+            else:
+                with open(args.path, "wb") as f:
+                    f.write(data)
+                print("object %s fetched." % hex_id)
+        return EXIT_SUCCESS
+
     def do_debug_put_obj(self, args):
         """put file(s) contents into the repository"""
         repository = self.open_repository(args.repository)
@@ -1048,6 +1068,22 @@ class Archiver:
                                type=location_validator(archive=True),
                                help='archive to dump')
 
+        debug_get_obj_epilog = textwrap.dedent("""
+        This command gets an object from the repository.
+        """)
+        subparser = subparsers.add_parser('debug-get-obj', parents=[common_parser],
+                                          description=self.do_debug_get_obj.__doc__,
+                                          epilog=debug_get_obj_epilog,
+                                          formatter_class=argparse.RawDescriptionHelpFormatter)
+        subparser.set_defaults(func=self.do_debug_get_obj)
+        subparser.add_argument('repository', metavar='REPOSITORY', nargs='?', default='',
+                               type=location_validator(archive=False),
+                               help='repository to use')
+        subparser.add_argument('id', metavar='ID', type=str,
+                               help='hex object ID to get from the repo')
+        subparser.add_argument('path', metavar='PATH', type=str,
+                               help='file to write object data into')
+
         debug_put_obj_epilog = textwrap.dedent("""
         This command puts objects into the repository.
         """)

+ 7 - 2
borg/testsuite/archiver.py

@@ -777,13 +777,18 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         assert len(output_dir) > 0 and output_dir[0].startswith('000000_')
         assert 'Done.' in output
 
-    def test_debug_put_delete_obj(self):
+    def test_debug_put_get_delete_obj(self):
         self.cmd('init', self.repository_location)
         data = b'some data'
         hexkey = sha256(data).hexdigest()
         self.create_regular_file('file', contents=data)
         output = self.cmd('debug-put-obj', self.repository_location, 'input/file')
         assert hexkey in output
+        output = self.cmd('debug-get-obj', self.repository_location, hexkey, 'output/file')
+        assert hexkey in output
+        with open('output/file', 'rb') as f:
+            data_read = f.read()
+        assert data == data_read
         output = self.cmd('debug-delete-obj', self.repository_location, hexkey)
         assert "deleted" in output
         output = self.cmd('debug-delete-obj', self.repository_location, hexkey)
@@ -901,5 +906,5 @@ class RemoteArchiverTestCase(ArchiverTestCase):
         pass
 
     @unittest.skip('only works locally')
-    def test_debug_put_delete_obj(self):
+    def test_debug_put_get_delete_obj(self):
         pass