From 56db2e65f8476bddf4e9ee4c553a9f524f1c493c Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Mon, 5 Dec 2022 12:13:28 -0500 Subject: [PATCH] Moved all syncing functionality into sshync.py, removed sshypRemote.py Former-commit-id: bf7833a21db35a7d33fdd89ee8a41775fd2f55c0 [formerly 56ea8d0cf98bb65641cb1147ea96413ce85e3f18] Former-commit-id: 820f85f9b0efc8a33ebf8a33f751e8f130fc8c34 --- lib/sshync.py | 74 +++++++++++++++++++++++++++++++++++++++++----- lib/sshyp.py | 35 +++++----------------- lib/sshypRemote.py | 35 ---------------------- 3 files changed, 75 insertions(+), 69 deletions(-) delete mode 100755 lib/sshypRemote.py diff --git a/lib/sshync.py b/lib/sshync.py index 3a5290a..cce2c6a 100755 --- a/lib/sshync.py +++ b/lib/sshync.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 -from os import walk -from os.path import getmtime, join +from os import listdir, remove, walk +from os.path import expanduser, getmtime, join +from pathlib import Path from subprocess import PIPE, run from sys import exit as s_exit @@ -21,8 +22,8 @@ def get_local_data(_directory, _device): # retrieves titles and mod times from def get_remote_data(_user_data): # retrieves titles and mod times from the remote server _titles_mods = run(['ssh', '-i', _user_data[5], '-p', _user_data[2], f"{_user_data[0]}@{_user_data[1]}", - f'cd /lib/sshyp; python3 -c \'import sshync; sshync.get_local_data("{_user_data[4]}", ' - f'"server")\''], stdout=PIPE, text=True).stdout.split('\n') + f'cd /lib/sshyp; python3 -c \'from sshync import get_local_data; get_local_data' + f'("{_user_data[4]}", "server")\''], stdout=PIPE, text=True).stdout.split('\n') _title_list = _titles_mods[:len(_titles_mods)//2] _mod_list = _titles_mods[len(_titles_mods)//2:] return _title_list, _mod_list @@ -44,6 +45,60 @@ def sort_titles_mods(_list_1, _list_2): # creates and returns two lists (of tit return _title_list_2_sorted, _mod_list_2_sorted +def delete(_file_path, _target_database): # deletes an entry or folder + from shutil import rmtree + try: + if _file_path.endswith('/'): + rmtree(f"{expanduser('~/.local/share/sshyp/')}{_file_path}") + else: + remove(f"{expanduser('~/.local/share/sshyp/')}{_file_path}.gpg") + except FileNotFoundError: + print(f"location does not exist {_target_database}") + if _target_database == 'remotely': + for _device_name in listdir(expanduser('~/.config/sshyp/devices')): + open(expanduser('~/.config/sshyp/deleted/') + _file_path.replace('/', '\x1e') + '\x1f' + _device_name, 'w') + + +def deletion_check(_client_device_name): # creates a list of entries and folders to be deleted from a client device + for _file in listdir(expanduser('~/.config/sshyp/deleted')): + _file_path, _sep, _device = _file.partition('\x1f') + _file_path = _file_path.replace('\x1e', '/') + if _device == _client_device_name: + print(_file_path) + try: + remove(f"{expanduser('~/.config/sshyp/deleted/')}{_file}") + except FileNotFoundError: + pass + + +def deletion_sync(_user_data, _silent): # check for deletions + _deletion_database = run(['ssh', '-i', _user_data[5], '-p', _user_data[2], f"{_user_data[0]}@{_user_data[1]}", + f'cd /lib/sshyp; python3 -c \'from sshync import deletion_check; deletion_check' + f'("{_user_data[6]}")\''], stdout=PIPE, text=True).stdout.split('\n') + for _file in _deletion_database: + if _file != '': + if _silent != 1: + print(f"\u001b[38;5;208m{_file}\u001b[0m has been sheared, removing...") + delete(_file, 'locally') + + +def folder_check(): # creates a list of folders to be compared with those of a client device + for _root, _directories, _files in walk(expanduser('~/.local/share/sshyp')): + for _dir in _directories: + print(f"{_root.replace(expanduser('~'), '')}/{_dir}") + + +def folder_sync(_user_data): # check for new folders + _folder_database = run(['ssh', '-i', _user_data[5], '-p', _user_data[2], f"{_user_data[0]}@{_user_data[1]}", + "cd /lib/sshyp; python3 -c 'from sshync import folder_check; folder_check()'"], + stdout=PIPE, text=True).stdout.split('\n') + for _folder in _folder_database: + if _folder != '' and not Path(f"{expanduser('~')}{_folder}").is_dir(): + print(f"\u001b[38;5;2m{_folder.replace('/.local/share/sshyp/', '')}/\u001b[0m does not exist locally, " + f"creating...") + Path(f"{expanduser('~')}{_folder}").mkdir(mode=0o700, parents=True, exist_ok=True) + + def make_profile(_profile_dir, _local_dir, _remote_dir, _identity, _ip, _port, _user): # creates a sshync job profile open(_profile_dir, 'w').write(f"{_user}\n{_ip}\n{_port}\n{_local_dir}\n{_remote_dir}\n{_identity}\n") @@ -62,11 +117,16 @@ def get_profile(_profile_dir): # returns a list of data read from a sshync job _local_dir = _profile_data[3].rstrip() _remote_dir = _profile_data[4].rstrip() _identity = _profile_data[5].rstrip() - return _user, _ip, _port, _local_dir, _remote_dir, _identity + _client_device_id = listdir(expanduser('~/.config/sshyp/devices'))[0].rstrip() + return _user, _ip, _port, _local_dir, _remote_dir, _identity, _client_device_id -def run_profile(_profile_dir): # runs a sshync job profile - _user_data = get_profile(_profile_dir) +def run_profile(_profile_dir, _silent): # runs a sshync job profile + _user_data = get_profile(_profile_dir) # import profile data + deletion_sync(_user_data, _silent) # check for and sync deletions + folder_sync(_user_data) # check for and sync folders + + # sync new and updated entries _remote_titles_mods = get_remote_data(_user_data) _index_l = sort_titles_mods(_remote_titles_mods, get_local_data(_user_data[3], 'client')) _index_r = sort_titles_mods(_index_l, _remote_titles_mods) diff --git a/lib/sshyp.py b/lib/sshyp.py index d7348cb..2028e93 100755 --- a/lib/sshyp.py +++ b/lib/sshyp.py @@ -4,8 +4,7 @@ from os.path import expanduser from pathlib import Path from random import randint from shutil import get_terminal_size, move, rmtree -import sshync -from sshypRemote import delete as offline_delete +from sshync import delete as offline_delete, run_profile, make_profile, get_profile from subprocess import CalledProcessError, DEVNULL, PIPE, run from sys import argv, exit as s_exit @@ -307,9 +306,9 @@ def tweak(): # runs configuration wizard _username_ssh = str(input('\nusername of the remote server: ')) # sshync profile generation - sshync.make_profile(expanduser('~/.config/sshyp/sshyp.sshync'), - expanduser('~/.local/share/sshyp/'), f"/home/{_username_ssh}/.local/share/sshyp/", - expanduser('~/.ssh/sshyp'), _ip, _port, _username_ssh) + make_profile(expanduser('~/.config/sshyp/sshyp.sshync'), + expanduser('~/.local/share/sshyp/'), f"/home/{_username_ssh}/.local/share/sshyp/", + expanduser('~/.ssh/sshyp'), _ip, _port, _username_ssh) # device id configuration for _id in listdir(expanduser('~/.config/sshyp/devices')): # remove existing device id @@ -487,28 +486,10 @@ def read_shortcut(): # shortcut to quickly read an entry def sync(): # calls sshync to sync changes to the user's server print('\nsyncing entries with the server device...\n') - # check for deletions - _deletion_database = run(['ssh', '-i', expanduser('~/.ssh/sshyp'), '-p', port, f"{username_ssh}@{ip}", - f'cd /lib/sshyp; python3 -c \'import sshypRemote; sshypRemote.deletion_check("' - f'{client_device_id}")\''], stdout=PIPE, text=True).stdout.split('\n') - for _file in _deletion_database: - if _file != '': - if silent_sync != 1: - print(f"\u001b[38;5;208m{_file}\u001b[0m has been sheared, removing...") - offline_delete(_file, 'locally') - # check for new folders - _folder_database = run(['ssh', '-i', expanduser('~/.ssh/sshyp'), '-p', port, f"{username_ssh}@{ip}", - "cd /lib/sshyp; python3 -c 'import sshypRemote; sshypRemote.folder_check()'"], - stdout=PIPE, text=True).stdout.split('\n') - for _folder in _folder_database: - if _folder != '' and not Path(f"{expanduser('~')}{_folder}").is_dir(): - print(f"\u001b[38;5;2m{_folder.replace('/.local/share/sshyp/', '')}/\u001b[0m does not exist locally, " - f"creating...") - Path(f"{expanduser('~')}{_folder}").mkdir(mode=0o700, parents=True, exist_ok=True) # set permissions before uploading run(['find', directory, '-type', 'd', '-exec', 'chmod', '-R', '700', '{}', '+']) run(['find', directory, '-type', 'f', '-exec', 'chmod', '-R', '600', '{}', '+']) - sshync.run_profile(expanduser('~/.config/sshyp/sshyp.sshync')) + run_profile(expanduser('~/.config/sshyp/sshyp.sshync'), silent_sync) def whitelist_setup(): # takes input from the user to set up quick-unlock password @@ -684,7 +665,7 @@ def rename(): # renames an entry or folder move(f"{directory}{_entry_name}.gpg", f"{directory}{_new_name}.gpg") if ssh_error != 1: run(['ssh', '-i', expanduser('~/.ssh/sshyp'), '-p', port, f"{username_ssh}@{ip}", - f'cd /lib/sshyp; python3 -c \'import sshypRemote; sshypRemote.delete("{_entry_name}", "remotely")\'']) + f'cd /lib/sshyp; python3 -c \'from sshync import delete; delete("{_entry_name}", "remotely")\'']) def edit(): # edits the contents of an entry @@ -798,7 +779,7 @@ def remove_data(): # deletes an entry from the server and flags it for local de determine_decrypt(expanduser('~/.config/sshyp/lock.gpg'), 0, 0) if ssh_error != 1: run(['ssh', '-i', expanduser('~/.ssh/sshyp'), '-p', port, f"{username_ssh}@{ip}", - f'cd /lib/sshyp; python3 -c \'import sshypRemote; sshypRemote.delete("{_entry_name}", "remotely")\'']) + f'cd /lib/sshyp; python3 -c \'from sshync import delete; delete("{_entry_name}", "remotely")\'']) else: offline_delete(_entry_name, 'locally') @@ -823,7 +804,7 @@ if __name__ == "__main__": editor = sshyp_data[2].rstrip() quick_unlock_enabled = sshyp_data[3].rstrip() if Path(expanduser('~/.config/sshyp/sshyp.sshync')).is_file(): - ssh_info = sshync.get_profile(expanduser('~/.config/sshyp/sshyp.sshync')) + ssh_info = get_profile(expanduser('~/.config/sshyp/sshyp.sshync')) username_ssh = ssh_info[0].rstrip() ip = ssh_info[1].rstrip() port = ssh_info[2].rstrip() diff --git a/lib/sshypRemote.py b/lib/sshypRemote.py deleted file mode 100755 index f7dbb6c..0000000 --- a/lib/sshypRemote.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 -from os import listdir, remove, walk -from os.path import expanduser - - -def delete(_file_path, _target_database): # deletes an entry or folder - from shutil import rmtree - try: - if _file_path.endswith('/'): - rmtree(f"{expanduser('~/.local/share/sshyp/')}{_file_path}") - else: - remove(f"{expanduser('~/.local/share/sshyp/')}{_file_path}.gpg") - except FileNotFoundError: - print(f"location does not exist {_target_database}") - if _target_database == 'remotely': - for _device_name in listdir(expanduser('~/.config/sshyp/devices')): - open(expanduser('~/.config/sshyp/deleted/') + _file_path.replace('/', '\x1e') + '\x1f' + _device_name, 'w') - - -def deletion_check(_client_device_name): # creates a list of entries and folders to be deleted from a client device - for _file in listdir(expanduser('~/.config/sshyp/deleted')): - _file_path, _sep, _device = _file.partition('\x1f') - _file_path = _file_path.replace('\x1e', '/') - if _device == _client_device_name: - print(_file_path) - try: - remove(f"{expanduser('~/.config/sshyp/deleted/')}{_file}") - except FileNotFoundError: - pass - - -def folder_check(): # creates a list of folders to be compared with those of a client device - for _root, _directories, _files in walk(expanduser('~/.local/share/sshyp')): - for _dir in _directories: - print(f"{_root.replace(expanduser('~'), '')}/{_dir}")