Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Copyright 2015 David Francoeur <dfrancoeur04@gmail.com>
  4. # This file is licensed under the GPLv2+. Please see COPYING for more information.
  5.  
  6. # KeePassX 2+ on Mac allows export to CSV. The CSV contains the following headers :
  7. # "Group","Title","Username","Password","URL","Notes"
  8. # Group and Title are used to build the path, @see prepareForInsertion
  9. # Password is the first line and the url and the notes are appended after
  10. #
  11. # Usage: ./csv_to_pass.py test.csv
  12.  
  13. import csv
  14. import itertools
  15. import sys
  16. from subprocess import Popen, PIPE
  17.  
  18. def pass_import_entry(path, data):
  19. """ Import new password entry to password-store using pass insert command """
  20. proc = Popen(['pass', 'insert', '--multiline', path], stdin=PIPE, stdout=PIPE)
  21. proc.communicate(data.encode('utf8'))
  22. proc.wait()
  23.  
  24. def readFile(filename):
  25. """ Read the file and proccess each entry """
  26. with open(filename, 'rU') as csvIN:
  27. next(csvIN)
  28. outCSV=(line for line in csv.reader(csvIN, dialect='excel'))
  29. for row in itertools.islice(outCSV, 0, 1):
  30. # for row in outCSV:
  31. #print("Length: ", len(row), row)
  32. prepareForInsertion(row)
  33.  
  34.  
  35. def prepareForInsertion(row):
  36. """ prepare each CSV entry into an insertable string """
  37. keyFolder = escape(row[0][4:])
  38. keyName = escape(row[1])
  39. username = row[2]
  40. password = row[3]
  41. url = row[4]
  42. notes = row[5]
  43.  
  44. path = keyFolder+"/"+keyName+"/"+username
  45. data = password + "\n" if password else "\n"
  46. data = "%s%s: %s\n" % (data, "url:", url+"\n")
  47. data = "%s%s: %s\n" % (data, "notes:", notes+"\n")
  48. pass_import_entry(path,data)
  49. print(path+" imported!")
  50.  
  51. def escape(strToEscape):
  52. """ escape the list """
  53. return strToEscape.replace(" ", "-").replace("&","and").replace("[","").replace("]","")
  54.  
  55.  
  56. def main(argv):
  57. inputFile = sys.argv[1]
  58. print("File to read: " + inputFile)
  59. readFile(inputFile)
  60.  
  61.  
  62. main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement