From f4492c0e45a58aded0e01e084e1e5a62c8e68594 Mon Sep 17 00:00:00 2001
From: gyulaid <gyulaid@gyulai.cloud>
Date: Tue, 29 Mar 2022 21:43:42 +0200
Subject: [PATCH] Fix charsh with missing env in config

---
 alice-ci/src/alice/__init__.py             |  1 -
 alice-ci/src/alice/runners/pythonrunner.py |  9 +++++----
 alice-ci/src/alice/utils.py                | 18 +++++++++++-------
 ci-examples/python1.yaml                   | 18 ++++++++++++++++++
 4 files changed, 34 insertions(+), 12 deletions(-)
 create mode 100644 ci-examples/python1.yaml

diff --git a/alice-ci/src/alice/__init__.py b/alice-ci/src/alice/__init__.py
index 1181dd0..5423544 100644
--- a/alice-ci/src/alice/__init__.py
+++ b/alice-ci/src/alice/__init__.py
@@ -6,6 +6,5 @@ from alice.runners.pythonrunner import PythonRunner
 from alice.exceptions import NonZeroRetcode
 from alice.exceptions import RunnerError
 from alice.exceptions import ConfigException
-from alice.__main__ import main
 
 name = "alice"
\ No newline at end of file
diff --git a/alice-ci/src/alice/runners/pythonrunner.py b/alice-ci/src/alice/runners/pythonrunner.py
index 776094b..3a58444 100644
--- a/alice-ci/src/alice/runners/pythonrunner.py
+++ b/alice-ci/src/alice/runners/pythonrunner.py
@@ -42,13 +42,15 @@ class PythonRunner():
         if "dependencies" in config:
             for dependency in config["dependencies"]:
                 # TODO: Check what happens with fixed version
-                with subprocess.Popen([self.vpython, "-m", "pip", "install", dependency, "--upgrade"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
+                command = [self.vpython, "-m", "pip", "install", dependency, "--upgrade"]
+                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(RunnerError(f"PythonRunner: Could not install dependency: {dependency} ({p.returncode})"))
-        for env_var in config["env"]:
-            self.env_vars[env_var["name"]] = env_var["value"]
+        if "env" in config:
+            for env_var in config["env"]:
+                self.env_vars[env_var["name"]] = env_var["value"]
         if "workdir" in config and config["workdir"] is not None:
             self.workdir = os.path.join(self.workdir, config["workdir"])
 
@@ -60,7 +62,6 @@ class PythonRunner():
                 base_name = os.path.basename(item)
                 if os.path.isdir(dir):
                     item_parts = base_name.split("*")
-                    print(item_parts)
                     for file in os.listdir(dir):
                         # TODO: Fix ordering! A*B = B*A = AB*
                         if item_parts[0] in file and item_parts[1] in file:
diff --git a/alice-ci/src/alice/utils.py b/alice-ci/src/alice/utils.py
index b9cd351..49bc933 100644
--- a/alice-ci/src/alice/utils.py
+++ b/alice-ci/src/alice/utils.py
@@ -8,19 +8,23 @@ class ConfigParser:
         with open(file_path) as f:
             self.config = yaml.safe_load(f)
         self.factory = factory
+        self.factory.set_globals(self.__gen_globals())
         if "runners" in self.config:
-            if "global" in self.config["runners"]:
-                self.factory.set_globals(self.__gen_globals())
             self.factory.update_runners(self.config["runners"])
         self.jobs = self.__get_jobs()
 
     # Initialize env, workdir if not present
     def __gen_globals(self):
-        globals = self.config["runners"]["global"]
-        if "env" not in globals:
-            globals["env"] = []
-        if "workdir" not in globals:
-            globals["workdir"] = None
+        globals = {
+            "env": [],
+            "workdir": None
+        }
+        if "runners" in self.config:
+            if "global" in self.config["runners"]:
+                if "env" in self.config["runners"]["global"]:
+                    globals["env"] = self.config["runners"]["global"]["env"]
+                if "workdir" in self.config["runners"]["global"]:
+                    globals["workdir"] = self.config["runners"]["global"]["workdir"]
         return globals
 
     def __get_jobs(self):
diff --git a/ci-examples/python1.yaml b/ci-examples/python1.yaml
new file mode 100644
index 0000000..b562f56
--- /dev/null
+++ b/ci-examples/python1.yaml
@@ -0,0 +1,18 @@
+runners:
+  python:
+    dependencies:
+      - flake8
+      - build
+      - twine
+jobs:
+  - name: selfcheck
+    type: python
+    workdir: ci
+    commands:
+      - "-m flake8 --ignore E501 --exclude venv"
+
+  - name: lint
+    type: python
+    workdir: alice-ci/src
+    commands:
+      - "-m flake8 --ignore E501"
\ No newline at end of file
-- 
GitLab