borg/darc/archiver.py

182 lines
7.4 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-25 18:22:20 +00:00
import os
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-26 19:25:25 +00:00
from .store import Store
2010-10-20 17:59:15 +00:00
from .cache import Cache
from .crypto import CryptoManager, KeyChain
2010-10-27 17:30:21 +00:00
from .helpers import location_validator, format_file_size, LevelFilter
class Archiver(object):
2010-10-15 18:35:49 +00:00
def open_store(self, location):
2010-10-26 19:25:25 +00:00
return Store(location.path)
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-26 19:48:43 +00:00
def do_init(self, args):
Store(args.store.path, create=True)
return self.exit_code_from_logger()
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):
print archive.metadata['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)
2010-10-25 17:57:54 +00:00
print 'Name:', archive.metadata['name']
print 'Hostname:', archive.metadata['hostname']
print 'Username:', archive.metadata['username']
print 'Time:', archive.metadata['time']
print 'Command line:', ' '.join(archive.metadata['cmdline'])
print 'Number of Files:', len(archive.items)
2010-10-27 17:30:21 +00:00
print 'Original size:', format_file_size(osize)
print 'Compressed size:', format_file_size(csize)
print 'Unique data:', format_file_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-25 18:22:20 +00:00
return KeyChain.generate(args.keychain)
2010-10-24 20:00:46 +00:00
def do_keychain_restrict(self, args):
2010-10-25 18:22:20 +00:00
return KeyChain(args.keychain).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-25 18:22:20 +00:00
default_keychain = os.path.join(os.path.expanduser('~'),
2010-10-27 18:12:40 +00:00
'.darc', 'keychain')
parser = argparse.ArgumentParser(description='DARC - Deduplicating Archiver')
2010-10-25 18:22:20 +00:00
parser.add_argument('-k', '--keychain', dest='keychain', type=str,
default=default_keychain,
help='Keychain to use')
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.set_defaults(func=self.do_keychain_generate)
2010-10-24 20:00:46 +00:00
subparser = subsubparsers.add_parser('restrict')
subparser.add_argument('output', metavar='OUTPUT', type=str,
help='Keychain to create')
subparser.set_defaults(func=self.do_keychain_restrict)
2010-10-25 18:22:20 +00:00
subparser = subsubparsers.add_parser('change-password')
2010-10-24 20:13:34 +00:00
subparser.set_defaults(func=self.do_keychain_chpass)
2010-10-26 19:48:43 +00:00
subparser = subparsers.add_parser('init')
subparser.set_defaults(func=self.do_init)
subparser.add_argument('store', metavar='STORE',
type=location_validator(archive=False),
help='Store to initialize')
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()