Copy new expanduser() usage from sshyp to sshync

Former-commit-id: 5a9e2f23d2e2b74901e94424e93e59b30b10782b
Former-commit-id: 3c92a86c2751499484e78eaf86b6d193f6a4511d
This commit is contained in:
2023-03-01 23:59:55 -05:00
parent 0109816d3d
commit 6584686b2c
+12 -11
View File
@@ -3,26 +3,27 @@ from os import listdir, remove, walk
from os.path import expanduser, isdir, getmtime, join from os.path import expanduser, isdir, getmtime, join
from subprocess import CalledProcessError, PIPE, run from subprocess import CalledProcessError, PIPE, run
from sys import exit as s_exit from sys import exit as s_exit
home = expanduser("~")
# REMOTE # REMOTE
def remote_list_gen(_client_device_name, _remote_dir): # prints all necessary remote data to stdout def remote_list_gen(_client_device_name, _remote_dir): # prints all necessary remote data to stdout
# deletions # deletions
for _file in listdir(expanduser('~/.config/sshyp/deleted')): # TODO copy expanduser change from sshyp for _file in listdir(f"{home}/.config/sshyp/deleted"):
_file_path, _sep, _device = _file.partition('\x1f') _file_path, _sep, _device = _file.partition('\x1f')
_file_path = _file_path.replace('\x1e', '/') _file_path = _file_path.replace('\x1e', '/')
if _device == _client_device_name: if _device == _client_device_name:
print(_file_path) print(_file_path)
try: try:
remove(f"{expanduser('~/.config/sshyp/deleted/')}{_file}") remove(f"{home}/.config/sshyp/deleted/{_file}")
except FileNotFoundError: except FileNotFoundError:
pass pass
print('\x1d') print('\x1d')
# folders # folders
for _root, _directories, _files in walk(expanduser('~/.local/share/sshyp')): for _root, _directories, _files in walk(f"{home}/.local/share/sshyp"):
for _dir in _directories: for _dir in _directories:
print(f"{_root.replace(expanduser('~'), '')}/{_dir}") print(f"{_root.replace(home, '')}/{_dir}")
print('\x1d') print('\x1d')
get_local_data(_remote_dir, 'server') # titles and mod times get_local_data(_remote_dir, 'server') # titles and mod times
@@ -33,14 +34,14 @@ def delete(_file_path, _target_database): # deletes a file or folder and/or mar
from shutil import rmtree from shutil import rmtree
try: try:
if _file_path.endswith('/'): # TODO properly check if _file_path is a directory if _file_path.endswith('/'): # TODO properly check if _file_path is a directory
rmtree(f"{expanduser('~/.local/share/sshyp/')}{_file_path}") rmtree(f"{home}/.local/share/sshyp/{_file_path}")
else: else:
remove(f"{expanduser('~/.local/share/sshyp/')}{_file_path}.gpg") remove(f"{home}/.local/share/sshyp/{_file_path}.gpg")
except FileNotFoundError: except FileNotFoundError:
print(f"location does not exist {_target_database}") print(f"location does not exist {_target_database}")
if _target_database == 'remotely': if _target_database == 'remotely':
for _device_name in listdir(expanduser('~/.config/sshyp/devices')): for _device_name in listdir(f"{home}/.config/sshyp/devices"):
open(expanduser('~/.config/sshyp/deleted/') + _file_path.replace('/', '\x1e') + '\x1f' + _device_name, 'w') open(f"{home}/.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 def get_local_data(_directory, _device): # retrieves and returns titles and mod times from the local device
@@ -87,10 +88,10 @@ def deletion_sync(_deletion_database, _silent): # checks for and acts upon file
def folder_sync(_folder_database): # creates matches of remote folders on the local client def folder_sync(_folder_database): # creates matches of remote folders on the local client
from pathlib import Path from pathlib import Path
for _folder in _folder_database: for _folder in _folder_database:
if _folder != '' and not isdir(f"{expanduser('~')}{_folder}"): if _folder != '' and not isdir(home + _folder):
print(f"\u001b[38;5;2m{_folder.replace('/.local/share/sshyp/', '')}/\u001b[0m does not exist locally, " print(f"\u001b[38;5;2m{_folder.replace('/.local/share/sshyp/', '')}/\u001b[0m does not exist locally, "
f"creating...") f"creating...")
Path(f"{expanduser('~')}{_folder}").mkdir(mode=0o700, parents=True, exist_ok=True) Path(home + _folder).mkdir(mode=0o700, parents=True, exist_ok=True)
def sort_titles_mods(_list_1, _list_2): # creates and returns two lists (of titles and mod times) generated by sorting def sort_titles_mods(_list_1, _list_2): # creates and returns two lists (of titles and mod times) generated by sorting
@@ -126,7 +127,7 @@ def get_profile(_profile_dir): # returns a list of data read from a sshync job
_local_dir = _profile_data[3].rstrip() _local_dir = _profile_data[3].rstrip()
_remote_dir = _profile_data[4].rstrip() _remote_dir = _profile_data[4].rstrip()
_identity = _profile_data[5].rstrip() _identity = _profile_data[5].rstrip()
_client_device_id = listdir(expanduser('~/.config/sshyp/devices'))[0].rstrip() _client_device_id = listdir(f"{home}/.config/sshyp/devices")[0].rstrip()
return _user, _ip, _port, _local_dir, _remote_dir, _identity, _client_device_id return _user, _ip, _port, _local_dir, _remote_dir, _identity, _client_device_id