From 32c08cf63e23c886f59e5377815babe3e56be647 Mon Sep 17 00:00:00 2001 From: gyulaid Date: Fri, 8 Apr 2022 22:14:43 +0000 Subject: [PATCH] Added PyPiRunner - unstable --- alice-ci/src/alice/runners/pypirunner.py | 104 ++++++++++++++++++++--- ci-examples/full.yaml | 4 +- 2 files changed, 92 insertions(+), 16 deletions(-) diff --git a/alice-ci/src/alice/runners/pypirunner.py b/alice-ci/src/alice/runners/pypirunner.py index 3eda5e9..e644210 100644 --- a/alice-ci/src/alice/runners/pypirunner.py +++ b/alice-ci/src/alice/runners/pypirunner.py @@ -1,29 +1,105 @@ import json -import sys -from urllib import request -from pkg_resources import parse_version +from urllib import request +from pkg_resources import parse_version +from os import environ, path +from alice.runners.pyutils import PackageManager +from alice.exceptions import ConfigException -from alice.exceptions import NonZeroRetcode, RunnerError, ConfigException + +def grab_from(target): + if "from_env" in target: + return environ[target["from_env"]] + else: + raise ConfigException(f"Unsupported grabber: {target.keys()}") + + +def get_uri(config, default): + if "repo" in config: + return config["repo"].get("uri", default) + return default + + +def get_user(config, default): + if "repo" in config: + if "username" in config["repo"]: + data = config["repo"]["username"] + if isinstance(data, str): + return data + else: + return grab_from(data) + return default + + +def get_pass(config, default): + if "repo" in config: + if "password" in config["repo"]: + data = config["repo"]["password"] + if isinstance(data, str): + return data + else: + return grab_from(data) + return default + + +# Parses and stores the config from yaml +class PypiConfig: + def __init__(self, config={}) -> None: + self.workdir = config.get("workdir", None) + self.repo_uri = get_uri(config, "https://pypi.python.org/pypi") + self.repo_user = get_user(config, None) + self.repo_pass = get_pass(config, None) + self.packages = config.get("packages", set()) + self.upload = config.get("upload", False) + print(self.packages) + + # returns a PyPiConfig with merged values + def copy(self, job_config={}): + p = PypiConfig() + p.workdir = job_config.get("workdir", self.workdir) + p.repo_uri = get_uri(job_config, self.repo_uri) + p.repo_user = get_user(job_config, self.repo_user) + p.repo_pass = get_pass(job_config, self.repo_pass) + p.packages = set(job_config.get("packages", [])).update(self.packages) + p.upload = job_config.get("upload", self.upload) + return p class PyPiRunner(): - def __init__(self, params, user_defaults) -> None: + def __init__(self, params, config) -> None: self.verbose = params["verbose"] if self.verbose: print("[PyPiRunner] Initializing") - self.workdir = user_defaults["workdir"] - # config only contains env and workdir - self.config = user_defaults - self.default_repo = "https://pypi.python.org/pypi" + self.workdir = config["workdir"] + self.config = PypiConfig(config) def __versions(self, pkg_name): # TODO: Error handling - url = f'{self.default_repo}/{pkg_name}/json' + url = f'{self.config.repo_uri}/{pkg_name}/json' releases = json.loads(request.urlopen(url).read())['releases'] - return sorted(releases, key=parse_version, reverse=True) + return sorted(releases, key=parse_version, reverse=True) + + def build(self, path): + # TODO: Actual build - silent, unless failure! + pass - def update_config(self, config): - print(config) + def upload(self, path, repo_uri, repo_pass, repo_user): + # TODO: Implement + pass def run(self, job_spec): - print(self.__versions("alice-ci")) \ No newline at end of file + print(self.__versions("alice-ci")) + job_config = self.config.copy(job_spec) + # TODO: This prints out None !!!!!!!!!!!!! + print(job_config.packages) + return + + PackageManager.getInstance().ensure("build") + for package in job_config.packages: + self.build(path.join(job_config.workdir, package)) + if job_config.upload: + PackageManager.getInstance().ensure("twine") + for package in job_config.packages: + self.build(path.join(job_config.workdir, package), + job_config.repo_uri, + job_config.repo_pass, + job_config.repo_user) diff --git a/ci-examples/full.yaml b/ci-examples/full.yaml index 3a5d0fa..b6aba40 100644 --- a/ci-examples/full.yaml +++ b/ci-examples/full.yaml @@ -7,7 +7,7 @@ runners: value: B - name: C value: C - workdir: packages + workdir: . python: env: - name: A @@ -41,6 +41,6 @@ jobs: uri: example.com username: asdf password: - from_env: PYPIPASS + from_env: COLORTERM packages: - alice-ci \ No newline at end of file