From df025d18f1a5afc1abb1dc0e2945b19211ba370f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?I=C3=B1igo=20Moreno?= <inigo.moreno@tecnalia.com>
Date: Fri, 17 Jun 2022 12:32:45 +0200
Subject: [PATCH] Make jobs python2 or python3 depending on distro

---
 ci-templates/auto-rules/no-default.yml | 20 ++++++++
 ci-templates/core.yml                  |  1 +
 ci-templates/python.yml                | 63 ++++++++++++++++++++++++++
 ci-templates/syntax-check.yml          | 39 ----------------
 4 files changed, 84 insertions(+), 39 deletions(-)
 create mode 100644 ci-templates/python.yml

diff --git a/ci-templates/auto-rules/no-default.yml b/ci-templates/auto-rules/no-default.yml
index 011021a..51bac7f 100644
--- a/ci-templates/auto-rules/no-default.yml
+++ b/ci-templates/auto-rules/no-default.yml
@@ -38,6 +38,26 @@ industrial_ci_noetic:
     - if: $DEFAULT_DISTRO == "noetic"
     - if: $BUILD_NOETIC
 
+
+.py3_template:
+  rules:
+    - if: $DEFAULT_DISTRO == "noetic"
+      when: always
+    - if: $BUILD_NOETIC
+      when: always
+  when: always
+
+.py2_template:
+  rules:
+    - if: $DEFAULT_DISTRO == "kinetic"
+      when: always
+    - if: $BUILD_KINETIC
+      when: always
+    - if: $DEFAULT_DISTRO == "melodic"
+      when: always
+    - if: $BUILD_MELODIC
+      when: always
+
 ddeploy:
   extends: .ddeploy
   rules:
diff --git a/ci-templates/core.yml b/ci-templates/core.yml
index 8cacbee..2c198d1 100644
--- a/ci-templates/core.yml
+++ b/ci-templates/core.yml
@@ -24,6 +24,7 @@ before_script:
 
 include:
   - ci-templates/syntax-check.yml
+  - ci-templates/python.yml
   - ci-templates/industrial-ci-templates.yml
 
 default:
diff --git a/ci-templates/python.yml b/ci-templates/python.yml
new file mode 100644
index 0000000..a9d1082
--- /dev/null
+++ b/ci-templates/python.yml
@@ -0,0 +1,63 @@
+
+.flake8_template:
+  stage: build
+  before_script: ${PYTHON_VERSION} -m pip -q install flake8
+  script: ${PYTHON_VERSION} -m flake8 --max-line-length 120 --statistics --show-source .
+
+
+.flake8_extended_template:
+  stage: .post
+  before_script:
+    - ${PYTHON_VERSION} -m pip install flake8 flake8-isort flake8-builtins
+    # The following flake8 extensions only work on python3
+    - ${PYTHON_VERSION} -m pip install flake8-eradicate flake8-functions-names flake8-return flake8-functions || true
+  script: ${PYTHON_VERSION} -m flake8 --max-line-length 120 --statistics --show-source .
+  allow_failure: true
+
+.security_template:
+  stage: .post
+  before_script:
+    - ${PYTHON_VERSION} -m pip install bandit safety
+  script:
+    - bandit -r . || RET=1
+    - safety check || RET=1
+    - exit $RET
+  when: always
+  allow_failure: true
+
+.py3_template:
+  image: python:3-alpine
+  variables:
+    PYTHON_VERSION: python3
+  when: never
+
+.py2_template:
+  image: python:2-alpine
+  variables:
+    PYTHON_VERSION: python2
+  when: always
+
+py3-flake8:
+    extends:
+      - .flake8_template
+      - .py3_template
+
+py2-flake8:
+    extends:
+      - .flake8_template
+      - .py2_template
+
+py3-flake8_extended:
+    extends:
+      - .flake8_extended_template
+      - .py3_template
+
+py2-flake8_extended:
+    extends:
+      - .flake8_extended_template
+      - .py2_template
+
+py3-security:
+    extends:
+      - .security_template
+      - .py3_template
\ No newline at end of file
diff --git a/ci-templates/syntax-check.yml b/ci-templates/syntax-check.yml
index 3c413c2..f834495 100644
--- a/ci-templates/syntax-check.yml
+++ b/ci-templates/syntax-check.yml
@@ -15,45 +15,6 @@ clang_format:
     CLANG_FORMAT_VERSION: "3.8"
   when: always
 
-##########################################################
-## Several Python checks
-##########################################################
-flake8:
-    stage: .post
-    image: python:3-alpine
-    before_script:
-        - pip3 install flake8
-    script:
-        - flake8 --max-line-length 120 --statistics --show-source .
-    tags:
-        - docker
-    when: always
-
-flake8_extended:
-    stage: .post
-    image: python:3-alpine
-    before_script:
-        - pip3 install flake8 flake8-isort flake8-builtins flake8-eradicate flake8-functions-names flake8-return flake8-functions
-    script:
-        - flake8 --max-line-length 120 --statistics --show-source .
-    tags:
-        - docker
-    when: always
-    allow_failure: true
-
-python_security:
-    stage: .post
-    image: python:3-alpine
-    before_script:
-        - pip3 install bandit safety
-    script:
-        - bandit -r . || RET=1
-        - safety check || RET=1
-        - exit $RET
-    tags:
-        - docker
-    when: always
-
 ##########################################################
 ## Check for errors in bash/sh scripts
 ## Uses https://github.com/koalaman/shellcheck
-- 
GitLab