From 6584686b2cb2c6534a2120f3bb2d3cec6cd5f762 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Wed, 1 Mar 2023 23:59:55 -0500 Subject: [PATCH] Copy new expanduser() usage from sshyp to sshync Former-commit-id: 5a9e2f23d2e2b74901e94424e93e59b30b10782b Former-commit-id: 3c92a86c2751499484e78eaf86b6d193f6a4511d --- lib/sshync.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/sshync.py b/lib/sshync.py index 35515e0..8a46e87 100755 --- a/lib/sshync.py +++ b/lib/sshync.py @@ -3,26 +3,27 @@ from os import listdir, remove, walk from os.path import expanduser, isdir, getmtime, join from subprocess import CalledProcessError, PIPE, run from sys import exit as s_exit +home = expanduser("~") # REMOTE def remote_list_gen(_client_device_name, _remote_dir): # prints all necessary remote data to stdout # 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 = _file_path.replace('\x1e', '/') if _device == _client_device_name: print(_file_path) try: - remove(f"{expanduser('~/.config/sshyp/deleted/')}{_file}") + remove(f"{home}/.config/sshyp/deleted/{_file}") except FileNotFoundError: pass print('\x1d') # 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: - print(f"{_root.replace(expanduser('~'), '')}/{_dir}") + print(f"{_root.replace(home, '')}/{_dir}") print('\x1d') 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 try: 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: - remove(f"{expanduser('~/.local/share/sshyp/')}{_file_path}.gpg") + remove(f"{home}/.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') + for _device_name in listdir(f"{home}/.config/sshyp/devices"): + 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 @@ -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 from pathlib import Path 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, " 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 @@ -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() _remote_dir = _profile_data[4].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