kedpm2pass.py (1568B)
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # 4 # Copyright (C) 2012 Antoine Beaupré <anarcat@orangeseeds.org>. All Rights Reserved. 5 # This file is licensed under the GPLv2+. Please see COPYING for more information. 6 # 7 # To double-check your import worked: 8 # grep Path passwords | sed 's#^Path: ##;s/$/.gpg/' | sort > listpaths 9 # (cd ~/.password-store/ ; find -type f ) | sort | diff -u - listpaths 10 11 import re 12 import fileinput 13 14 import sys # for exit 15 16 import subprocess 17 18 def insert(d): 19 path = d['Path'] 20 del d['Path'] 21 print "inserting " + path 22 content = d['Password'] + "\n" 23 del d['Password'] 24 for k, v in d.iteritems(): 25 content += "%s: %s\n" % (k, v) 26 del d 27 cmd = ["pass", "insert", "--force", "--multiline", path] 28 process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 29 stdout, stderr = process.communicate(content) 30 retcode = process.wait() 31 if retcode: 32 print 'command "%s" failed with exit code %d: %s' % (" ".join(cmd), retcode, stdout + stderr) 33 sys.exit(1); 34 35 d = None 36 for line in fileinput.input(): 37 if line == "\n": 38 continue 39 match = re.match("(\w+): (.*)$", line) 40 if match: 41 if match.group(1) == 'Path': 42 if d is not None: 43 insert(d) 44 else: 45 d = {} 46 d[match.group(1)] = match.group(2) 47 #print "found field: %s => %s" % (match.group(1), match.group(2)) 48 else: 49 print "warning: no match found on line: *%s*" % line 50 51 if d is not None: 52 insert(d)