From e650e195e19bf9a3b1063c9f2ea0fe6e4129d01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Tue, 26 Oct 2010 21:48:43 +0200 Subject: [PATCH] Added "init" subcommand --- dedupestore/archiver.py | 10 ++++++++++ dedupestore/store.py | 30 ++++++++++++++++-------------- dedupestore/test.py | 1 + 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/dedupestore/archiver.py b/dedupestore/archiver.py index 0980b15a9..9d3607c23 100644 --- a/dedupestore/archiver.py +++ b/dedupestore/archiver.py @@ -18,6 +18,10 @@ def open_store(self, location): def exit_code_from_logger(self): return 1 if self.level_filter.count.get('ERROR') else 0 + def do_init(self, args): + Store(args.store.path, create=True) + return self.exit_code_from_logger() + def do_create(self, args): store = self.open_store(args.archive) keychain = KeyChain(args.keychain) @@ -115,6 +119,12 @@ def run(self, args=None): subparser = subsubparsers.add_parser('change-password') subparser.set_defaults(func=self.do_keychain_chpass) + 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') + subparser = subparsers.add_parser('create') subparser.set_defaults(func=self.do_create) subparser.add_argument('archive', metavar='ARCHIVE', diff --git a/dedupestore/store.py b/dedupestore/store.py index eb78dc38b..8cec38626 100644 --- a/dedupestore/store.py +++ b/dedupestore/store.py @@ -23,16 +23,24 @@ class AlreadyExists(KeyError): ACTIVE = 'Active' BAND_LIMIT = 1 * 1024 * 1024 - def __init__(self, path): + def __init__(self, path, create=False): self.read_fd = None self.write_fd = None - if not os.path.exists(path): + if create: self.create(path) self.open(path) + def get_option(self, key): + return self.cursor.execute('SELECT value FROM system WHERE key=?', (key,)) \ + .fetchone()[0] + + def set_option(self, key, value): + return self.cursor.execute('UPDATE system SET value=? WHERE key=?', + (value, key)) + def open(self, path): if not os.path.isdir(path): - raise Exception('%s Does not look like a store') + raise Exception('%s Does not look like a store' % path) db_path = os.path.join(path, 'dedupestore.db') if not os.path.exists(db_path): raise Exception('%s Does not look like a store2') @@ -44,15 +52,6 @@ def open(self, path): self.cursor = self.cnx.cursor() self._begin() - def get_option(self, key): - return self.cursor.execute('SELECT value FROM system WHERE key=?', (key,)) \ - .fetchone()[0] - - def set_option(self, key, value): - return self.cursor.execute('UPDATE system SET value=? WHERE key=?', - (value, key)) - - def _begin(self): if self.read_fd: self.read_fd.close() @@ -76,7 +75,10 @@ def _begin(self): band += 1 def create(self, path): - os.mkdir(path) + if os.path.exists(path) and (not os.path.isdir(path) or os.listdir(path)): + raise Exception('Path "%s" already exists' % path) + if not os.path.exists(path): + os.mkdir(path) os.mkdir(os.path.join(path, 'bands')) cnx = sqlite3.connect(os.path.join(path, 'dedupestore.db')) cnx.execute('CREATE TABLE objects(ns BINARY NOT NULL, id BINARY NOT NULL, ' @@ -212,7 +214,7 @@ class StoreTestCase(unittest.TestCase): def setUp(self): self.tmppath = tempfile.mkdtemp() - self.store = Store(os.path.join(self.tmppath, 'store')) + self.store = Store(os.path.join(self.tmppath, 'store'), create=True) def tearDown(self): shutil.rmtree(self.tmppath) diff --git a/dedupestore/test.py b/dedupestore/test.py index 030885bb9..a2e1f25bb 100644 --- a/dedupestore/test.py +++ b/dedupestore/test.py @@ -16,6 +16,7 @@ def setUp(self): self.keychain = '/tmp/_test_dedupstore.keychain' if not os.path.exists(self.keychain): self.dedupestore('keychain', 'generate') + self.dedupestore('init', self.store_path) def tearDown(self): shutil.rmtree(self.tmpdir)