Skip to content

Commit

Permalink
Refactor backup retrieval logic to support multiple file selections a…
Browse files Browse the repository at this point in the history
…nd improve code readability
  • Loading branch information
Alex Al-Saffar committed Oct 28, 2024
1 parent dc64c07 commit 913bebc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
24 changes: 14 additions & 10 deletions src/machineconfig/scripts/python/devops_backup_retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# import subprocess
from crocodile.file_management import Read, P
from machineconfig.utils.utils import LIBRARY_ROOT, DEFAULTS_PATH, print_code, choose_cloud_interactively, display_options
from machineconfig.utils.utils import LIBRARY_ROOT, DEFAULTS_PATH, print_code, choose_cloud_interactively, choose_multiple_options
from machineconfig.scripts.python.cloud_sync import ES
from platform import system
from typing import Any, Literal, Optional
Expand All @@ -15,21 +15,23 @@

def main_backup_retrieve(direction: OPTIONS, which: Optional[str] = None):
try:
cloud: str=Read.ini(DEFAULTS_PATH)['general']['rclone_config_name']
cloud: str = Read.ini(DEFAULTS_PATH)['general']['rclone_config_name']
print(f"\n{'--' * 50}\n ⚠️ Using default cloud: `{cloud}` ⚠️\n{'--' * 50}\n")
except (FileNotFoundError, KeyError, IndexError): cloud = choose_cloud_interactively()

bu_file: dict[str, Any] = LIBRARY_ROOT.joinpath("profile/backup.toml").readit()
bu_file: dict[str, Any] = Read.toml(LIBRARY_ROOT.joinpath("profile/backup.toml"))
if system() == "Linux": bu_file = {key: val for key, val in bu_file.items() if "windows" not in key}
elif system() == "Windows": bu_file = {key: val for key, val in bu_file.items() if "linux" not in key}

if which is None:
choice_key = display_options(msg=f"WHICH FILE of the following do you want to {direction}?", options=['all'] + list(bu_file.keys()))
assert isinstance(choice_key, str)
else: choice_key = which
choices = choose_multiple_options(msg=f"WHICH FILE of the following do you want to {direction}?", options=['all'] + list(bu_file.keys()))
else:
choices = which.split(",")

if choice_key == "all": items = bu_file
else: items = {choice_key: bu_file[choice_key]}
if "all" in choices: items = bu_file
else:
# items = {choices: bu_file[choices]}
items = {key: val for key, val in bu_file.items() if key in choices}

program = f"""$cloud = "{cloud}:{ES}" \n """ if system() == "Windows" else f"""cloud="{cloud}:{ES}" \n """
for item_name, item in items.items():
Expand All @@ -44,13 +46,15 @@ def main_backup_retrieve(direction: OPTIONS, which: Optional[str] = None):
elif direction == "RETRIEVE": program += f"""\ncloud_copy $cloud "{P(item['path']).as_posix()}" {flags}\n"""
else:
raise RuntimeError(f"Unknown direction: {direction}")
if item_name == "dotfiles" and system() == "Linux": program += f"""\nchmod 700 ~/.ssh/*\n"""
if item_name == "dotfiles" and system() == "Linux": program += """\nchmod 700 ~/.ssh/*\n"""
print_code(program, lexer="shell", desc=f"{direction} script")
return program


def main(direction: OPTIONS, which: Optional[str] = None):
code = main_backup_retrieve(direction, which)
code = main_backup_retrieve(direction=direction, which=which)
from machineconfig.utils.utils import write_shell_script
write_shell_script(program=code, desc="backup_retrieve.sh")


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions src/machineconfig/scripts/python/gh_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


# as per https://github.com/marketplace/models/azure-openai/o1-preview
from openai import OpenAI
from crocodile.file_management import Read, P
Expand Down Expand Up @@ -44,7 +43,7 @@ def interactive_chat():
continue
else:
break

for a_choice in choices:
response_content = a_choice.message.content
print("\n" * 5)
Expand Down
7 changes: 3 additions & 4 deletions src/machineconfig/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def get_shell_script(shell_script: str):
return shell_file


def get_shell_file_executing_python_script(python_script: str, ve_name: str="ve", verbose: bool=True):
def get_shell_file_executing_python_script(python_script: str, ve_name: str, verbose: bool=True):
if verbose:
python_script = f"""
code = r'''{python_script}'''
Expand All @@ -310,9 +310,9 @@ def write_shell_script(program: str, desc: str="", preserve_cwd: bool=True, disp
if display:
print(f"Executing {PROGRAM_PATH}")
print_code(code=program, lexer="shell", desc=desc)

PROGRAM_PATH.create(parents_only=True).write_text(program)
if execute: Terminal().run(f". {PROGRAM_PATH}", shell="powershell").print_if_unsuccessful(desc="Executing shell script", strict_err=True, strict_returncode=True)
if execute:
Terminal().run(f". {PROGRAM_PATH}", shell="powershell").print_if_unsuccessful(desc="Executing shell script", strict_err=True, strict_returncode=True)
return None


Expand Down Expand Up @@ -368,7 +368,6 @@ def get_ssh_hosts() -> list[str]:
def choose_ssh_host(multi: bool=True): return display_options(msg="", options=get_ssh_hosts(), multi=multi, fzf=True)



def check_dotfiles_version_is_beyond(commit_dtm: str, update: bool=False):
dotfiles_path = str(P.home().joinpath("dotfiles"))
from git import Repo
Expand Down

0 comments on commit 913bebc

Please sign in to comment.