From e449e41971355ca73d1c25b163bb9871bbd3e3d2 Mon Sep 17 00:00:00 2001 From: asoler Date: Sun, 30 Jan 2022 18:58:39 +0100 Subject: [PATCH] Refactoring Repl --- .coverage | Bin 53248 -> 53248 bytes r2env/exceptions.py | 2 + r2env/repl.py | 186 ++++++++++++++++++++-------------------- r2env/test/test_repl.py | 25 ++++++ run_tests.sh | 2 +- tox.ini | 10 +-- 6 files changed, 125 insertions(+), 100 deletions(-) create mode 100644 r2env/exceptions.py diff --git a/.coverage b/.coverage index dfe2d9e2b4226711adf17f105154145b09350b59..c4392675ec41a94142013829ddd0b26216420746 100644 GIT binary patch delta 436 zcmZozz}&Eac|sCXaKgrvMe>&q(|S}GVBSs9vG85#33 zF-r@Vr4|)u=I5E|88r$qvdA_`PR`etP}M7_OwB9Phe{Uf8|xVw=o#qeWG3ksRF-7q z=Ya$^uh$P3;J(Sge~iDA-;ZB}Z?j;-2|gi9R%V7u{m^2d!TPzSMFkm^lLh@1Cx7tc z5VBxlW++FO(h#y_#-c+>(g^4TphK*gm>Fu(H7UvzBqnDkrl$fGSTJHSRdMo9e_ozj z4E)FWyZHV2Mfq+4!{7v;pcQi^BZgsvkh}Cx*mUIr!iSD VmvdrRxA|xMdwoW+%`f}K9RP9Ji8lZM delta 514 zcmZozz}&Eac|sDC^Wlvti{!cV8Td2!Bl&Il^*0L&$n#G&(w9#(uuw3xurfBYGBM?4 zVwM&zOD!tS%+E8^GinrNWRY!>FRm<(Pb^9=i&obUEe1;J=av=~WK`;hlY=a zBo?LS7p3bL8Kvfx>6d|soXjNsg36MN{5)el%gyWc!v%P@G4Su_ujaSp`@y#jX!#O8 zNe5OIhDKH_W=>A@M zOVXYZw;hU;fBN(CY-iv@uU diff --git a/r2env/exceptions.py b/r2env/exceptions.py new file mode 100644 index 0000000..6242996 --- /dev/null +++ b/r2env/exceptions.py @@ -0,0 +1,2 @@ +class ActionException(Exception): + pass diff --git a/r2env/repl.py b/r2env/repl.py index 7b27e70..ef5be6c 100644 --- a/r2env/repl.py +++ b/r2env/repl.py @@ -3,105 +3,99 @@ import argparse import sys +from r2env.exceptions import ActionException from r2env.tools import print_console, ERROR from r2env.core import R2Env -HELP_MESSAGE = """ -Usage: r2env [-flags] [action] [args...] +class REPL: + HELP_MESSAGE = """ + Usage: r2env [-flags] [action] [args...] + + Flags: + + -h, --help - show this help. + -v, --version - display r2env version. + -m, --meson - use meson instead of acr. + -p, --package - install the dist package instead of building + -l, --list - list available and installed packages + + Actions: + + init - create ~/.r2env directory. + config - display current .r2env settings. + add [pkg] - build and install given package. See -p and -m + use [pkg] - stow a specific version to be the default. + rm [pkg] - remove package from ~/.r2env + path - show path of current r2 in use. + list - list all packages available to r2env. + shell - enter a new shell with PATH env var set. + purge - remove ~/.r2env + + Environment + + R2ENV_PATH - specify different path other than ~/.r2env + + """ -Flags: + def __init__(self): + self.actions_with_argument = ["add", "install", "rm", "uninstall"] + self.actions_with_arguments = ["sh", "shell", "use"] + self._core = R2Env() + self.actions = { + "init": self._core.init, + "v": self._core.version, + "version": self._core.version, + "path": self._core.get_r2_path, + "sh": self._core.shell, + "shell": self._core.shell, + "config": self._core.show_config, + "ls": self._core.list_packages, + "list": self._core.list_packages, + "add": self._core.install, + "install": self._core.install, + "installed": self._core.list_installed_packages, + "uninstall": self._core.uninstall, + "rm": self._core.uninstall, + "use": self._core.use, + "purge": self._core.purge, + "h": self.show_help, + "help": self.show_help + } - -h, --help - show this help. - -v, --version - display r2env version. - -m, --meson - use meson instead of acr. - -p, --package - install the dist package instead of building - -l, --list - list available and installed packages + def show_help(self): + print_console(self.HELP_MESSAGE) -Actions: - - init - create ~/.r2env directory. - config - display current .r2env settings. - add [pkg] - build and install given package. See -p and -m - use [pkg] - stow a specific version to be the default. - rm [pkg] - remove package from ~/.r2env - path - show path of current r2 in use. - list - list all packages available to r2env. - shell - enter a new shell with PATH env var set. - purge - remove ~/.r2env - -Environment - - R2ENV_PATH - specify different path other than ~/.r2env - -""" - - -def show_help(): - print_console(HELP_MESSAGE) - - -def show_version(): - print_console(R2Env().version()) - - -actions_with_argument = ["add", "install", "rm", "uninstall"] -actions_with_arguments = ["sh", "shell", "use"] -actions = { - "init": R2Env().init, - "v": show_version, - "version": show_version, - "path": R2Env().get_r2_path, - "sh": R2Env().shell, - "shell": R2Env().shell, - "config": R2Env().show_config, - "ls": R2Env().list_packages, - "list": R2Env().list_packages, - "add": R2Env().install, - "install": R2Env().install, - "installed": R2Env().list_installed_packages, - "uninstall": R2Env().uninstall, - "rm": R2Env().uninstall, - "use": R2Env().use, - "purge": R2Env().purge, - "h": show_help, - "help": show_help -} - - -def run_action(argp): - action = "" - args = [] - if len(argp.args) > 0: - action = argp.args[0] - if len(argp.args) > 1: - args = argp.args[1:] - if argp.version: - print_console(R2Env().version()) - elif argp.list: - actions["list"]() - elif action == "": - show_help() - elif action not in actions: - print_console("Invalid action", ERROR) - elif action in actions_with_arguments: - actions[action](" ".join(args)) - elif action in actions_with_argument: - exit_if_not_argument_is_set(args, action) - actions[action](args[0], use_meson=argp.meson, use_dist=argp.package) - else: - actions[action]() - - -def exit_if_not_argument_is_set(args, action): - if len(args) < 1: - if action in ["use", "rm", "uninstall"]: - print_console("[x] Package not defined.", ERROR) - R2Env().list_installed_packages() + def run_action(self, action_args): + action = "" + args = [] + if not action_args or len(action_args) == 0: + raise ActionException("No action selected") + if action_args.version: + self.actions["version"]() + return + if action_args.list: + list_method = self.actions["list"] + list_method() + if len(action_args) > 1: + args = action_args.args[1:] + action = action_args.args[0] + if len(args) < 1 and action in self.actions_with_argument: + if action in ["use", "rm", "uninstall"]: + R2Env().list_installed_packages() + raise ActionException(f"Action {action} requires a package as an argument.") + raise ActionException(f"Action {action} requires arguments") + if action == "": + self.show_help() + elif action not in self.actions: + raise ActionException("Invalid action selected") + elif action in self.actions_with_arguments: + self.actions[action](" ".join(args)) + elif action in self.actions_with_argument: + self.exit_if_not_argument_is_set(args, action) + self.actions[action](args[0], use_meson=argp.meson, use_dist=argp.package) else: - print_console("[x] Missing package argument.", ERROR) - R2Env().list_available_packages() - sys.exit(-1) + self.actions[action]() def main(): @@ -113,9 +107,13 @@ def main(): parser.add_argument("-m", "--meson", dest="meson", help="Use meson instead of acr to compile", action="store_true") parser.add_argument("-p", "--package", dest="package", help="Use binary package for target system if available", action="store_true") parser.add_argument("-l", "--list", dest="list", help="List available and installed packages", action="store_true") - parser.print_help = show_help - argp = parser.parse_args() - run_action(argp) + parser.print_help = REPL().show_help + action_args = parser.parse_args() + try: + REPL().run_action(action_args) + except ActionException as err: + print_console(err.msg, level=ERROR) + sys.exit(-1) if __name__ == "__main__": diff --git a/r2env/test/test_repl.py b/r2env/test/test_repl.py index e69de29..b4bce94 100644 --- a/r2env/test/test_repl.py +++ b/r2env/test/test_repl.py @@ -0,0 +1,25 @@ +from mock import patch +import unittest + +from r2env.repl import REPL +from r2env.exceptions import ActionException + + +class MockActions: + def __init__(self): + self.args = [] + self.version = False + self.list = False + + +class TestRepl(unittest.TestCase): + def setUp(self): + self.repl = REPL() + + def tearDown(self): + patch.stopall() + + def test_invalid_run_with_no_action(self): + no_action = "" + with self.assertRaises(ActionException): + self.repl.run_action(no_action) diff --git a/run_tests.sh b/run_tests.sh index 095649c..3b647ac 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -6,7 +6,7 @@ readlink=$(which greadlink 2>/dev/null) || readlink=$(which readlink) base_dir=$(cd $(dirname $(${readlink} -f "$0")); pwd) echo "Cleaning *.pyc files ..." -find $base_dir -name \*.pyc | xargs rm 2>/dev/null +find . -name \*.pyc | xargs rm 2>/dev/null python -m coverage run -m pytest -v -rsx -k "test_" --ignore=bin/ ecode=$? diff --git a/tox.ini b/tox.ini index e1036b0..34e3b42 100644 --- a/tox.ini +++ b/tox.ini @@ -1,15 +1,15 @@ [tox] -envlist = py{38,39}-{tests},{py38,py39}-codecheck-{pylint,flake8,bandit} +envlist = py{38,39,310}-{tests},{py38,py39,310}-codecheck-{pylint,flake8,bandit} skipsdist = True [testenv] deps = -r {toxinidir}/requirements/test.txt passenv = * commands = - py{38,39}-tests: {toxinidir}/run_tests.sh unit - py{38,39}-codecheck-pylint: pylint r2env - py{38,39}-codecheck-flake8: flake8 r2env - py{38,39}-codecheck-bandit: bandit -c .bandit.yml -r r2env + py{38,39,310}-tests: {toxinidir}/run_tests.sh unit + py{38,39,310}-codecheck-pylint: pylint r2env + py{38,39,310}-codecheck-flake8: flake8 r2env + py{38,39,310}-codecheck-bandit: bandit -c .bandit.yml -r r2env [flake8] # to ignore complete files, add to the file: # flake8: noqa