Better detect whether user input is a file or a directory (re-write of rename())

Former-commit-id: b74a0973086ecf9967e40024e75a4c5cab730801
Former-commit-id: ea1a29a39eee6fa5b1da12f83455d3ea7da6c00a
This commit is contained in:
2023-03-02 01:32:40 -05:00
parent 6584686b2c
commit d1a994a103
2 changed files with 23 additions and 18 deletions
+4 -3
View File
@@ -32,11 +32,12 @@ def remote_list_gen(_client_device_name, _remote_dir): # prints all necessary r
def delete(_file_path, _target_database): # deletes a file or folder and/or marks it for deletion upon syncing
from shutil import rmtree
_directory = f"{home}/.local/share/sshyp/"
try:
if _file_path.endswith('/'): # TODO properly check if _file_path is a directory
rmtree(f"{home}/.local/share/sshyp/{_file_path}")
if isdir(_directory + _file_path):
rmtree(_directory + _file_path)
else:
remove(f"{home}/.local/share/sshyp/{_file_path}.gpg")
remove(f"{_directory}{_file_path}.gpg")
except FileNotFoundError:
print(f"location does not exist {_target_database}")
if _target_database == 'remotely':
+19 -15
View File
@@ -595,16 +595,25 @@ def add_folder(): # creates a new folder
def rename(): # renames an entry or folder
from shutil import copy
if not isfile(f"{directory}{entry_name}.gpg") and not isdir(f"{directory}{entry_name}"):
print(f"\n\u001b[38;5;9merror: entry (/{entry_name}) does not exist\u001b[0m\n")
_file = True
if not exists(f"{directory}{entry_name}.gpg") and not exists(f"{directory}{entry_name}"):
print(f"\n\u001b[38;5;9merror: (/{entry_name}) does not exist\u001b[0m\n")
s_exit(2)
_new_name = str(input('new name: '))
if _new_name.startswith('/'):
_new_name = _new_name[1:]
if isfile(f"{directory}{_new_name}.gpg") or isdir(f"{directory}{_new_name}"):
print(f"\n\u001b[38;5;9merror: (/{_new_name}) already exists\u001b[0m\n")
s_exit(3)
if entry_name.endswith('/'): # TODO properly check if entry_name is a directory
elif not isfile(f"{directory}{entry_name}.gpg"):
_file = False
_new_name = str(input('new name: ')).strip('/')
if _file: # if renaming a file
if isfile(f"{directory}{_new_name}.gpg"): # check if the new file already exists
print(f"\n\u001b[38;5;9merror: (/{_new_name}) already exists\u001b[0m\n")
s_exit(3)
if not ssh_error:
copy(f"{directory}{entry_name}.gpg", f"{directory}{_new_name}.gpg")
else:
move(f"{directory}{entry_name}.gpg", f"{directory}{_new_name}.gpg")
else: # if renaming a folder
if isdir(f"{directory}{_new_name}"): # check if the new folder already exists
print(f"\n\u001b[38;5;9merror: (/{_new_name}/) already exists\u001b[0m\n")
s_exit(3)
if not ssh_error:
Path(f"{directory}{_new_name}").mkdir(mode=0o700, parents=True, exist_ok=True)
run(['ssh', '-i', f"{home}/.ssh/sshyp", '-p', port, f"{username_ssh}@{ip}",
@@ -612,11 +621,6 @@ def rename(): # renames an entry or folder
f'.mkdir(mode=0o700, parents=True, exist_ok=True)\''])
else:
move(f"{directory}{entry_name}", f"{directory}{_new_name}")
else:
if not ssh_error:
copy(f"{directory}{entry_name}.gpg", f"{directory}{_new_name}.gpg")
else:
move(f"{directory}{entry_name}.gpg", f"{directory}{_new_name}.gpg")
if not ssh_error:
run(['ssh', '-i', f"{home}/.ssh/sshyp", '-p', port, f"{username_ssh}@{ip}",
f'cd /lib/sshyp; python3 -c \'from sshync import delete; delete("{entry_name}", "remotely")\''])
@@ -751,7 +755,7 @@ if __name__ == "__main__":
if arg_count < 1 or (arg_count > 0 and arguments[0] != 'tweak'):
if arg_count > 0 and arguments[0].startswith('/'):
arg_start = 1
entry_name = arguments[0][1:]
entry_name = arguments[0].strip('/')
else:
arg_start = 0