diff options
| author | seth <[email protected]> | 2022-08-06 04:31:48 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2022-08-06 04:31:48 -0400 |
| commit | f36a74c805cdbacaa671fd4019204ed3889bae55 (patch) | |
| tree | efa43ecad4ac5e0e6b4c45ea6766c81d8939e05a | |
| parent | 2d53cc8a2ba81ca74ec8e2cb587bee5bd5f43e63 (diff) | |
stop using platform, start using subprocess
also did some more refactoring :trollfig:
| -rw-r--r-- | README.md | 14 | ||||
| -rwxr-xr-x | bin/hiccup | 76 | ||||
| -rw-r--r-- | default-config.json | 10 |
3 files changed, 64 insertions, 36 deletions
@@ -3,8 +3,18 @@ hiccup is a python script that attempts to upgrade your system with multiple package managers. ## how it works -hiccup reads from `$XDG_CONFIG_HOME/hiccup/config.json`. this file specifies distros (by their freedesktop id), shell plugin -managers, and other package managers, paired with a command that runs an update - see `default-config.json`. +hiccup reads from `$XDG_CONFIG_HOME/hiccup/config.json`. this file must contain 5 objects: + +- `system_update_cmds` +- `extra_cmds` +- `clean_cmds` +- `shell_plugin_cmds` +- `other_cmds` + +the value for most keys will be run with `bash -c`, but values in `system_update_cmds` and `clean_cmds` +will run with `sudo bash -c` and values in `shell_plugin_cmds` will run with name of the shell specified. + +see `default-config.json` for example ## how to install hiccup only needs one command to install :) @@ -1,10 +1,11 @@ #!/usr/bin/env python3 import argparse import json -import platform +import subprocess import os CONFIG_FILE = os.path.join(os.environ['XDG_CONFIG_HOME'], 'hiccup/config.json') +OS_RELEASE_PATH = '/etc/os-release' class DistroNotSupportedError(Exception): @@ -13,15 +14,8 @@ class DistroNotSupportedError(Exception): super().__init__(self.message) -class CommandFailedError(Exception): - def __init__(self, name): - self.message = 'failed to use {}'.format(name) - super().__init__(self.message) - - class CurrentDistro: __silent = ' > /dev/null 2>&1' - __sudo = 'sudo ' def __init__(self, id: str): try: @@ -49,18 +43,32 @@ class CurrentDistro: def __get_cmd(self, dct: dict): return dct[self.id] - def __sys_cmd(self, name: str, cmd: str, prepend='', append=''): - result = os.system('{}{}{}'.format(prepend, cmd, append)) - if result != 0: - raise CommandFailedError(name) - return result - - def __run_items(self, msg: str, dct: dict): + def __sys_cmd( + self, + cmd: str, + executable='bash', + append='', + prepend='', + sudo=False): + + cmd = '{}{}{}'.format(prepend, cmd, append) + if sudo: + cmd = 'sudo bash -c \'{}\''.format(cmd) + + return subprocess.run( + cmd, + check=True, + executable=os.path.join("/usr/bin/", executable), + shell=True) + + def __run_items(self, msg: str, dct: dict, name_as_cmd=False): + executable = 'sh' for name, cmd in dct.items(): print(msg.format(name)) - result = self.__sys_cmd(name, cmd, append=self.__silent) - if result != 0: - raise CommandFailedError(name) + + if name_as_cmd: + executable = name + self.__sys_cmd(cmd, executable=executable, append=self.__silent) def is_supported(self): return self.id in self.__system_update_cmds @@ -81,19 +89,20 @@ class CurrentDistro: return self.__get_cmd(self.__clean_cmds) def update_system(self): - self.__sys_cmd(self.id, self.update_cmd, prepend=self.__sudo) + self.__sys_cmd(self.update_cmd, sudo=True) if self.has_extra_cmd(): - self.__sys_cmd(self.id, self.extra_cmd) + self.__sys_cmd(self.extra_cmd) - def clean_system(self): + def cleanup_system(self): if self.has_clean_cmd(): print('cleaning up system...') - return self.__sys_cmd(self.id, self.clean_cmd, prepend=self.__sudo) - raise DistroNotSupportedError(self.id) + return self.__sys_cmd(self.clean_cmd, sudo=True) + print('no cleanup command found for {}'.format(self.id)) def update_shell_plugins(self): msg = 'updating {} plugins...' - return self.__run_items(msg, self.__shell_plugin_cmds) + return self.__run_items(msg, self.__shell_plugin_cmds, + name_as_cmd=True) def update_other(self): msg = 'updating {}...' @@ -103,26 +112,35 @@ class CurrentDistro: self.update_system() self.update_shell_plugins() self.update_other() - if self.has_clean_cmd(): - self.clean_system() + self.cleanup_system() + + +def get_os_release(): + cmd = 'source {}; echo -n $ID'.format(OS_RELEASE_PATH) + p: subprocess.Popen = subprocess.Popen( + cmd, + shell=True, + stdout=subprocess.PIPE) + + return str(p.communicate()[0], 'UTF-8').strip() def run(): - current_distro = platform.freedesktop_os_release()["ID"] + current_distro = get_os_release() distro = CurrentDistro(current_distro) parser = argparse.ArgumentParser( description='a python script to help keep you up to date') parser.add_argument("--cleanonly", "-c", action="store_true", default=False, dest="cleanonly", - help='only remove unneeded dependencies') + help='cleanup unneeded dependencies') parser.add_argument("--systemonly", "-s", action="store_true", default=False, dest="systemonly", help='only update through the system\'s package manager') # noqa: E501 args = parser.parse_args() if args.cleanonly: - return distro.clean_system() + return distro.cleanup_system() if args.systemonly: return distro.update_system() diff --git a/default-config.json b/default-config.json index dd4043c..4616521 100644 --- a/default-config.json +++ b/default-config.json @@ -1,20 +1,20 @@ { "system_update_cmds": { - "arch": "bash -c 'pacman -Sy --needed archlinux-keyring && pacman -Su'", - "debian": "bash -c 'apt update && apt upgrade'", + "arch": "pacman -Sy --needed archlinux-keyring && pacman -Su", + "debian": "apt update && apt upgrade", "fedora": "dnf upgrade" }, "extra_cmds": { "arch": "aur sync --upgrades --rmdeps --sign --remove --verify" }, "clean_cmds": { - "arch": "bash -c 'pacman -Qdtq | pacman -Rns -; exit 0'", + "arch": "pacman -Rns $(pacman -Qdtq); exit 0", "debian": "apt-get --purge autoremove", "fedora": "dnf autoremove" }, "shell_plugin_cmds": { - "fish": "fish -c 'fisher update'", - "zsh": "zsh -c 'source $HOME/.config/zsh/.antidote/antidote.zsh && antidote update'" + "fish": "fisher update", + "zsh": "source $HOME/.config/zsh/.antidote/antidote.zsh && antidote update" }, "other_cmds": { "neovim": "nvim --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync'" |
