From a2fc479da38a4e0e49b2cd31ad9ca400cc265188 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 6 Nov 2015 17:31:05 +0100 Subject: [PATCH] debug-put-obj command --- borg/archiver.py | 28 ++++++++++++++++++++++++++++ borg/testsuite/archiver.py | 17 ++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/borg/archiver.py b/borg/archiver.py index 9ad291aa1..fe46a20f5 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -3,6 +3,7 @@ from binascii import hexlify from datetime import datetime +from hashlib import sha256 from operator import attrgetter import functools import inspect @@ -514,6 +515,19 @@ def do_debug_dump_archive_items(self, args): 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 @@ def build_parser(self, args=None, prog=None): 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. """) diff --git a/borg/testsuite/archiver.py b/borg/testsuite/archiver.py index f9c438359..717c81aa6 100644 --- a/borg/testsuite/archiver.py +++ b/borg/testsuite/archiver.py @@ -777,20 +777,19 @@ def test_debug_dump_archive_items(self): 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 @@ def test_fuse_mount_archive(self): pass @unittest.skip('only works locally') - def test_debug_delete_obj(self): + def test_debug_put_delete_obj(self): pass