Last active
October 19, 2015 09:12
-
-
Save matachi/f52257bfee070023b134 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import csv | |
from subprocess import Popen, PIPE | |
def get_file_name(base_name, depth=0): | |
name = '%s %d' % (base_name, depth) if depth > 0 else base_name | |
if os.path.exists(os.path.join( | |
os.path.expanduser('~'), '.password-store', '%s.gpg' % name | |
)): | |
return get_file_name(base_name, depth + 1) | |
else: | |
return name | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('path', metavar='PATH', type=str, | |
help='Path to the LastPass .csv file.') | |
args = parser.parse_args() | |
with open(args.path) as f: | |
rows = list(csv.reader(f))[1:] | |
for row in rows: | |
name = row[4] | |
file_name = get_file_name(name) | |
content = [] | |
if row[2] != '': | |
content.append(row[2]) # password | |
if row[1] != '': | |
content.append('username: {}'.format(row[1])) | |
if row[0] != '': | |
content.append('url: {}'.format(row[0])) | |
if row[3] != '': | |
content.append(row[3]) # extra | |
content.append('') # an extra newline character at the end | |
p = Popen(['pass', 'insert', '-m', file_name], stdin=PIPE, stdout=PIPE) | |
p.communicate(bytes('\n'.join(content), 'utf-8')) | |
p.stdin.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment