diff --git a/c2cciutils/__init__.py b/c2cciutils/__init__.py index bf2565953..49b924aec 100644 --- a/c2cciutils/__init__.py +++ b/c2cciutils/__init__.py @@ -1,6 +1,4 @@ -""" -c2cciutils shared utils function. -""" +"""c2cciutils shared utils function.""" import glob import json @@ -18,9 +16,7 @@ def get_repository() -> str: - """ - Get the current GitHub repository like `organization/project`. - """ + """Get the current GitHub repository like `organization/project`.""" if "GITHUB_REPOSITORY" in os.environ: return os.environ["GITHUB_REPOSITORY"] @@ -44,11 +40,12 @@ def merge(default_config: Any, config: Any) -> Any: Arguments: default_config: The default config that will be applied config: The base config, will be modified + """ if not isinstance(default_config, dict) or not isinstance(config, dict): return config - for key in default_config.keys(): + for key in default_config: if key not in config: config[key] = default_config[key] else: @@ -73,9 +70,7 @@ def get_master_branch(repo: list[str]) -> tuple[str, bool]: def get_config() -> c2cciutils.configuration.Configuration: - """ - Get the configuration, with project and auto detections. - """ + """Get the configuration, with project and auto detections.""" config: c2cciutils.configuration.Configuration = {} if os.path.exists("ci/config.yaml"): with open("ci/config.yaml", encoding="utf-8") as open_file: @@ -155,6 +150,7 @@ def error( line: The line number of the error col: The column number of the error error_type: The kind of error (error or warning) + """ result = "" on_ci = os.environ.get("CI", "false").lower() == "true" @@ -197,6 +193,7 @@ def compile_re(config: c2cciutils.configuration.VersionTransform, prefix: str = prefix: The version prefix Return the compiled transform config. + """ result = [] for conf in config: @@ -226,6 +223,7 @@ def match( Returns the re match object, the matched config and the value as a tuple On no match it returns None, value + """ for conf in config: matched = conf["from"].match(value) @@ -243,6 +241,7 @@ def does_match(value: str, config: list[VersionTransform]) -> bool: config: The result of `compile` Returns True it it does match else False + """ matched, _, _ = match(value, config) return matched is not None @@ -262,6 +261,7 @@ def get_value(matched: Optional[Match[str]], config: Optional[VersionTransform], value: The default value on returned no match Return the value + """ return matched.expand(config.get("to", r"\1")) if matched is not None and config is not None else value @@ -272,6 +272,7 @@ def print_versions(config: c2cciutils.configuration.PrintVersions) -> bool: Arguments: config: The print configuration + """ for version in config.get("versions", c2cciutils.configuration.PRINT_VERSIONS_VERSIONS_DEFAULT): try: @@ -310,6 +311,7 @@ def gopass(key: str, default: Optional[str] = None) -> Optional[str]: default: the value to return if gopass is not found Return the value + """ try: return subprocess.check_output(["gopass", "show", key]).strip().decode() @@ -326,6 +328,7 @@ def gopass_put(secret: str, key: str) -> None: Arguments: secret: The secret value key: The key + """ subprocess.check_output(["gopass", "insert", "--force", key], input=secret.encode()) @@ -338,6 +341,7 @@ def add_authorization_header(headers: dict[str, str]) -> dict[str, str]: headers: The headers Return the headers (to be chained) + """ try: token = ( @@ -375,6 +379,7 @@ def graphql(query_file: str, variables: dict[str, Any], default: Any = None) -> Return the data result In case of error it throw an exception + """ with open(os.path.join(os.path.dirname(__file__), query_file), encoding="utf-8") as query_open: query = query_open.read() diff --git a/c2cciutils/applications_definition.py b/c2cciutils/applications_definition.py index 49fec80e6..a0f63c265 100644 --- a/c2cciutils/applications_definition.py +++ b/c2cciutils/applications_definition.py @@ -1,6 +1,4 @@ -""" -Automatically generated file from a JSON schema. -""" +"""Automatically generated file from a JSON schema.""" from typing import Literal, TypedDict, Union diff --git a/c2cciutils/configuration.py b/c2cciutils/configuration.py index c8102f522..87c578587 100644 --- a/c2cciutils/configuration.py +++ b/c2cciutils/configuration.py @@ -1,6 +1,4 @@ -""" -Automatically generated file from a JSON schema. -""" +"""Automatically generated file from a JSON schema.""" from typing import Any, Literal, TypedDict, Union @@ -721,7 +719,7 @@ class _PrintVersionsVersionsItem(TypedDict, total=False): class _PublishDockerConfigSnyk(TypedDict, total=False): - """Checks the published images with Snyk""" + """Checks the published images with Snyk.""" monitor_args: Union["_PublishDockerSnykMonitorArgsOneof0", "_PublishDockerSnykMonitorArgsOneof1"] """ diff --git a/c2cciutils/env.py b/c2cciutils/env.py index 187463caa..2f76b4b09 100644 --- a/c2cciutils/env.py +++ b/c2cciutils/env.py @@ -10,9 +10,7 @@ class PrintVersions: - """ - Print some tools versions. - """ + """Print some tools versions.""" def __init__(self, config: c2cciutils.configuration.PrintVersions) -> None: """Construct.""" @@ -24,9 +22,7 @@ def __call__(self) -> None: class PrintConfig: - """ - Print the configuration. - """ + """Print the configuration.""" def __init__(self, config: c2cciutils.configuration.Configuration) -> None: """Construct.""" @@ -40,49 +36,37 @@ def __call__(self) -> None: def print_environment_variables() -> None: - """ - Print the environment variables. - """ + """Print the environment variables.""" for name, value in sorted(os.environ.items()): if name != "GITHUB_EVENT": print(f"{name}: {value}") def print_github_event_file() -> None: - """ - Print the GitHub event file. - """ + """Print the GitHub event file.""" if "GITHUB_EVENT_PATH" in os.environ: with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as event: print(event.read()) def print_github_event_object() -> None: - """ - Print the GitHub event object. - """ + """Print the GitHub event object.""" github_event = json.loads(os.environ["GITHUB_EVENT"]) print(yaml.dump(github_event, indent=2)) def print_python_package_version() -> None: - """ - Print the version of the Python packages. - """ + """Print the version of the Python packages.""" subprocess.run(["python3", "-m", "pip", "freeze", "--all"]) # pylint: disable=subprocess-run-check def print_node_package_version() -> None: - """ - Print the version of the Python packages. - """ + """Print the version of the Python packages.""" subprocess.run(["npm", "list", "--global"]) # pylint: disable=subprocess-run-check def print_debian_package_version() -> None: - """ - Print the version of the Python packages. - """ + """Print the version of the Python packages.""" subprocess.run(["dpkg", "--list"]) # pylint: disable=subprocess-run-check diff --git a/c2cciutils/lib/docker.py b/c2cciutils/lib/docker.py index ca0c92b90..50f9b898c 100644 --- a/c2cciutils/lib/docker.py +++ b/c2cciutils/lib/docker.py @@ -1,6 +1,4 @@ -""" -Some utility functions for Docker images. -""" +"""Some utility functions for Docker images.""" import os import subprocess # nosec: B404 @@ -102,9 +100,7 @@ def get_dpkg_packages_versions( def get_versions_config() -> tuple[dict[str, dict[str, str]], bool]: - """ - Get the versions from the config file. - """ + """Get the versions from the config file.""" if os.path.exists("ci/dpkg-versions.yaml"): with open("ci/dpkg-versions.yaml", encoding="utf-8") as versions_file: return ( diff --git a/c2cciutils/lib/oidc.py b/c2cciutils/lib/oidc.py index 82abd5303..84a2f9513 100755 --- a/c2cciutils/lib/oidc.py +++ b/c2cciutils/lib/oidc.py @@ -154,8 +154,10 @@ def pypi_login() -> None: Connect to PyPI using OpenID Connect and mint a token for the user. See Also + -------- - https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/about-security-hardening-with-openid-connect - https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-pypi + """ pypirc_filename = os.path.expanduser("~/.pypirc") diff --git a/c2cciutils/publish.py b/c2cciutils/publish.py index 3ae07c5e6..734801971 100644 --- a/c2cciutils/publish.py +++ b/c2cciutils/publish.py @@ -1,6 +1,4 @@ -""" -The publishing functions. -""" +"""The publishing functions.""" import argparse import datetime @@ -10,11 +8,11 @@ import re import subprocess # nosec import sys -import tomllib import uuid from typing import Optional import ruamel.yaml +import tomllib from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow @@ -24,15 +22,11 @@ class GoogleCalendar: - """ - Interact with the Google Calendar API. - """ + """Interact with the Google Calendar API.""" # pylint: disable=too-many-instance-attributes def __init__(self) -> None: - """ - Initialize. - """ + """Initialize.""" self.scopes = ["https://www.googleapis.com/auth/calendar"] # in fact it is better to hard-code this self.credentials_pickle_file = os.environ.get("TMP_CREDS_FILE", f"/tmp/{uuid.uuid4()}.pickle") self.credentials_json_file = os.environ.get( @@ -62,9 +56,7 @@ def __init__(self) -> None: self.service = build("calendar", "v3", credentials=self.creds) def init_calendar_service(self) -> Credentials: # type: ignore - """ - Initialize the calendar service. - """ + """Initialize the calendar service.""" # The file token pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. @@ -95,9 +87,7 @@ def init_calendar_service(self) -> Credentials: # type: ignore pickle.dump(creds, token) def _update_creds(self) -> None: - """ - Update the credentials. - """ + """Update the credentials.""" self.client_id = self.creds.client_id self.client_secret = self.creds.client_secret self.token = self.creds.token @@ -105,9 +95,7 @@ def _update_creds(self) -> None: self.refresh_token = self.creds.refresh_token def print_all_calendars(self) -> None: - """ - Print all calendar events. - """ + """Print all calendar events.""" # list all the calendars that the user has access to. # used to debug credentials print("Getting list of calendars") @@ -129,6 +117,7 @@ def print_latest_events(self, time_min: Optional[datetime.datetime] = None) -> N Arguments: time_min: The time to be considered. + """ now = datetime.datetime.utcnow() if not time_min: @@ -163,6 +152,7 @@ def create_event( Arguments: summary: The event summary description: The event description + """ now = datetime.datetime.now() start = now.isoformat() @@ -178,9 +168,7 @@ def create_event( print(f"Created event with id: {event_result['id']}") def save_credentials_to_gopass(self) -> None: - """ - Save the calendar credentials to gopass. - """ + """Save the calendar credentials to gopass.""" objects_to_save = { "gs/ci/google_calendar/calendarId": self.calendar_id, "gs/ci/google_calendar/token": self.token, @@ -194,17 +182,13 @@ def save_credentials_to_gopass(self) -> None: c2cciutils.gopass_put(secret, key) def __del__(self) -> None: - """ - Delete the credentials file. - """ + """Delete the credentials file.""" if os.path.exists(self.credentials_pickle_file): os.remove(self.credentials_pickle_file) def main_calendar() -> None: - """ - Run the calendar main function. - """ + """Run the calendar main function.""" parser = argparse.ArgumentParser( description="Interact with google API for the Docker publishing calendar" ) @@ -254,6 +238,7 @@ def pip( version_branch, feature_branch, feature_tag (for pull request) publish: If False only check the package package: The package configuration + """ print(f"::group::{'Publishing' if publish else 'Checking'} '{package.get('path')}' to pypi") sys.stdout.flush() @@ -352,6 +337,7 @@ def docker( tag_src: The source tag (usually latest) dst_tags: Publish using the provided tags images_full: The list of published images (with tag), used to build the dispatch event + """ print( f"::group::Publishing {image_config['name']} to the server {name} using the tags {', '.join(dst_tags)}" @@ -411,6 +397,7 @@ def helm(folder: str, version: str, owner: str, repo: str, commit_sha: str, toke repo: The GitHub repository name commit_sha: The sha of the current commit token: The GitHub token + """ print(f"::group::Publishing Helm chart from '{folder}' to GitHub release") sys.stdout.flush() diff --git a/c2cciutils/scripts/__init__.py b/c2cciutils/scripts/__init__.py index acef9dd6c..4db9243af 100644 --- a/c2cciutils/scripts/__init__.py +++ b/c2cciutils/scripts/__init__.py @@ -1,3 +1 @@ -""" -The scripts main functions. -""" +"""The scripts main functions.""" diff --git a/c2cciutils/scripts/clean.py b/c2cciutils/scripts/clean.py index b90d089c1..744eec4c3 100644 --- a/c2cciutils/scripts/clean.py +++ b/c2cciutils/scripts/clean.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -""" -The clean main function. -""" +"""The clean main function.""" import argparse import json @@ -24,6 +22,7 @@ def clean(image: str, tag: str, token: str) -> None: image: The image name that should be deleted (/) tag: The tag that should be deleted token: The token used to be authenticated on Docker hub + """ print(f"Delete image '{image}:{tag}'.") @@ -51,9 +50,7 @@ def clean(image: str, tag: str, token: str) -> None: def main() -> None: - """ - Run the main function. - """ + """Run the main function.""" parser = argparse.ArgumentParser( description=( "Clean the Docker images on Docker Hub for the branch we delete " @@ -87,10 +84,7 @@ def main() -> None: with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as event_file: event = json.loads(event_file.read()) print(yaml.dump(event)) - if "pull_request" in event: - ref = str(event["number"]) - else: - ref = event["ref"] + ref = str(event["number"]) if "pull_request" in event else event["ref"] ref = ref.replace("/", "_") diff --git a/c2cciutils/scripts/env.py b/c2cciutils/scripts/env.py index 04fa0a006..25f438862 100644 --- a/c2cciutils/scripts/env.py +++ b/c2cciutils/scripts/env.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -""" -The checker main function. -""" +"""The checker main function.""" import argparse @@ -10,9 +8,7 @@ def main() -> None: - """ - Run the checks. - """ + """Run the checks.""" parser = argparse.ArgumentParser(description="Print the environment information.") parser.parse_args() diff --git a/c2cciutils/scripts/k8s/__init__.py b/c2cciutils/scripts/k8s/__init__.py index 49abee23f..530aff901 100644 --- a/c2cciutils/scripts/k8s/__init__.py +++ b/c2cciutils/scripts/k8s/__init__.py @@ -1,3 +1 @@ -""" -The commands that can be used to test a chart on Kubernetes. -""" +"""The commands that can be used to test a chart on Kubernetes.""" diff --git a/c2cciutils/scripts/main.py b/c2cciutils/scripts/main.py index c2f01de40..3d35aa501 100644 --- a/c2cciutils/scripts/main.py +++ b/c2cciutils/scripts/main.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -""" -The main function of some utilities. -""" +"""The main function of some utilities.""" import argparse @@ -13,9 +11,7 @@ def main() -> None: - """ - Run the utilities. - """ + """Run the utilities.""" parser = argparse.ArgumentParser(description="Some utils of c2cciutils.") parser.add_argument("--get-config", action="store_true", help="display the current config") parser.add_argument("--version", action="store_true", help="display the current version") diff --git a/c2cciutils/scripts/publish.py b/c2cciutils/scripts/publish.py index f3db17aa1..4d388c1ff 100644 --- a/c2cciutils/scripts/publish.py +++ b/c2cciutils/scripts/publish.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -""" -The publish script. -""" +"""The publish script.""" import argparse import os @@ -34,6 +32,7 @@ def match(tpe: str, base_re: str) -> Optional[Match[str]]: Arguments: tpe: The type of ref we want to match (heads, tag, ...) base_re: The regular expression to match the value + """ if base_re[0] == "^": base_re = base_re[1:] @@ -50,6 +49,7 @@ def to_version(full_config: c2cciutils.configuration.Configuration, value: str, full_config: The full configuration value: The value to be transformed kind: The name of the transformer in the configuration + """ item_re = c2cciutils.compile_re( cast( @@ -63,9 +63,7 @@ def to_version(full_config: c2cciutils.configuration.Configuration, value: str, def main() -> None: - """ - Run the publish. - """ + """Run the publish.""" parser = argparse.ArgumentParser(description="Publish the project.") parser.add_argument("--group", default="default", help="The publishing group") parser.add_argument("--version", help="The version to publish to") diff --git a/c2cciutils/scripts/trigger_image_update.py b/c2cciutils/scripts/trigger_image_update.py index 23f7f2877..c81850b09 100644 --- a/c2cciutils/scripts/trigger_image_update.py +++ b/c2cciutils/scripts/trigger_image_update.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -""" -Trigger an image update on the argocd repository. -""" +"""Trigger an image update on the argocd repository.""" import argparse import os.path @@ -60,9 +58,7 @@ def main() -> None: def dispatch(repository: str, event_type: str, images_full: list[str]) -> None: - """ - Trigger an image update on the argocd repository. - """ + """Trigger an image update on the argocd repository.""" id_ = random.randint(1, 100000) # nosec print(f"Triggering {event_type}:{id_} on {repository} with {','.join(images_full)}") diff --git a/example-project/project/__init__.py b/example-project/project/__init__.py index 7b527d842..76fd503e4 100644 --- a/example-project/project/__init__.py +++ b/example-project/project/__init__.py @@ -1,3 +1 @@ -""" -Example project. -""" +"""Example project."""