borg/dedupestore/archiver.py

170 lines
6.8 KiB
Python
Raw Normal View History

2010-10-15 18:35:49 +00:00
import argparse
2010-10-20 17:59:15 +00:00
import logging
2010-10-16 09:45:36 +00:00
import sys
2010-02-23 21:12:22 +00:00
2010-10-20 17:59:15 +00:00
from .archive import Archive
2010-10-19 19:12:12 +00:00
from .bandstore import BandStore
2010-10-20 17:59:15 +00:00
from .cache import Cache
from .crypto import CryptoManager, KeyChain
2010-10-20 17:59:15 +00:00
from .helpers import location_validator, pretty_size, LevelFilter
class Archiver(object):
2010-10-15 18:35:49 +00:00
def open_store(self, location):
store = BandStore(location.path)
return store
2010-02-23 20:34:28 +00:00
2010-10-16 09:45:36 +00:00
def exit_code_from_logger(self):
2010-10-20 17:59:15 +00:00
return 1 if self.level_filter.count.get('ERROR') else 0
2010-10-16 09:45:36 +00:00
2010-10-15 18:35:49 +00:00
def do_create(self, args):
store = self.open_store(args.archive)
keychain = KeyChain(args.keychain)
crypto = CryptoManager(keychain)
archive = Archive(store, crypto)
2010-10-23 17:41:31 +00:00
cache = Cache(store, archive.crypto)
2010-10-15 18:35:49 +00:00
archive.create(args.archive.archive, args.paths, cache)
2010-10-16 09:45:36 +00:00
return self.exit_code_from_logger()
2010-02-20 21:28:46 +00:00
2010-10-15 18:35:49 +00:00
def do_extract(self, args):
store = self.open_store(args.archive)
keychain = KeyChain(args.keychain)
crypto = CryptoManager(keychain)
archive = Archive(store, crypto, args.archive.archive)
2010-10-15 18:35:49 +00:00
archive.extract(args.dest)
2010-10-16 09:45:36 +00:00
return self.exit_code_from_logger()
2010-10-15 18:35:49 +00:00
def do_delete(self, args):
store = self.open_store(args.archive)
keychain = KeyChain(args.keychain)
crypto = CryptoManager(keychain)
archive = Archive(store, crypto, args.archive.archive)
2010-10-23 17:41:31 +00:00
cache = Cache(store, archive.crypto)
2010-10-15 18:35:49 +00:00
archive.delete(cache)
2010-10-16 09:45:36 +00:00
return self.exit_code_from_logger()
2010-10-15 18:35:49 +00:00
def do_list(self, args):
store = self.open_store(args.src)
keychain = KeyChain(args.keychain)
crypto = CryptoManager(keychain)
2010-10-15 18:35:49 +00:00
if args.src.archive:
archive = Archive(store, crypto, args.src.archive)
2010-10-15 18:35:49 +00:00
archive.list()
else:
for archive in Archive.list_archives(store, crypto):
2010-10-25 17:17:48 +00:00
print archive.name
2010-10-16 09:45:36 +00:00
return self.exit_code_from_logger()
2010-10-15 18:35:49 +00:00
def do_verify(self, args):
store = self.open_store(args.archive)
keychain = KeyChain(args.keychain)
crypto = CryptoManager(keychain)
archive = Archive(store, crypto, args.archive.archive)
2010-10-15 18:35:49 +00:00
archive.verify()
2010-10-16 09:45:36 +00:00
return self.exit_code_from_logger()
2010-10-15 18:35:49 +00:00
def do_info(self, args):
store = self.open_store(args.archive)
keychain = KeyChain(args.keychain)
crypto = CryptoManager(keychain)
archive = Archive(store, crypto, args.archive.archive)
2010-10-23 17:41:31 +00:00
cache = Cache(store, archive.crypto)
2010-10-20 19:08:46 +00:00
osize, csize, usize = archive.stats(cache)
print 'Original size:', pretty_size(osize)
print 'Compressed size:', pretty_size(csize)
print 'Unique data:', pretty_size(usize)
2010-10-16 09:45:36 +00:00
return self.exit_code_from_logger()
2010-04-18 20:34:21 +00:00
def do_keychain_generate(self, args):
2010-10-24 20:00:46 +00:00
return KeyChain.generate(args.path)
def do_keychain_restrict(self, args):
return KeyChain(args.input).restrict(args.output)
2010-10-24 20:13:34 +00:00
def do_keychain_chpass(self, args):
return KeyChain(args.keychain).chpass()
2010-10-16 09:45:36 +00:00
def run(self, args=None):
2010-10-15 18:35:49 +00:00
parser = argparse.ArgumentParser(description='Dedupestore')
parser.add_argument('-k', '--key-chain', dest='keychain', type=str,
help='Key chain')
2010-10-15 18:35:49 +00:00
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
default=False,
help='Verbose output')
2010-10-15 18:35:49 +00:00
subparsers = parser.add_subparsers(title='Available subcommands')
subparser = subparsers.add_parser('keychain')
subsubparsers = subparser.add_subparsers(title='Available subcommands')
subparser = subsubparsers.add_parser('generate')
subparser.add_argument('path', metavar='PATH', type=str,
help='Path to keychain')
subparser.set_defaults(func=self.do_keychain_generate)
2010-10-24 20:00:46 +00:00
subparser = subsubparsers.add_parser('restrict')
subparser.add_argument('input', metavar='INPUT', type=str,
help='Existing keychain')
subparser.add_argument('output', metavar='OUTPUT', type=str,
help='Keychain to create')
subparser.set_defaults(func=self.do_keychain_restrict)
2010-10-24 20:13:34 +00:00
subparser = subsubparsers.add_parser('chpass')
subparser.add_argument('keychain', metavar='KEYCHAIN', type=str,
help='Path to keychain')
subparser.set_defaults(func=self.do_keychain_chpass)
2010-10-15 18:35:49 +00:00
subparser = subparsers.add_parser('create')
subparser.set_defaults(func=self.do_create)
subparser.add_argument('archive', metavar='ARCHIVE',
type=location_validator(archive=True),
help='Archive to create')
subparser.add_argument('paths', metavar='PATH', nargs='+', type=str,
help='Paths to add to archive')
subparser = subparsers.add_parser('extract')
subparser.set_defaults(func=self.do_extract)
subparser.add_argument('archive', metavar='ARCHIVE',
type=location_validator(archive=True),
help='Archive to create')
subparser.add_argument('dest', metavar='DEST', type=str, nargs='?',
help='Where to extract files')
subparser = subparsers.add_parser('delete')
subparser.set_defaults(func=self.do_delete)
subparser.add_argument('archive', metavar='ARCHIVE',
type=location_validator(archive=True),
help='Archive to delete')
subparser = subparsers.add_parser('list')
subparser.set_defaults(func=self.do_list)
subparser.add_argument('src', metavar='SRC', type=location_validator(),
help='Store/Archive to list contents of')
subparser= subparsers.add_parser('verify')
subparser.set_defaults(func=self.do_verify)
subparser.add_argument('archive', metavar='ARCHIVE',
type=location_validator(archive=True),
help='Archive to verity integrity of')
subparser= subparsers.add_parser('info')
subparser.set_defaults(func=self.do_info)
subparser.add_argument('archive', metavar='ARCHIVE',
type=location_validator(archive=True),
help='Archive to display information about')
2010-10-16 09:45:36 +00:00
args = parser.parse_args(args)
2010-10-15 18:35:49 +00:00
if args.verbose:
2010-10-13 21:28:40 +00:00
logging.basicConfig(level=logging.INFO, format='%(message)s')
else:
logging.basicConfig(level=logging.WARNING, format='%(message)s')
2010-10-16 09:45:36 +00:00
self.level_filter = LevelFilter()
logging.getLogger('').addFilter(self.level_filter)
return args.func(args)
2010-03-06 17:25:35 +00:00
2010-02-20 17:23:46 +00:00
def main():
archiver = Archiver()
2010-10-16 09:45:36 +00:00
sys.exit(archiver.run())
2010-02-20 17:23:46 +00:00
if __name__ == '__main__':
2010-03-06 17:25:35 +00:00
main()