Organized functions in sshync.py by use method

This commit is contained in:
2022-12-05 23:21:02 -05:00
parent 56ea8d0cf9
commit a49334482c
2 changed files with 46 additions and 42 deletions
+44 -40
View File
@@ -6,7 +6,43 @@ from subprocess import PIPE, run
from sys import exit as s_exit
def get_local_data(_directory, _device): # retrieves titles and mod times from the local device
# REMOTE
def deletion_check(_client_device_name): # creates a list of files 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}")
# HYBRID
def delete(_file_path, _target_database): # deletes a file or folder and/or marks it for deletion upon syncing
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 get_local_data(_directory, _device): # retrieves and returns titles and mod times from the local device
_title_list, _mod_list = [], []
for _root, _directories, _files in walk(_directory):
for _filename in _files:
@@ -20,7 +56,9 @@ def get_local_data(_directory, _device): # retrieves titles and mod times from
return _title_list, _mod_list
def get_remote_data(_user_data): # retrieves titles and mod times from the remote server
# LOCAL
def get_remote_data(_user_data): # retrieves and returns 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 \'from sshync import get_local_data; get_local_data'
f'("{_user_data[4]}", "server")\''], stdout=PIPE, text=True).stdout.split('\n')
@@ -30,7 +68,7 @@ def get_remote_data(_user_data): # retrieves titles and mod times from the remo
def sort_titles_mods(_list_1, _list_2): # creates and returns two lists (of titles and mod times) generated by sorting
# information from two different, provided lists
# information from two provided 2D lists
_title_list_2_sorted, _mod_list_2_sorted = [], []
# title sorting
for _title in _list_1[0]:
@@ -45,33 +83,7 @@ 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
def deletion_sync(_user_data, _silent): # checks for and acts upon files and folders marked for deletion
_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')
@@ -82,13 +94,7 @@ def deletion_sync(_user_data, _silent): # check for deletions
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
def folder_sync(_user_data): # creates matches of remote folders on the local client
_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')
@@ -110,7 +116,6 @@ def get_profile(_profile_dir): # returns a list of data read from a sshync job
print('\n\u001b[38;5;9merror: the profile does not exist or is corrupted.\u001b[0m\n')
_profile_data = None
s_exit(3)
# extract data from profile
_user = _profile_data[0].rstrip()
_ip = _profile_data[1].rstrip()
_port = _profile_data[2].rstrip()
@@ -125,8 +130,7 @@ 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
# sync new and updated files
_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)
+2 -2
View File
@@ -9,7 +9,7 @@ from subprocess import CalledProcessError, DEVNULL, PIPE, run
from sys import argv, exit as s_exit
# BELOW - utility functions
# UTILITY FUNCTIONS
def entry_list_gen(_directory=expanduser('~/.local/share/sshyp/')): # generates and prints full entry list
from textwrap import fill
@@ -228,8 +228,8 @@ def copy_id_check(_port, _username_ssh, _ip, _client_device_id):
open(expanduser('~/.config/sshyp/ssh-error'), 'w').write('0')
return 0
# BELOW - argument-specific functions
# ARGUMENT-SPECIFIC FUNCTIONS
def tweak(): # runs configuration wizard
from os import symlink