From 0c3eab9ed45e6f7a4440e37dff3e6c13022fd65a Mon Sep 17 00:00:00 2001 From: riley Date: Wed, 22 Sep 2021 03:52:45 -0400 Subject: [PATCH] add xonsh addins --- .xonshrc | 11 +++++++++++ addins/__init__.py | 4 ++++ addins/package.xsh | 40 ++++++++++++++++++++++++++++++++++++++++ addins/version.xsh | 21 +++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 addins/__init__.py create mode 100644 addins/package.xsh create mode 100644 addins/version.xsh diff --git a/.xonshrc b/.xonshrc index 3fd0722..58c4d4e 100644 --- a/.xonshrc +++ b/.xonshrc @@ -4,6 +4,14 @@ $XONSH_COLOR_STYLE = 'fruity' xontrib load apt_tabcomplete argcomplete autovox jedi z # XONSH WEBCONFIG END +# path stuff + +import sys +sys.path.insert(0, '') +sys.path.append(Path("~/addins").resolve()) + +import addins + # debug def _debug(): if $XONSH_SHOW_TRACEBACK: @@ -31,3 +39,6 @@ aliases['bwc'] = _bwc # dots aliases['config'] = '/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME' + +# ls +aliases['ls'] = 'ls -alhs --color=auto' diff --git a/addins/__init__.py b/addins/__init__.py new file mode 100644 index 0000000..263a6be --- /dev/null +++ b/addins/__init__.py @@ -0,0 +1,4 @@ +from .version import * +from .package import * + +__all__ = ['Version', 'Package'] diff --git a/addins/package.xsh b/addins/package.xsh new file mode 100644 index 0000000..b69c7cb --- /dev/null +++ b/addins/package.xsh @@ -0,0 +1,40 @@ +from .version import Version +v = Version() + +class Package: + def __init__(self, name, source): + self.name = name + self.source = source + self._installed = None + + @property + def installed(self): + self._installed = self._get_installed() + return self._installed + + def _get_installed(self) -> bool: + if self.source == 'apt': + if !(dpkg -s @(self.name)).returncode == 0: + return True + else: + return False + if self.source == 'npm': + if !(npm list -g @(self.name)).returncode == 0: + return True + else: + return False + else: + return None + + def install(self): + if self.installed: + print(f"package '{self.name}' already installed.") + return + elif self.source == 'apt': + $(sudo apt-get install -qq --yes @(self.name)) + elif self.source == 'npm': + $(npm install -g @(self.name)) + elif self.source == 'pacman': + $(pacman -Syu @(self.name)) + else: + print(f"unsupported package manager '{source}'.") diff --git a/addins/version.xsh b/addins/version.xsh new file mode 100644 index 0000000..b3da493 --- /dev/null +++ b/addins/version.xsh @@ -0,0 +1,21 @@ +from pathlib import Path + +__all__ = ['Version'] + +class Version: + def __init__(self): + data = self._load_dict() + for key, value in data.items(): + setattr(self, key.lower(), value) + if hasattr(self, "id_like"): # special handling for ID_LIKE + self.id_like = self.id_like.split() + + def _load_dict(self): + data = {} + os_files = Path('/etc').glob("*-release") + for i in os_files: + with i.open() as f: + for line in f: + line = line.split("=") + data[line[0]] = line[1].strip().replace('"', '') + return data