Create stx-distcloud-tox-black job

This commit introduces a new Zuul job, stx-distcloud-tox-black,
and a corresponding black_check tox environment to ensure Python
files are formatted with Black.

Initially, the black_check environment will check the formatting
of specified modules[1]. As modules are progressively formatted, the
configuration will be updated to enforce Black formatting on these
modules. During this transition, the job will only fail for modules
that have been updated to comply with Black formatting. Eventually,
the environment will be configured to check the entire project,
ensuring consistent code style across all files.

1: Check `modules` in run_black.py script.

Note: Once all modules are formatted, run_black.py will be deleted.

Test Plan:
PASS - Success in stx-distcloud-tox-black

Story: 2011149
Task: 50432

Change-Id: I8a8044d44b85679cf5e71106784c192ef343f5b6
Signed-off-by: Hugo Brito <hugo.brito@windriver.com>
This commit is contained in:
Hugo Brito 2024-06-25 14:05:48 -03:00
parent 222ba53911
commit 75cb349622
4 changed files with 74 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 <file_path>
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

View File

@ -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 =