Browse Source

debug-put-obj command

Thomas Waldmann 9 năm trước cách đây
mục cha
commit
a2fc479da3
2 tập tin đã thay đổi với 36 bổ sung9 xóa
  1. 28 0
      borg/archiver.py
  2. 8 9
      borg/testsuite/archiver.py

+ 28 - 0
borg/archiver.py

@@ -3,6 +3,7 @@ from .support import argparse  # see support/__init__.py docstring
 
 from binascii import hexlify
 from datetime import datetime
+from hashlib import sha256
 from operator import attrgetter
 import functools
 import inspect
@@ -514,6 +515,19 @@ class Archiver:
         print('Done.')
         return EXIT_SUCCESS
 
+    def do_debug_put_obj(self, args):
+        """put file(s) contents into the repository"""
+        repository = self.open_repository(args.repository)
+        manifest, key = Manifest.load(repository)
+        for path in args.paths:
+            with open(path, "rb") as f:
+                data = f.read()
+            h = sha256(data)  # XXX hardcoded
+            repository.put(h.digest(), data)
+            print("object %s put." % h.hexdigest())
+        repository.commit()
+        return EXIT_SUCCESS
+
     def do_debug_delete_obj(self, args):
         """delete the objects with the given IDs from the repo"""
         repository = self.open_repository(args.repository)
@@ -1034,6 +1048,20 @@ class Archiver:
                                type=location_validator(archive=True),
                                help='archive to dump')
 
+        debug_put_obj_epilog = textwrap.dedent("""
+        This command puts objects into the repository.
+        """)
+        subparser = subparsers.add_parser('debug-put-obj', parents=[common_parser],
+                                          description=self.do_debug_put_obj.__doc__,
+                                          epilog=debug_put_obj_epilog,
+                                          formatter_class=argparse.RawDescriptionHelpFormatter)
+        subparser.set_defaults(func=self.do_debug_put_obj)
+        subparser.add_argument('repository', metavar='REPOSITORY', nargs='?', default='',
+                               type=location_validator(archive=False),
+                               help='repository to use')
+        subparser.add_argument('paths', metavar='PATH', nargs='+', type=str,
+                               help='file(s) to read and create object(s) from')
+
         debug_delete_obj_epilog = textwrap.dedent("""
         This command deletes objects from the repository.
         """)

+ 8 - 9
borg/testsuite/archiver.py

@@ -777,20 +777,19 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         assert len(output_dir) > 0 and output_dir[0].startswith('000000_')
         assert 'Done.' in output
 
-    def test_debug_delete_obj(self):
+    def test_debug_put_delete_obj(self):
         self.cmd('init', self.repository_location)
-        repository = Repository(self.repository_location)
         data = b'some data'
-        h = sha256(data)
-        key, hexkey = h.digest(), h.hexdigest()
-        repository.put(key, data)
-        repository.commit()
-        output = self.cmd('debug-delete-obj', self.repository_location, 'invalid')
-        assert "is invalid" in output
+        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-delete-obj', self.repository_location, hexkey)
         assert "deleted" in output
         output = self.cmd('debug-delete-obj', self.repository_location, hexkey)
         assert "not found" in output
+        output = self.cmd('debug-delete-obj', self.repository_location, 'invalid')
+        assert "is invalid" in output
 
 
 @unittest.skipUnless('binary' in BORG_EXES, 'no borg.exe available')
@@ -902,5 +901,5 @@ class RemoteArchiverTestCase(ArchiverTestCase):
         pass
 
     @unittest.skip('only works locally')
-    def test_debug_delete_obj(self):
+    def test_debug_put_delete_obj(self):
         pass