Fix Python linter warnings

This commit is contained in:
cytopia
2022-12-08 12:41:44 +01:00
parent b724ceaca3
commit 39db14d294
4 changed files with 50 additions and 102 deletions

View File

@@ -23,7 +23,7 @@ MYPY_ARGS = --strict --disable-error-code no-any-return
PYLINT_DIR = *.py
PYLINT_PIP_PKGS = yamllint
PYLINT_ARGS = --disable=invalid-name,duplicate-code
PYLINT_ARGS = --disable=invalid-name,duplicate-code,consider-using-f-string
PYCODE_ARGS = --max-line-length=100

View File

@@ -4,7 +4,6 @@
import os
import re
import sys
from collections import OrderedDict
from typing import Dict, List, Any
import yaml
@@ -19,44 +18,30 @@ PHP_TOOL_PATH = str(os.path.join(REPOSITORY_PATH, "php_tools"))
DOC_FILE = str(os.path.join(REPOSITORY_PATH, "doc", "available-tools.md"))
PHP_VERSIONS = ["5.2", "5.3", "5.4", "5.5", "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]
PHP_VERSIONS = [
"5.2",
"5.3",
"5.4",
"5.5",
"5.6",
"7.0",
"7.1",
"7.2",
"7.3",
"7.4",
"8.0",
"8.1",
"8.2",
]
DEFAULT_TOOLS = [
{
"name": "**composer**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
},
{
"name": "**corepack**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
},
{
"name": "**nvm**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
},
{
"name": "**npm**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
},
{
"name": "**node**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
},
{
"name": "**yarn**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
},
{
"name": "**pip**",
"dir": "../.ansible/group_vars/all/work-help.yml",
"exclude": []
}
{"name": "**composer**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
{"name": "**corepack**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
{"name": "**nvm**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
{"name": "**npm**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
{"name": "**node**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
{"name": "**yarn**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
{"name": "**pip**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []},
]
@@ -117,10 +102,9 @@ def get_tools() -> List[Dict[str, Any]]:
{
"dir": "../php_tools/" + item.name,
"name": data["name"],
"exclude": [str(x) for x in data["exclude"]]
"exclude": [str(x) for x in data["exclude"]],
}
)
#return tools
return sorted(DEFAULT_TOOLS + tools, key=lambda tool: tool["name"].replace("*", ""))
@@ -133,16 +117,16 @@ def print_terminal(tools: List[Dict[str, Any]]) -> None:
"""Print directory tools."""
padding = 15
# First Row
print('| {name: <{padding}}| '.format(name="Tool", padding=padding), end="")
print("| {name: <{padding}}| ".format(name="Tool", padding=padding), end="")
print(" | ".join(PHP_VERSIONS), end="")
print(" |")
# Second Row
print('|{name:-<{padding}}-|'.format(name="", padding=padding), end="")
print("|{name:-<{padding}}-|".format(name="", padding=padding), end="")
for php in PHP_VERSIONS:
print("-----|", end="")
print()
for tool in tools:
print('| {name: <{padding}}|'.format(name=tool["name"], padding=padding), end="")
print("| {name: <{padding}}|".format(name=tool["name"], padding=padding), end="")
for php in PHP_VERSIONS:
if str(php) in tool["exclude"]:
print(" |", end="")
@@ -151,21 +135,23 @@ def print_terminal(tools: List[Dict[str, Any]]) -> None:
print()
def get_markdown(tools: List[Dict[str, Any]]) -> None:
"""Print directory tools."""
def get_markdown(tools: List[Dict[str, Any]]) -> str:
"""Get markdown tools table."""
padding = 35
# First Row
markdown = '| {name: <{padding}}| '.format(name="Tool", padding=padding)
markdown = "| {name: <{padding}}| ".format(name="Tool", padding=padding)
markdown += " | ".join(PHP_VERSIONS)
markdown += " |\n"
# Second Row
markdown += '|{name:-<{padding}}-|'.format(name="", padding=padding)
markdown += "|{name:-<{padding}}-|".format(name="", padding=padding)
for php in PHP_VERSIONS:
markdown += "-----|"
markdown += "\n"
for tool in tools:
markdown += '| {name: <{padding}}|'.format(name="["+tool["name"]+"][lnk_"+tool["name"]+"]", padding=padding)
markdown += "| {name: <{padding}}|".format(
name="[" + tool["name"] + "][lnk_" + tool["name"] + "]", padding=padding
)
for php in PHP_VERSIONS:
if str(php) in tool["exclude"]:
markdown += " |"
@@ -180,57 +166,13 @@ def get_markdown(tools: List[Dict[str, Any]]) -> None:
return markdown
# --------------------------------------------------------------------------------------------------
# MAIN FUNCTION
# --------------------------------------------------------------------------------------------------
def print_help() -> None:
"""Show help screen."""
print("Usage:", os.path.basename(__file__), "[options] [PHP-TOOL]...")
print(" ", os.path.basename(__file__), "-h, --help")
print()
print("This script will generate the Ansible group_vars file: .ansible/group_vars/all/work.yml")
print("based on all the tools found in php_tools/ directory.")
print()
print("Positional arguments:")
print(" [PHP-TOOL] Specify None, one or more PHP tools to generate group_vars for.")
print(" When no PHP tool is specified (argument is omitted), group_vars")
print(" for all tools will be genrated.")
print(" When one or more PHP tool are specified, only group_vars for")
print(" these tools will be created.")
print(" only be generated for this single tool (and its dependencies).")
print(" This is useful if you want to test new tools and not build all")
print(" previous tools in the Dockerfile.")
print()
print(" Note: You still need to generate the Dockerfiles via Ansible for")
print(" the changes to take effect, before building the image.")
print("Optional arguments:")
print(" -i Ignore dependent tools.")
print(" By default each tool is checked for dependencies of other")
print(" tools.")
print(" By specifying -i, those dependent tools are not beeing added to")
print(" ansible group_vars. Use at your own risk.")
def main(argv: List[str]) -> None:
def main() -> None:
"""Main entrypoint."""
ignore_dependencies = False
selected_tools = []
if len(argv):
for arg in argv:
if arg in ("-h", "--help"):
print_help()
sys.exit(0)
for arg in argv:
if arg.startswith("-") and arg != "-i":
print("Invalid argument:", arg)
print("Use -h or --help for help")
sys.exit(1)
if arg == "-i":
ignore_dependencies = True
else:
selected_tools.append(arg)
# Get tools in order of dependencies
tools = get_tools()
@@ -246,12 +188,18 @@ def main(argv: List[str]) -> None:
markdown = get_markdown(tools)
print()
with open(DOC_FILE, "r") as f:
with open(DOC_FILE, "r", encoding="utf8") as f:
content = f.read()
content_new = re.sub(r'(\<\!\-\- TOOLS_WORK_START \-\-\>)(.*)(\<\!\-\- TOOLS_WORK_END \-\-\>)', r"\1\n\n"+markdown+r"\n\3", content, flags = re.DOTALL)
content_new = re.sub(
r"(\<\!\-\- TOOLS_WORK_START \-\-\>)(.*)(\<\!\-\- TOOLS_WORK_END \-\-\>)",
r"\1\n\n" + markdown + r"\n\3",
content,
flags=re.DOTALL,
)
with open(DOC_FILE, "w") as f:
with open(DOC_FILE, "w", encoding="utf8") as f:
f.write(content_new)
if __name__ == "__main__":
main(sys.argv[1:])
main()

View File

@@ -91,7 +91,7 @@ def get_modules(selected_modules: List[str], ignore_dependencies: bool) -> List[
"dir": item.name,
"name": data["name"],
"deps": data["depends_build"],
"exclude": data["exclude"]
"exclude": data["exclude"],
}
)
# Convert list of deps into dict(dir, name, deps)

View File

@@ -86,7 +86,7 @@ def get_tools(selected_tools: List[str], ignore_dependencies: bool) -> List[Dict
"dir": item.name,
"name": data["name"],
"deps": data["depends"],
"exclude": data["exclude"]
"exclude": data["exclude"],
}
)
# Convert list of deps into dict(dir, name, deps)