Ver Fonte

More user friendly key file handling

Jonas Borgström há 14 anos atrás
pai
commit
64a318501d
3 ficheiros alterados com 29 adições e 14 exclusões
  1. 1 1
      darc/archiver.py
  2. 6 0
      darc/helpers.py
  3. 22 13
      darc/key.py

+ 1 - 1
darc/archiver.py

@@ -46,7 +46,7 @@ class Archiver(object):
 
     def do_init(self, args):
         store = self.open_store(args.store, create=True)
-        key = Key.create(store)
+        key = Key.create(store, args.store.to_key_filename())
 
     def do_create(self, args):
         store = self.open_store(args.archive)

+ 6 - 0
darc/helpers.py

@@ -284,6 +284,12 @@ class Location(object):
         items.append('archive=%r' % self.archive)
         return ', '.join(items)
 
+    def to_key_filename(self):
+        name = re.sub('[^\w]', '_', self.path).strip('_')
+        if self.proto != 'file':
+            name = self.host + '__' + name
+        return os.path.join(os.path.expanduser('~'), '.darc', 'keys', name)
+
     def __repr__(self):
         return "Location(%s)" % self
 

+ 22 - 13
darc/key.py

@@ -20,12 +20,21 @@ class Key(object):
 
     def __init__(self, store=None):
         if store:
-            self.open(store)
-
-    def open(self, store):
-        path = os.path.join(os.path.expanduser('~'),
-                            '.darc', 'keys', store.id.encode('hex'))
-        with open(path, 'rb') as fd:
+            self.open(self.find_key_file(store))
+
+    def find_key_file(self, store):
+        id = store.id.encode('hex')
+        keys_dir = os.path.join(os.path.expanduser('~'), '.darc', 'keys')
+        for name in os.listdir(keys_dir):
+            filename = os.path.join(keys_dir, name)
+            with open(filename, 'rb') as fd:
+                line = fd.readline().strip()
+                if line and line.startswith(self.FILE_ID) and line[9:] == id:
+                    return filename
+        raise Exception('Key file for store with ID %s not found' % id)
+
+    def open(self, filename):
+        with open(filename, 'rb') as fd:
             lines = fd.readlines()
             if not lines[0].startswith(self.FILE_ID) != self.FILE_ID:
                 raise ValueError('Not a DARC key file')
@@ -90,7 +99,7 @@ class Key(object):
         with open(path, 'wb') as fd:
             fd.write('%s %s\n' % (self.FILE_ID, self.store_id.encode('hex')))
             fd.write(data.encode('base64'))
-            print 'Key chain "%s" created' % path
+            print 'Key file "%s" created' % path
 
     def chpass(self):
         password, password2 = 1, 2
@@ -103,12 +112,12 @@ class Key(object):
         return 0
 
     @staticmethod
-    def create(store):
-        path = os.path.join(os.path.expanduser('~'),
-                            '.darc', 'keys', store.id.encode('hex'))
-        if os.path.exists(path):
-            print '%s already exists' % path
-            return 1
+    def create(store, filename):
+        i = 1
+        path = filename
+        while os.path.exists(path):
+            i += 1
+            path = filename + '.%d' % i
         password, password2 = 1, 2
         while password != password2:
             password = getpass('Keychain password: ')