From 37c8aa2d428b2a7170fc894512ffed124318c2cf Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 6 Nov 2015 17:45:30 +0100 Subject: [PATCH] debug-get-obj command --- borg/archiver.py | 36 ++++++++++++++++++++++++++++++++++++ borg/testsuite/archiver.py | 9 +++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/borg/archiver.py b/borg/archiver.py index fe46a20f5..1af8e46e5 100644 --- a/borg/archiver.py +++ b/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. """) diff --git a/borg/testsuite/archiver.py b/borg/testsuite/archiver.py index 717c81aa6..8e7cbf6e8 100644 --- a/borg/testsuite/archiver.py +++ b/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