import subprocess from getpass import getpass from sys import argv def run(command, **kwargs): return subprocess.run(command, shell=True, **kwargs) def cap_live_lines(command, **kwargs): lines = [] with subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, text=True, **kwargs ) as proc: for line in proc.stdout: print(line, end="") lines.append(line.strip()) return lines def press_to_exit(): getpass("Exit") def update_rpm_ostree(): lines = cap_live_lines("rpm-ostree upgrade") if "no upgrade" not in lines[-1].lower(): press_to_exit() def update_flatpak(): lines = cap_live_lines("flatpak update -y") done_updates = "nothing to do" not in lines[-1].lower() run("flatpak remove --delete-data --unused") if done_updates: press_to_exit() def update_pipx(): lines = cap_live_lines("pipx upgrade-all --include-injected") if "did not change" not in out[-1].lower(): press_to_exit() def update_nvim(): run('nvim --headless -c "autocmd User PackerComplete quitall" -c "PackerSync"') """ podman run \ --name build \ -dit \ -v ~/.cargo:/root/.cargo:Z \ -v ~/.npm-global:/root/.npm-global:Z \ registry.fedoraproject.org/fedora:(rpm -E %fedora) """ def command_in_build(command, export_path): if export_path: export_container_path = ( "export PATH=/root/.cargo/bin:/root/.npm-global/bin:$PATH && " ) else: export_container_path = "" return f"podman exec -it build bash -c '{export_container_path}{command}'" def run_in_build(command, export_path=True, **kwargs): return run(command_in_build(command, export_path), **kwargs) def cap_live_lines_in_build(command, export_path=True, **kwargs): lines = cap_live_lines(command_in_build(command, export_path), **kwargs) return lines def start_build(): run("podman start build") def stop_build(): run("podman kill build") def update_build(): run_in_build("dnf update -y && dnf autoremove -y", export_path=False) def update_cargo(): """ podman exec -it build bash -c 'dnf install -y cargo pkg-config openssl-devel libssh2-devel libgit2-devel' podman exec -it build bash -c 'cargo install ...' """ run_in_build("cargo install-update -l") lines = ( run_in_build("cargo install-update -a", stdout=subprocess.PIPE, text=True) .stdout.strip() .split("\n") ) if "no packages need updating" not in lines[-2].lower(): print("\n---DONE---\n") press_to_exit() def update_npm(): """ podman exec -it build bash -c 'dnf install -y npm' podman exec -it build bash -c 'npm config set prefix /root/.npm-global' podman exec -it build bash -c 'npm install -g npm ...' """ run_in_build("npm update -g") def main(input): if input == "start": start_build() elif input == "rpm-ostree": update_rpm_ostree() elif input == "flatpak": update_flatpak() elif input == "nvim": update_nvim() elif input == "cargo": update_cargo() elif input == "pipx": update_pipx() elif input == "npm": update_npm() elif input == "end": update_build() stop_build() else: print(f"Input {input} not valid!") if __name__ == "__main__": main(argv[1])