From 5120293c294ccb14df3c512560f934b343ee8a09 Mon Sep 17 00:00:00 2001 From: gyulaid Date: Fri, 8 Apr 2022 21:55:40 +0000 Subject: [PATCH] Cleanup + pep8 --- alice-ci/src/alice/__init__.py | 2 +- alice-ci/src/alice/configparser.py | 1 - alice-ci/src/alice/runnerfactory.py | 7 +- alice-ci/src/alice/runners/pythonrunner.py | 6 +- alice-ci/src/alice/runners/pyutils.py | 13 ++-- test.py | 85 ---------------------- 6 files changed, 16 insertions(+), 98 deletions(-) delete mode 100644 test.py diff --git a/alice-ci/src/alice/__init__.py b/alice-ci/src/alice/__init__.py index a442884..3c513fe 100644 --- a/alice-ci/src/alice/__init__.py +++ b/alice-ci/src/alice/__init__.py @@ -1,5 +1,5 @@ # flake8: noqa F401 -from alice.utils import ConfigParser +from alice.configparser import ConfigParser from alice.exceptions import NonZeroRetcode from alice.runnerfactory import Factory from alice.runners.pythonrunner import PythonRunner diff --git a/alice-ci/src/alice/configparser.py b/alice-ci/src/alice/configparser.py index 4af4848..4ecfb26 100644 --- a/alice-ci/src/alice/configparser.py +++ b/alice-ci/src/alice/configparser.py @@ -1,4 +1,3 @@ -import inspect from os import getcwd, path, environ import subprocess import yaml diff --git a/alice-ci/src/alice/runnerfactory.py b/alice-ci/src/alice/runnerfactory.py index 24db0a3..618248c 100644 --- a/alice-ci/src/alice/runnerfactory.py +++ b/alice-ci/src/alice/runnerfactory.py @@ -20,8 +20,8 @@ class Factory(): # https://git.gyulai.cloud/gyulaid/alice/issues/4 # module = __import__("module_file") # my_class = getattr(module, "class_name") - self.runners = {"python": PythonRunner, - "pypi": PyPiRunner} + self.runnertypes = {"python": PythonRunner, + "pypi": PyPiRunner} if (self.verbose): print(f"[Alice] Available runners: {'|'.join(self.runnertypes.keys())}") @@ -50,7 +50,8 @@ class Factory(): params = { "verbose": self.verbose } - config = self.runner_configs[runnertype] + # If there is a runner specific config, use that, else global + config = self.runner_configs.get(runnertype, self.globals.copy()) self.runners[runnertype] = self.runnertypes[runnertype](params, config) else: raise ConfigException(f"Invalid runner type: {runnertype}") diff --git a/alice-ci/src/alice/runners/pythonrunner.py b/alice-ci/src/alice/runners/pythonrunner.py index bd3dc1b..f69d9cf 100644 --- a/alice-ci/src/alice/runners/pythonrunner.py +++ b/alice-ci/src/alice/runners/pythonrunner.py @@ -1,4 +1,3 @@ -from http.server import executable import subprocess import os import sys @@ -7,6 +6,7 @@ import shlex from alice.exceptions import NonZeroRetcode, RunnerError, ConfigException from alice.runners.pyutils import PackageManager + # TODO: Handle config like PyPiConfig class PythonRunner: def __init__(self, params, config) -> None: @@ -39,12 +39,12 @@ class PythonRunner: if self.verbose: print(f"[PythonRunner] Found virtualenv at {self.virtual_dir}") dependencies = self.config.get("dependencies", []) - if len(dependencies) >0: + if len(dependencies) > 0: if self.verbose: print(f"[PythonRunner] Ensuring dependencies: {', '.join(dependencies)}") PackageManager.getInstance().ensure_more(dependencies, executable=self.vpython) if self.verbose: - print(f"[PythonRunner] Installation done") + print("[PythonRunner] Installation done") def __ghetto_glob(self, command, workdir): if self.verbose: diff --git a/alice-ci/src/alice/runners/pyutils.py b/alice-ci/src/alice/runners/pyutils.py index 6631f53..ca95641 100644 --- a/alice-ci/src/alice/runners/pyutils.py +++ b/alice-ci/src/alice/runners/pyutils.py @@ -1,21 +1,24 @@ import subprocess import sys -from pkg_resources import parse_version +from pkg_resources import parse_version import re from alice.exceptions import RunnerError, ConfigException + class PackageManager: __instance = None + @staticmethod def getInstance(): """ Static access method. """ - if PackageManager.__instance == None: + if PackageManager.__instance is None: PackageManager() return PackageManager.__instance + def __init__(self): """ Virtually private constructor. """ - if PackageManager.__instance != None: + if PackageManager.__instance is not None: raise Exception("This class is a singleton!") else: PackageManager.__instance = self @@ -24,7 +27,7 @@ class PackageManager: def __get_packages(self): packages = {} with subprocess.Popen([sys.executable, "-m", "pip", "freeze"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: + stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: p.wait() installed = list(map(lambda x: x.decode("UTF-8").split("=="), filter(lambda x: b'==' in x, p.stdout.read().splitlines()))) for name, version in installed: @@ -76,4 +79,4 @@ class PackageManager: raise ConfigException(f"Illegal comparator found: {comparator}") else: return True - return False \ No newline at end of file + return False diff --git a/test.py b/test.py deleted file mode 100644 index ef1a001..0000000 --- a/test.py +++ /dev/null @@ -1,85 +0,0 @@ -import subprocess -import sys -from pkg_resources import parse_version -import re - -class PackageManager: - __instance = None - @staticmethod - def getInstance(): - """ Static access method. """ - if PackageManager.__instance == None: - PackageManager() - return PackageManager.__instance - def __init__(self): - """ Virtually private constructor. """ - if PackageManager.__instance != None: - raise Exception("This class is a singleton!") - else: - PackageManager.__instance = self - self.package_list = self.__get_packages() - - def __get_packages(self): - packages = {} - with subprocess.Popen([sys.executable, "-m", "pip", "freeze"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: - p.wait() - installed = list(map(lambda x: x.decode("UTF-8").split("=="), filter(lambda x: b'==' in x, p.stdout.read().splitlines()))) - for name, version in installed: - packages[name] = parse_version(version) - return packages - - def ensure_more(self, package_list, executable=sys.executable): - to_install = list(filter(lambda x: not self.__has_package(x), package_list)) - if len(to_install) > 0: - command = [executable, "-m", "pip", "install"] + to_install - with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: - p.wait() - if p.returncode != 0: - sys.stdout.buffer.write(p.stderr.read()) - raise(Exception(f"[PythonRunner] Could not install dependencies ({p.returncode})")) - self.package_list = self.__get_packages() - - # Assumption: there are more hits in the long run, than misses - def ensure(self, package_string, executable=sys.executable): - if not self.__has_package(package_string): - command = [executable, "-m", "pip", "install", package_string] - with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: - p.wait() - if p.returncode != 0: - sys.stdout.buffer.write(p.stderr.read()) - raise(Exception(f"[PythonRunner] Could not install dependencies ({p.returncode})")) - self.package_list = self.__get_packages() - - def __has_package(self, package_string): - package_data = re.split("==|>|>=|<|<=", package_string) - # check in cache - if package_data[0] in self.package_list: - # check if version is needed - if len(package_data) == 2: - required_version = parse_version(package_data[1]) - installed_version = self.package_list[package_data[0]] - comparator = package_string.replace(package_data[0], "").replace(package_data[1], "") - if comparator == "==": - return required_version == installed_version - elif comparator == ">": - return installed_version > required_version - elif comparator == ">=": - return installed_version >= required_version - elif comparator == "<": - return installed_version < required_version - elif comparator == "<=": - return installed_version <= required_version - else: - raise Exception(f"Illegal comparator found: {comparator}") - else: - return True - return False - - - -if __name__ == "__main__": - p = PackageManager().getInstance() - - print(p.package_list) - p.ensure_more(["kubernetes", "minio"]) \ No newline at end of file