all repos — dotfiles @ 5f72ff8ac2cfd2918572b49a9cd9fa904ea78047

linux dotfiles

update scripts/pd

add edit functionality
prithugoswami prithugoswami524@gmail.com
Mon, 04 Jun 2018 14:40:35 +0530
commit

5f72ff8ac2cfd2918572b49a9cd9fa904ea78047

parent

d32c7c4aa6895d650971bdc0efc040e32cbbe43d

1 files changed, 77 insertions(+), 35 deletions(-)

jump to
M scripts/pdscripts/pd

@@ -58,8 +58,12 @@ import datetime

import tempfile import subprocess import shlex +import sys +import hashlib +## VARIABLES ## +# Change these accordingly env_home = os.environ['HOME'] tmp_pd_path = '/tmp/pd' key_path = env_home + '/.pdkey'

@@ -67,35 +71,91 @@ pd_dir = env_home + '/Dropbox/pd'

pd_path = pd_dir + '/pd' rclone_dir = 'drop:/pd' + if not os.path.exists(key_path): print("Exiting..No .pdkey found in home dir") exit() if os.path.exists(tmp_pd_path): os.remove(tmp_pd_path) + +def fetch_and_decrypt(): + print("Fetching changes...") + if not os.system('rclone sync {}/ {}/'.format(rclone_dir, pd_dir)): + print("Done") + decrypt_cmd = ('gpg --passphrase-file {} --batch -o {} -d {}' + .format(key_path, tmp_pd_path, pd_path)) + args = shlex.split(decrypt_cmd) + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True) + out, err = p.communicate() + if p.returncode: # if gpg exits with a non-zero return code + print("Error while decrypting :\n" + err) + exit() + else: + print("Something went wrong") + + +def encrypt_and_push(): + if os.path.exists(pd_path): + os.remove(pd_path) + encrypt_cmd = ('gpg --passphrase-file {} --batch -o {} -c {}' + .format(key_path, pd_path, tmp_pd_path)) + args = shlex.split(encrypt_cmd) + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True) + out, err = p.communicate() + if p.returncode: + print("Error while encrypting :\n" + err) + exit() + + os.remove(tmp_pd_path) + print("Pushing changes...") + if not os.system('rclone sync {}/ {}/'.format(pd_dir, rclone_dir)): + print("Done") + else: + print("Something went wrong") + + + +# PD edit argument +# with the 'edit' argument passed to the script it will fetch the latest pd +# version from the clound and let you edit and then push it back + +if len(sys.argv) > 1: + if sys.argv[1] == 'edit': + if os.path.exists(tmp_pd_path): + os.remove(tmp_pd_path) + fetch_and_decrypt() + + with open(tmp_pd_path, 'r') as pdtemp: + hash_before_edit = (hashlib.sha1(pdtemp.read().encode('utf-8')) + .hexdigest()) + + os.system("vim {}".format(tmp_pd_path)) + + with open(tmp_pd_path, 'r') as pdtemp: + hash_after_edit = (hashlib.sha1(pdtemp.read().encode('utf-8')) + .hexdigest()) + + if hash_before_edit == hash_after_edit: + print("Nothing was changed. Exiting...") + exit() + encrypt_and_push() + exit() + + + entry = None with tempfile.NamedTemporaryFile(suffix='.pdentry') as temp: command = "vim {}".format(temp.name) os.system(command) entry = open(temp.name, 'r').read() - if entry == '': + if len(entry.strip()) == 0: print("Nothing was entered...") exit() - -print("Fetching changes...") -if not os.system('rclone sync {}/ {}/'.format(rclone_dir, pd_dir)): - print("Done") - decrypt_cmd = ('gpg --passphrase-file {} --batch -o {} -d {}' - .format(key_path, tmp_pd_path, pd_path)) - args = shlex.split(decrypt_cmd) - p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - out, err = p.communicate() - if p.returncode: # if gpg exits with a non-zero return code - print("Error while decrypting :\n" + err) - exit() -else: - print("Something went wrong") + +fetch_and_decrypt() with open(tmp_pd_path, 'a', encoding='utf8') as fp: dt = datetime.datetime.now()

@@ -106,22 +166,4 @@

fp.write('\n\n\n===============================\n' + date_and_time + '\n') fp.write(entry) - -if os.path.exists(pd_path): - os.remove(pd_path) -encrypt_cmd = ('gpg --passphrase-file {} --batch -o {} -c {}' - .format(key_path, pd_path, tmp_pd_path)) -args = shlex.split(encrypt_cmd) -p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) -out, err = p.communicate() -if p.returncode: - print("Error while encrypting :\n" + err) - exit() - -os.remove(tmp_pd_path) -print("Pushing changes...") -if not os.system('rclone sync {}/ {}/'.format(pd_dir, rclone_dir)): - print("Done") -else: - print("Something went wrong") +encrypt_and_push()