From 6e6819e626039e5fd66878ce4b6c8b4b40b73c97 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Mon, 9 Mar 2015 16:02:06 +0100 Subject: [PATCH] attic delete: add repository + local cache deletion --- attic/archiver.py | 41 ++++++++++++++++++++++++++--------------- attic/cache.py | 9 ++++++++- attic/remote.py | 5 ++++- attic/repository.py | 7 +++++++ 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/attic/archiver.py b/attic/archiver.py index 47650c2d4..7e6f38297 100644 --- a/attic/archiver.py +++ b/attic/archiver.py @@ -224,18 +224,28 @@ def do_extract(self, args): return self.exit_code def do_delete(self, args): - """Delete an existing archive""" - repository = self.open_repository(args.archive, exclusive=True) + """Delete an existing repository or archive""" + repository = self.open_repository(args.target, exclusive=True) manifest, key = Manifest.load(repository) cache = Cache(repository, key, manifest) - archive = Archive(repository, key, manifest, args.archive.archive, cache=cache) - stats = Statistics() - archive.delete(stats) - manifest.write() - repository.commit() - cache.commit() - if args.stats: - stats.print_('Deleted data:', cache) + if args.target.archive: + archive = Archive(repository, key, manifest, args.target.archive, cache=cache) + stats = Statistics() + archive.delete(stats) + manifest.write() + repository.commit() + cache.commit() + if args.stats: + stats.print_('Deleted data:', cache) + else: + print("You requested to completely DELETE the repository *including* all archives it contains:") + for archive in sorted(Archive.list_archives(repository, key, manifest), key=attrgetter('ts')): + print(format_archive(archive)) + print("""Type "YES" if you understand this and want to continue.\n""") + if input('Do you want to continue? ') == 'YES': + repository.destroy() + cache.destroy() + print("Repository and corresponding cache were deleted.") return self.exit_code def do_mount(self, args): @@ -591,8 +601,9 @@ def run(self, args=None): help='paths to extract') delete_epilog = textwrap.dedent(""" - This command deletes an archive from the repository. Any disk space not - shared with any other existing archive is also reclaimed. + This command deletes an archive from the repository or the complete repository. + Disk space is reclaimed accordingly. If you delete the complete repository, the + local cache for it (if any) is also deleted. """) subparser = subparsers.add_parser('delete', parents=[common_parser], description=self.do_delete.__doc__, @@ -602,9 +613,9 @@ def run(self, args=None): subparser.add_argument('-s', '--stats', dest='stats', action='store_true', default=False, help='print statistics for the deleted archive') - subparser.add_argument('archive', metavar='ARCHIVE', - type=location_validator(archive=True), - help='archive to delete') + subparser.add_argument('target', metavar='TARGET', + type=location_validator(), + help='archive or repository to delete') list_epilog = textwrap.dedent(""" This command lists the contents of a repository or an archive. diff --git a/attic/cache.py b/attic/cache.py index 7bec89c16..d8170440c 100644 --- a/attic/cache.py +++ b/attic/cache.py @@ -38,7 +38,7 @@ def __del__(self): self.close() def create(self): - """Create a new empty cache at `path` + """Create a new empty cache at `self.path` """ os.makedirs(self.path) with open(os.path.join(self.path, 'README'), 'w') as fd: @@ -54,6 +54,13 @@ def create(self): with open(os.path.join(self.path, 'files'), 'w') as fd: pass # empty file + def destroy(self): + """destroy the cache at `self.path` + """ + self.close() + os.remove(os.path.join(self.path, 'config')) # kill config first + shutil.rmtree(self.path) + def open(self): if not os.path.isdir(self.path): raise Exception('%s Does not look like an Attic cache' % self.path) diff --git a/attic/remote.py b/attic/remote.py index f2a0aed06..7169a9eec 100644 --- a/attic/remote.py +++ b/attic/remote.py @@ -218,6 +218,9 @@ def commit(self, *args): def rollback(self, *args): return self.call('rollback') + def destroy(self): + return self.call('destroy') + def __len__(self): return self.call('__len__') @@ -312,4 +315,4 @@ def get_many(self, keys): def cache_if_remote(repository): if isinstance(repository, RemoteRepository): return RepositoryCache(repository) - return repository \ No newline at end of file + return repository diff --git a/attic/repository.py b/attic/repository.py index eed85dc43..08ff6d6da 100644 --- a/attic/repository.py +++ b/attic/repository.py @@ -79,6 +79,13 @@ def create(self, path): with open(os.path.join(path, 'config'), 'w') as fd: config.write(fd) + def destroy(self): + """Destroy the repository at `self.path` + """ + self.close() + os.remove(os.path.join(self.path, 'config')) # kill config first + shutil.rmtree(self.path) + def get_index_transaction_id(self): indicies = sorted((int(name[6:]) for name in os.listdir(self.path) if name.startswith('index.') and name[6:].isdigit())) if indicies: