From bb1e428c44573f3fe5ee521a2d34a958c6170ad7 Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Thu, 11 May 2023 12:27:15 -0700 Subject: [PATCH] Transition to Ruff (#324) Transitions project maintenance, building, and distribution to the pyproject.toml file. This also allows many of the python configuration files to be moved into the pyproject.toml file themselves. Updates CI yaml steps (azure pipelines and github action) to download pip requirements via the pyproject.toml rather then a requirements file. --- .githooks/advanced-pre-commit-config.yaml | 8 ++++---- .githooks/basic-pre-commit-config.yaml | 8 ++++---- .github/workflows/CIRunner.yml | 8 ++------ .gitignore | 1 + .vscode/extensions.json | 3 +++ .vscode/settings.json | 9 ++++++--- ...authenticated_variables_structure_support.py | 12 ++++++------ edk2toollib/uefi/edk2/guid_list.py | 4 ++-- edk2toollib/uefi/edk2/parsers/guid_parser.py | 2 +- edk2toollib/utility_functions.py | 3 +++ pyproject.toml | 17 ++++++++++------- 11 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 .vscode/extensions.json diff --git a/.githooks/advanced-pre-commit-config.yaml b/.githooks/advanced-pre-commit-config.yaml index 78b92b5e..d40e5d19 100644 --- a/.githooks/advanced-pre-commit-config.yaml +++ b/.githooks/advanced-pre-commit-config.yaml @@ -13,11 +13,11 @@ repos: hooks: - id: cspell types_or: [markdown, python] -- repo: https://github.com/PyCQA/flake8 - rev: 3.8.4 +- repo: https://github.com/charliermarsh/ruff-pre-commit + rev: "v0.0.265" hooks: - - id: flake8 - entry: flake8 edk2toollib + - id: ruff + args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/igorshubovych/markdownlint-cli rev: v0.12.0 hooks: diff --git a/.githooks/basic-pre-commit-config.yaml b/.githooks/basic-pre-commit-config.yaml index 45cccf40..bfc58dd8 100644 --- a/.githooks/basic-pre-commit-config.yaml +++ b/.githooks/basic-pre-commit-config.yaml @@ -13,11 +13,11 @@ repos: hooks: - id: cspell types_or: [markdown, python] -- repo: https://github.com/PyCQA/flake8 - rev: 3.8.4 +- repo: https://github.com/charliermarsh/ruff-pre-commit + rev: "v0.0.265" hooks: - - id: flake8 - entry: flake8 edk2toollib + - id: ruff + args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/igorshubovych/markdownlint-cli rev: v0.12.0 hooks: diff --git a/.github/workflows/CIRunner.yml b/.github/workflows/CIRunner.yml index 13135c6b..f9ba9b63 100644 --- a/.github/workflows/CIRunner.yml +++ b/.github/workflows/CIRunner.yml @@ -52,9 +52,9 @@ jobs: npm install -g markdownlint-cli@0.32.2 npm install -g cspell@5.20.0 - - name: Run flake8 + - name: Run ruff if: success() || failure() - run: flake8 ${{ inputs.package-src }} + run: ruff check ${{ inputs.package-src }} --format=github - name: Run markdownlint if: success() || failure() @@ -64,10 +64,6 @@ jobs: if: success() || failure() run: cspell -c .cspell.json "**/*.py" "**/*.md" - - name: Run pydocstyle - if: success() || failure() - run: pydocstyle ${{ inputs.package-src }} - - name: Run mkdocs build if: success() || failure() run: mkdocs build --strict diff --git a/.gitignore b/.gitignore index 81673566..55e61a34 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ build. flake8.err.log /.eggs /htmlcov +/.ruff_cache diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..76e7e143 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["charliermarsh.ruff"] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 779dc499..8b9b3ca5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,7 +4,10 @@ "${workspaceRoot}/edk2toollib/tests", ], "python.testing.unittestEnabled": false, - "python.linting.flake8Enabled": true, - "python.linting.enabled": true, - "python.linting.pydocstyleEnabled": true + "[python]": { + "editor.defaultFormatter": null, + "editor.codeActionsOnSave": { + "source.fixAll.ruff": true, + } + } } diff --git a/edk2toollib/uefi/authenticated_variables_structure_support.py b/edk2toollib/uefi/authenticated_variables_structure_support.py index f5e311ad..9e45df56 100644 --- a/edk2toollib/uefi/authenticated_variables_structure_support.py +++ b/edk2toollib/uefi/authenticated_variables_structure_support.py @@ -685,12 +685,12 @@ def GetCanonicalAndDupes(self): Returns: (Tuple[EfiSignatureDatabase, EfiSignatureDatabase]): (canonical, duplicates) - NOTE: - canonical is an EfiSignatureDatabase where EfiSignatureLists are merged (where possible), - deduplicated, & sorted, and the EfiSignatureData elements are also deduplicated & sorted - duplicates is an EfiSignatureDatabase with EfiSignatureLists containing any duplicated - EfiSignatureData entries (only the data contents are checked for effective equality, - signature owner is ignored) + !!! note + canonical is an EfiSignatureDatabase where EfiSignatureLists are merged (where possible), + deduplicated, & sorted, and the EfiSignatureData elements are also deduplicated & sorted + duplicates is an EfiSignatureDatabase with EfiSignatureLists containing any duplicated + EfiSignatureData entries (only the data contents are checked for effective equality, + signature owner is ignored) """ # First group EFI_SIGNATURE_LISTS by type, merging them where possible diff --git a/edk2toollib/uefi/edk2/guid_list.py b/edk2toollib/uefi/edk2/guid_list.py index 96b515a9..63111f8d 100644 --- a/edk2toollib/uefi/edk2/guid_list.py +++ b/edk2toollib/uefi/edk2/guid_list.py @@ -113,7 +113,7 @@ def parse_guids_from_dec(stream, filename: str) -> list: try: results.append(GuidListEntry(dec.Dict["PACKAGE_NAME"], dec.Dict["PACKAGE_GUID"], filename)) - except: + except Exception: logging.warning("Failed to find Package Guid from dec file: " + filename) return results @@ -131,6 +131,6 @@ def parse_guids_from_inf(filename: str) -> list: inf.ParseFile(filename) try: return [GuidListEntry(inf.Dict["BASE_NAME"], inf.Dict["FILE_GUID"].upper(), filename)] - except: + except Exception: logging.warning("Failed to find info from INF file: " + filename) return [] diff --git a/edk2toollib/uefi/edk2/parsers/guid_parser.py b/edk2toollib/uefi/edk2/parsers/guid_parser.py index 9eb4367b..89641210 100644 --- a/edk2toollib/uefi/edk2/parsers/guid_parser.py +++ b/edk2toollib/uefi/edk2/parsers/guid_parser.py @@ -99,7 +99,7 @@ def reg_guid_from_c_format(cls, guidstring: str) -> str: int(guidValueList[9], 16), int(guidValueList[10], 16) ) - except: + except Exception: return '' @classmethod diff --git a/edk2toollib/utility_functions.py b/edk2toollib/utility_functions.py index f5c3d468..e652d203 100644 --- a/edk2toollib/utility_functions.py +++ b/edk2toollib/utility_functions.py @@ -430,6 +430,7 @@ def hexdump(byte_list, offset_start=0, out_fs=sys.stdout, **kwargs) -> None: byte_list (bytearray): byte array to print offset_start (int): offset to print to the side of the hexdump out_fs (io.BytesIO): output file stream to print to + kwargs (any): keyword arguments expanded below. Keyword Arguments: include_ascii (bool): Option (Default: True) to include ascii @@ -496,6 +497,7 @@ def export_c_type_array(buffer_fs, variable_name, out_fs, **kwargs) -> None: buffer_fs (io.BytesIO): buffer file stream to turn into a C style array variable_name (str): variable name to use for the C style array out_fs (io.StringIO): output filestream to write to + kwargs (any): keyword arguments expanded below. Keyword Arguments: data_type (str): The datatype of the array (Default: UINT8) @@ -507,6 +509,7 @@ def export_c_type_array(buffer_fs, variable_name, out_fs, **kwargs) -> None: include_ascii (bool): includes a ascii comment to side of hex include_length (bool): includes length in the decleration of array (ex. "UINT8 TestVariable[13] = {") + Return: None diff --git a/pyproject.toml b/pyproject.toml index bb83a9aa..3bdd4473 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,11 +28,9 @@ issues = "https://github.com/tianocore/edk2-pytool-library/issues/" [project.optional-dependencies] dev = [ - "flake8 == 6.0.0", - "flake8-pyproject == 1.2.3", + "ruff == 0.0.265", "pytest == 7.3.1", "coverage == 7.2.5", - "pydocstyle == 6.3.0", ] publish = [ "setuptools == 67.7.2", @@ -56,11 +54,16 @@ docs = [ [tool.coverage.run] include = ["edk2toollib/*"] -[tool.flake8] -ignore = "E266,E722,W503" -max_line_length = 120 +[tool.ruff] +src = ["edk2toollib"] +select = [ + "E", # pycodestyle errors + "F", # PyFlakes + "D", # pydocstyle +] +line-length = 120 -[tool.pydocstyle] +[tool.ruff.pydocstyle] convention = "google" [tool.pytest.ini_options]