Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. import csv
  2. import os
  3. import sys
  4. import gnupg
  5.  
  6.  
  7. def traverse(path):
  8.     for root, dirs, files in os.walk(path):
  9.         if '.git' in dirs:
  10.             dirs.remove('.git')
  11.         for name in files:
  12.             yield os.path.join(root, name)
  13.  
  14.  
  15. def parse(basepath, path, data):
  16.     name = os.path.splitext(os.path.basename(path))[0]
  17.     group = os.path.dirname(os.path.os.path.relpath(path, basepath))
  18.     split_data = data.split('\n', maxsplit=6)
  19.     password = split_data[0]
  20.     notes = split_data[1].replace('---', '')
  21.     try:
  22.         username = split_data[3].replace('username: ', '')
  23.     except IndexError:
  24.         username = ''
  25.  
  26.     try:
  27.         url = split_data[5].replace('url: ', '')
  28.     except IndexError:
  29.         url = ""
  30.  
  31.     if notes[:7] == 'login: ':
  32.         username = notes.replace('login: ', '')
  33.  
  34.     return [group, name, password, username, url, notes]
  35.  
  36.  
  37. def main(path):
  38.     gpg = gnupg.GPG()
  39.     gpg.encoding = 'utf-8'
  40.     csv_data = []
  41.     for file_path in traverse(path):
  42.         if os.path.splitext(file_path)[1] == '.gpg':
  43.             with open(file_path, 'rb') as f:
  44.                 data = str(gpg.decrypt_file(f))
  45.                 csv_data.append(parse(path, file_path, data))
  46.  
  47.     with open('pass.csv', 'w', newline='') as csv_file:
  48.         writer = csv.writer(csv_file, delimiter=',')
  49.         writer.writerows(csv_data)
  50.  
  51.  
  52. if __name__ == '__main__':
  53.     path = os.path.abspath(sys.argv[1])
  54.     main(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement