Refactoring Repl

This commit is contained in:
asoler authored and Murphy committed 2022-03-15 11:36:37 +01:00
1 parent 56426fd1f2
commit e449e41971
6 files changed
+125 -100

No files matched your search

BIN
View File
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
class ActionException(Exception):
pass
+92 -94
View File
@@ -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__":
+25
View File
@@ -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)
+1 -1
View File
@@ -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=$?
+5 -5
View File
@@ -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