Przeglądaj źródła

attic delete: add repository + local cache deletion

Thomas Waldmann 10 lat temu
rodzic
commit
6e6819e626
4 zmienionych plików z 45 dodań i 17 usunięć
  1. 26 15
      attic/archiver.py
  2. 8 1
      attic/cache.py
  3. 4 1
      attic/remote.py
  4. 7 0
      attic/repository.py

+ 26 - 15
attic/archiver.py

@@ -224,18 +224,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)
-        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 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
                                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 @@ 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

@@ -38,7 +38,7 @@ class Cache(object):
         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 @@ class Cache(object):
         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)

+ 4 - 1
attic/remote.py

@@ -218,6 +218,9 @@ class RemoteRepository(object):
     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 @@ class RepositoryCache:
 def cache_if_remote(repository):
     if isinstance(repository, RemoteRepository):
         return RepositoryCache(repository)
-    return repository
+    return repository

+ 7 - 0
attic/repository.py

@@ -79,6 +79,13 @@ class Repository(object):
         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: