diff --git a/.zuul.yaml b/.zuul.yaml index dc2ed04a7..9e266cc8d 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -11,12 +11,14 @@ - stx-distcloud-tox-pep8 - stx-distcloud-tox-py39 - stx-distcloud-tox-pylint + - stx-distcloud-tox-black gate: jobs: - openstack-tox-linters - stx-distcloud-tox-pep8 - stx-distcloud-tox-py39 - stx-distcloud-tox-pylint + - stx-distcloud-tox-black post: jobs: - stx-distcloud-upload-git-mirror @@ -69,6 +71,16 @@ tox_envlist: pep8 tox_extra_args: -c distributedcloud/tox.ini +- job: + name: stx-distcloud-tox-black + parent: tox + description: Run black for distcloud + nodeset: debian-bullseye + vars: + python_version: 3.9 + tox_envlist: black + tox_extra_args: -c distributedcloud/tox.ini + - job: name: stx-distcloud-upload-git-mirror parent: upload-git-mirror diff --git a/distributedcloud/run_black.py b/distributedcloud/run_black.py new file mode 100644 index 000000000..df810aa74 --- /dev/null +++ b/distributedcloud/run_black.py @@ -0,0 +1,43 @@ +# noqa: H102 + +import subprocess +import sys + +# List of module directories to check +modules = [ + "dccommon", + "dcdbsync/api", + "dcdbsync", + "dcorch/api", + "dcorch/common", + "dcorch/db", + "dcorch/engine", + "dcorch", + "dcmanager/api", + "dcmanager/audit", + "dcmanager/common", + "dcmanager/db", + "dcmanager/orchestrator", + "dcmanager/tests", + "dcmanager", +] + +# List of modules that are already formatted with black +formatted_modules = [] + + +# Function to run black check +def run_black_check(module): + try: + subprocess.run(["black", "--check", "--quiet", f"./{module}"], check=True) + print(f"Black check passed for {module}") + except subprocess.CalledProcessError as e: + print(f"Black check failed for {module}") + # If the module is in formatted_modules, stx-distcloud-tox-black will fail + if module in formatted_modules: + sys.exit(e.returncode) + + +# Iterate over modules and run black check +for module in modules: + run_black_check(module) diff --git a/distributedcloud/tox.ini b/distributedcloud/tox.ini index 6e0d92927..cb43ce5f3 100644 --- a/distributedcloud/tox.ini +++ b/distributedcloud/tox.ini @@ -1,6 +1,6 @@ [tox] -envlist = py39,pep8,pylint -minversion = 2.3 +envlist = py39,pep8,pylint,black +minversion = 4.4.0 skipsdist = True # move tox work directory out of the source tree @@ -20,10 +20,6 @@ tsconfig_src_dir = {[dc]stx_config_dir}/tsconfig/tsconfig [testenv] basepython = python3.9 sitepackages = False -install_command = pip install -v -v -v \ - -c https://opendev.org/starlingx/root/raw/branch/master/build-tools/requirements/debian/upper-constraints.txt \ - {opts} {packages} - setenv = VIRTUAL_ENV={envdir} OS_STDOUT_CAPTURE=1 @@ -32,8 +28,8 @@ setenv = CURRENT_CFG_FILE={toxinidir}/.current.cfg SINGLE_REPO=True OSLO_LOCK_PATH={toxinidir} - deps = + -c{env:UPPER_CONSTRAINTS_FILE:https://opendev.org/starlingx/root/raw/branch/master/build-tools/requirements/debian/upper-constraints.txt} -r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt -e{[dc]cgcs_patch_src_dir} @@ -94,3 +90,16 @@ setenv = PYTHONPATH = {toxinidir} commands = pylint {posargs} dccommon dcdbsync dcmanager dcorch --rcfile=./.pylintrc + +[testenv:black] +# This environment checks and displays the recommended changes by Black for formatting +# Python files. +# To apply the format in a specific file, use the following command: black +deps = + black>=23.11.0 # MIT +# Note: After all files being formatted by Black, the following command should be used +# as default for the black environment and run_black.py script should be deleted. +; commands = +; black --check --diff . +commands = + python run_black.py diff --git a/tox.ini b/tox.ini index 0f9828802..eadfadf22 100644 --- a/tox.ini +++ b/tox.ini @@ -27,6 +27,9 @@ description = Dummy environment to allow pep8 to be run in subdir tox [testenv:pylint] description = Dummy environment to allow pylint to be run in subdir tox +[testenv:black] +description = Dummy environment to allow black to be run in subdir tox + [testenv:docs] deps = -r{toxinidir}/doc/requirements.txt commands =