diff options
| author | seth <[email protected]> | 2022-08-21 13:14:00 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2022-08-21 13:15:45 -0400 |
| commit | a615f6cc4901a05f7fe72a55d005280119cb792e (patch) | |
| tree | 0f3ddc4ac0a27e3a0077e0b79082df17a4e6e5b9 /bin | |
| parent | a2ff6e94b6ba21818613611cb3fed15e56f4894d (diff) | |
refactor and (try to) follow PEP518
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/hiccup | 160 |
1 files changed, 0 insertions, 160 deletions
diff --git a/bin/hiccup b/bin/hiccup deleted file mode 100755 index d65b650..0000000 --- a/bin/hiccup +++ /dev/null @@ -1,160 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import json -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): - def __init__(self, name): - self.message = '{} isn\'t supported yet'.format(name) - super().__init__(self.message) - - -class CurrentDistro: - __silent = ' > /dev/null 2>&1' - - def __init__(self, id: str): - try: - with open(CONFIG_FILE) as file: - data = json.load(file) - self.__system_update_cmds = data['system_update_cmds'] - self.__extra_cmds = data['extra_cmds'] - self.__clean_cmds = data['clean_cmds'] - self.__shell_plugin_cmds = data['shell_plugin_cmds'] - self.__other_cmds = data['other_cmds'] - except Exception: - pass - - self.id = id - - if self.is_supported(): - self.update_cmd = self.get_update_cmd() - if self.has_clean_cmd(): - self.clean_cmd = self.get_clean_cmd() - if self.has_extra_cmd(): - self.extra_cmd = self.get_extra_cmd() - else: - raise DistroNotSupportedError(self.id) - - def __get_cmd(self, dct: dict): - return dct[self.id] - - 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=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)) - - 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 - - def has_clean_cmd(self): - return self.id in self.__clean_cmds - - def has_extra_cmd(self): - return self.id in self.__extra_cmds - - def get_update_cmd(self): - return self.__get_cmd(self.__system_update_cmds) - - def get_extra_cmd(self): - return self.__get_cmd(self.__extra_cmds) - - def get_clean_cmd(self): - return self.__get_cmd(self.__clean_cmds) - - def update_system(self): - self.__sys_cmd(self.update_cmd, sudo=True) - if self.has_extra_cmd(): - self.__sys_cmd(self.extra_cmd) - - def cleanup_system(self): - if self.has_clean_cmd(): - print('cleaning up system...') - 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, - name_as_cmd=True) - - def update_other(self): - msg = 'updating {}...' - return self.__run_items(msg, self.__other_cmds) - - def update_all(self): - self.update_system() - self.update_shell_plugins() - self.update_other() - self.cleanup_system() - - -def get_os_release(): - cmd = 'source {}; echo -n $ID'.format(OS_RELEASE_PATH) - p: subprocess.Popen = subprocess.Popen( - cmd, - shell=True, - executable='bash', - stdout=subprocess.PIPE) - - return str(p.communicate()[0], 'UTF-8').strip() - - -def run(): - 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='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.cleanup_system() - if args.systemonly: - return distro.update_system() - - return distro.update_all() - - -if __name__ == '__main__': - if os.geteuid() == 0: - print('please don\'t run this as root :(') - exit(1) - try: - run() - print('done!') - except Exception as e: - print(repr(e)) - exit(2) |
