Browse Source

PR #226 - Merge branch 'delete_repo' into merge

Thomas Waldmann 10 years ago
parent
commit
118fb1a55c
4 changed files with 44 additions and 16 deletions
  1. 26 15
      attic/archiver.py
  2. 8 1
      attic/cache.py
  3. 3 0
      attic/remote.py
  4. 7 0
      attic/repository.py

+ 26 - 15
attic/archiver.py

@@ -263,18 +263,28 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
         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, do_files=args.cache_files)
-        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):
@@ -659,8 +669,9 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
                                help='the new archive name to use')
 
         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__,
@@ -670,9 +681,9 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
         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.

+ 8 - 1
attic/cache.py

@@ -77,7 +77,7 @@ class Cache:
         return answer and answer in 'Yy'
 
     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:
@@ -93,6 +93,13 @@ class Cache:
         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)

+ 3 - 0
attic/remote.py

@@ -250,6 +250,9 @@ class RemoteRepository:
     def rollback(self, *args):
         return self.call('rollback')
 
+    def destroy(self):
+        return self.call('destroy')
+
     def __len__(self):
         return self.call('__len__')
 

+ 7 - 0
attic/repository.py

@@ -78,6 +78,13 @@ class Repository:
         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):
         indices = sorted((int(name[6:]) for name in os.listdir(self.path) if name.startswith('index.') and name[6:].isdigit()))
         if indices: