Browse Source

Cleanup + pep8

pull/18/head
Daniel Gyulai 3 years ago
parent
commit
5120293c29
  1. 2
      alice-ci/src/alice/__init__.py
  2. 1
      alice-ci/src/alice/configparser.py
  3. 5
      alice-ci/src/alice/runnerfactory.py
  4. 4
      alice-ci/src/alice/runners/pythonrunner.py
  5. 7
      alice-ci/src/alice/runners/pyutils.py
  6. 85
      test.py

2
alice-ci/src/alice/__init__.py

@ -1,5 +1,5 @@
# flake8: noqa F401 # flake8: noqa F401
from alice.utils import ConfigParser from alice.configparser import ConfigParser
from alice.exceptions import NonZeroRetcode from alice.exceptions import NonZeroRetcode
from alice.runnerfactory import Factory from alice.runnerfactory import Factory
from alice.runners.pythonrunner import PythonRunner from alice.runners.pythonrunner import PythonRunner

1
alice-ci/src/alice/configparser.py

@ -1,4 +1,3 @@
import inspect
from os import getcwd, path, environ from os import getcwd, path, environ
import subprocess import subprocess
import yaml import yaml

5
alice-ci/src/alice/runnerfactory.py

@ -20,7 +20,7 @@ class Factory():
# https://git.gyulai.cloud/gyulaid/alice/issues/4 # https://git.gyulai.cloud/gyulaid/alice/issues/4
# module = __import__("module_file") # module = __import__("module_file")
# my_class = getattr(module, "class_name") # my_class = getattr(module, "class_name")
self.runners = {"python": PythonRunner, self.runnertypes = {"python": PythonRunner,
"pypi": PyPiRunner} "pypi": PyPiRunner}
if (self.verbose): if (self.verbose):
@ -50,7 +50,8 @@ class Factory():
params = { params = {
"verbose": self.verbose "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) self.runners[runnertype] = self.runnertypes[runnertype](params, config)
else: else:
raise ConfigException(f"Invalid runner type: {runnertype}") raise ConfigException(f"Invalid runner type: {runnertype}")

4
alice-ci/src/alice/runners/pythonrunner.py

@ -1,4 +1,3 @@
from http.server import executable
import subprocess import subprocess
import os import os
import sys import sys
@ -7,6 +6,7 @@ import shlex
from alice.exceptions import NonZeroRetcode, RunnerError, ConfigException from alice.exceptions import NonZeroRetcode, RunnerError, ConfigException
from alice.runners.pyutils import PackageManager from alice.runners.pyutils import PackageManager
# TODO: Handle config like PyPiConfig # TODO: Handle config like PyPiConfig
class PythonRunner: class PythonRunner:
def __init__(self, params, config) -> None: def __init__(self, params, config) -> None:
@ -44,7 +44,7 @@ class PythonRunner:
print(f"[PythonRunner] Ensuring dependencies: {', '.join(dependencies)}") print(f"[PythonRunner] Ensuring dependencies: {', '.join(dependencies)}")
PackageManager.getInstance().ensure_more(dependencies, executable=self.vpython) PackageManager.getInstance().ensure_more(dependencies, executable=self.vpython)
if self.verbose: if self.verbose:
print(f"[PythonRunner] Installation done") print("[PythonRunner] Installation done")
def __ghetto_glob(self, command, workdir): def __ghetto_glob(self, command, workdir):
if self.verbose: if self.verbose:

7
alice-ci/src/alice/runners/pyutils.py

@ -5,17 +5,20 @@ import re
from alice.exceptions import RunnerError, ConfigException from alice.exceptions import RunnerError, ConfigException
class PackageManager: class PackageManager:
__instance = None __instance = None
@staticmethod @staticmethod
def getInstance(): def getInstance():
""" Static access method. """ """ Static access method. """
if PackageManager.__instance == None: if PackageManager.__instance is None:
PackageManager() PackageManager()
return PackageManager.__instance return PackageManager.__instance
def __init__(self): def __init__(self):
""" Virtually private constructor. """ """ Virtually private constructor. """
if PackageManager.__instance != None: if PackageManager.__instance is not None:
raise Exception("This class is a singleton!") raise Exception("This class is a singleton!")
else: else:
PackageManager.__instance = self PackageManager.__instance = self

85
test.py

@ -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"])
Loading…
Cancel
Save