aboutsummaryrefslogtreecommitdiffstats
path: root/pass2csv.py
diff options
context:
space:
mode:
authorRasmus Steinke <rasi@xssn.at>2017-07-21 14:22:32 +0200
committerRasmus Steinke <rasi@xssn.at>2017-07-21 14:22:32 +0200
commitec523d74d98807f8b58c59a5a99d88ec9789efb6 (patch)
treea21343f57f56b29cc1b4d0418981e1b0377c650c /pass2csv.py
parentb6c31a80e81a6652626ca19825b91655f7f257aa (diff)
downloadrofi-pass-ec523d74d98807f8b58c59a5a99d88ec9789efb6.tar.gz
added csv exporter
Diffstat (limited to 'pass2csv.py')
-rw-r--r--pass2csv.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/pass2csv.py b/pass2csv.py
new file mode 100644
index 0000000..16f8a8e
--- /dev/null
+++ b/pass2csv.py
@@ -0,0 +1,75 @@
+import csv
+import os
+import sys
+import gnupg
+
+## Usage
+# Run pass2csv /path/to/password-storage
+
+## options
+# field names
+userfield = "user"
+urlfield = "url"
+
+def traverse(path):
+ for root, dirs, files in os.walk(path):
+ if '.git' in dirs:
+ dirs.remove('.git')
+ for name in files:
+ yield os.path.join(root, name)
+
+
+def parse(basepath, path, data):
+ name = os.path.splitext(os.path.basename(path))[0]
+ group = os.path.dirname(os.path.os.path.relpath(path, basepath))
+ split_data = data.split('\n')
+ password = split_data[0]
+ matching_user = [s for s in split_data if userfield+": " in s]
+ user = None
+ url = None
+ if matching_user:
+ for x in matching_user:
+ user_split = x.split(userfield+": ")
+ if len(user_split) == 2:
+ user = user_split[1]
+ else:
+ user = None
+ matching_url = [s for s in split_data if urlfield+": " in s]
+ if matching_url:
+ for x in matching_url:
+ url_split = x.split(urlfield+": ")
+ if len(url_split) == 2:
+ url = url_split[1]
+ else:
+ url = None
+ if url == "None":
+ if user == "None":
+ return [group, name, password]
+ else:
+ return [group, name, password, user]
+ else:
+ if user == "None":
+ return [group, name, password, url]
+ else:
+ return [group, name, password, url, user]
+
+
+
+def main(path):
+ gpg = gnupg.GPG()
+ gpg.encoding = 'utf-8'
+ csv_data = []
+ for file_path in traverse(path):
+ if os.path.splitext(file_path)[1] == '.gpg':
+ with open(file_path, 'rb') as f:
+ data = str(gpg.decrypt_file(f))
+ csv_data.append(parse(path, file_path, data))
+
+ with open('pass.csv', 'w', newline='') as csv_file:
+ writer = csv.writer(csv_file, delimiter=',')
+ writer.writerows(csv_data)
+
+
+if __name__ == '__main__':
+ path = os.path.abspath(sys.argv[1])
+ main(path)