diff options
author | Cristián Maureira-Fredes <Cristian.Maureira-Fredes@qt.io> | 2022-12-16 15:27:35 +0100 |
---|---|---|
committer | Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2022-12-19 10:04:38 +0000 |
commit | 5b4df3e6ff02a6af35d143d202b6e700a307144c (patch) | |
tree | 5cd374893736b8e70bc8ea5fe9d3d6c5cde8a374 | |
parent | b10cdba1752b232a990ba2432c4bb6d634f933ec (diff) | |
download | qtrepotools-5b4df3e6ff02a6af35d143d202b6e700a307144c.tar.gz |
flake8: fix style issues
flake8 reported a few issues, and there were other details
we could improve, like using more f-strings, avoiding
breaking some lines with '\' at the end and using round parenthesis
instead, and complicated long lines that could be split a bit.
Some function signatures were also long enough to split them into
one argument per line, in favor to display the type annotation
on the last line
Change-Id: I5f65f6c52ac3f2f36488d26a0cb46b8f7c9fff82
Reviewed-by: Daniel Smith <Daniel.Smith@qt.io>
-rw-r--r-- | util/dependency_updater/main.py | 10 | ||||
-rw-r--r-- | util/dependency_updater/tools/config.py | 7 | ||||
-rw-r--r-- | util/dependency_updater/tools/datasources/datasources.py | 4 | ||||
-rw-r--r-- | util/dependency_updater/tools/dependency_resolver.py | 10 | ||||
-rw-r--r-- | util/dependency_updater/tools/namespace.py | 6 | ||||
-rw-r--r-- | util/dependency_updater/tools/proposal.py | 12 | ||||
-rw-r--r-- | util/dependency_updater/tools/repo.py | 12 | ||||
-rw-r--r-- | util/dependency_updater/tools/state.py | 4 | ||||
-rw-r--r-- | util/dependency_updater/tools/teams_connector.py | 7 | ||||
-rw-r--r-- | util/dependency_updater/tools/toolbox.py | 95 |
10 files changed, 86 insertions, 81 deletions
diff --git a/util/dependency_updater/main.py b/util/dependency_updater/main.py index f5f628d..5349ad7 100644 --- a/util/dependency_updater/main.py +++ b/util/dependency_updater/main.py @@ -129,7 +129,7 @@ def main(): # Collect the list of qt5 modules for our reference. config.qt5_default = toolbox.get_qt5_submodules(config, ["essential", "addon", "deprecated", - "preview"]) + "preview"]) # Collect Repo objects for everything in the cache or list of qt5 modules, as necessary. repos = toolbox.get_repos(config) @@ -184,11 +184,11 @@ def main(): repo=config.rewind_module, text=f"INFO: Rewinding '{config.args.branch}' to {new_sha}." f" Modules depending on {config.rewind_module.id} have been reset.") - if config.args.update_supermodule and config.state_data.get("qt/qt5") \ - and not config.rewind_module.id == "yocto/meta-qt6": + if (config.args.update_supermodule and config.state_data.get("qt/qt5") + and not config.rewind_module.id == "yocto/meta-qt6"): del config.state_data["qt/qt5"] - if config.args.update_yocto_meta and config.state_data.get("yocto/meta-qt6") \ - and not config.rewind_module.id == "qt/qt5": + if (config.args.update_yocto_meta and config.state_data.get("yocto/meta-qt6") + and not config.rewind_module.id == "qt/qt5"): del config.state_data["yocto/meta-qt6"] # bump the progress of repos that have had updates pushed and merged. diff --git a/util/dependency_updater/tools/config.py b/util/dependency_updater/tools/config.py index 0e10f46..7dac495 100644 --- a/util/dependency_updater/tools/config.py +++ b/util/dependency_updater/tools/config.py @@ -1,7 +1,6 @@ # Copyright (C) 2021 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -import json import os from pathlib import Path from shutil import copyfile @@ -49,7 +48,7 @@ def _load_config(file, args): c = yaml.load(config_file, Loader=yaml.SafeLoader) else: try: - copyfile(file.parent / (file.name + ".template"), file) + copyfile(file.parent / (f"{file.name}.template"), file) print("Config file not found, so we created 'config.yaml' from the template.") with open(file) as config_file: c = yaml.load(config_file) @@ -76,6 +75,6 @@ def _load_config(file, args): user_loc = contents.find("User", loc) user_name = contents[user_loc:contents.find("\n", user_loc)].split(" ")[1] if user_name: - config._state_ref = f"refs/personal/{user_name or config.GERRIT_USERNAME}" \ - f"/submodule_updater" + config._state_ref = (f"refs/personal/{user_name or config.GERRIT_USERNAME}" + f"/submodule_updater") return config diff --git a/util/dependency_updater/tools/datasources/datasources.py b/util/dependency_updater/tools/datasources/datasources.py index 8d74202..fdc7f98 100644 --- a/util/dependency_updater/tools/datasources/datasources.py +++ b/util/dependency_updater/tools/datasources/datasources.py @@ -16,6 +16,6 @@ class Datasources(Namespace): datasource_names = [o for o in Datasources.__dict__.keys() if o.endswith("_client")] for func_name in datasource_names: dict.__setattr__(self, func_name, - getattr(sys.modules["tools.datasources." + func_name], - "create_" + func_name)(config)) + getattr(sys.modules[f"tools.datasources.{func_name}"], + f"create_{func_name}")(config)) print("Done loading datasources!") diff --git a/util/dependency_updater/tools/dependency_resolver.py b/util/dependency_updater/tools/dependency_resolver.py index 066ca24..63b7fa4 100644 --- a/util/dependency_updater/tools/dependency_resolver.py +++ b/util/dependency_updater/tools/dependency_resolver.py @@ -1,7 +1,6 @@ # Copyright (C) 2020 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only import copy -import json from tools import toolbox from .config import Config @@ -74,8 +73,9 @@ def retrieve_or_generate_proposal(config: Config, repo) -> Proposal: return repo.proposal -def check_subtree(config, source: Repo, source_ref: str, target: Repo) -> tuple[ - str, tuple[str, str]]: +def check_subtree(config, source: Repo, + source_ref: str, + target: Repo) -> tuple[str, tuple[str, str]]: """Compare a sha between two repos' dependencies.yaml references for the same dependency. Recurse for each dependency which is not the same as the source. @@ -120,8 +120,8 @@ def discover_dep_inconsistencies(config: Config, repo: Repo) \ return mismatches -def discover_repo_dependencies(config: Config, repos_override: list[Repo] = None) -> dict[ - str, Repo]: +def discover_repo_dependencies(config: Config, + repos_override: list[Repo] = None) -> dict[str, Repo]: """Traverse the dependency tree for a repo and add any repos found to the list of repos to update if it was not already specified or found to be part of qt5 default modules. diff --git a/util/dependency_updater/tools/namespace.py b/util/dependency_updater/tools/namespace.py index c49377a..2811dec 100644 --- a/util/dependency_updater/tools/namespace.py +++ b/util/dependency_updater/tools/namespace.py @@ -7,10 +7,12 @@ class Namespace(object): 'object[attr]' and '.get(attr)' accessors. """ - def __init__(self, **kwargs): self.__dict__.update(kwargs) + def __init__(self, **kwargs): + self.__dict__.update(kwargs) @property # For use when serializing, to dump back to JSON - def as_map(self): return self.__dict__ + def as_map(self): + return self.__dict__ def __repr__(self): return str(self.as_map) diff --git a/util/dependency_updater/tools/proposal.py b/util/dependency_updater/tools/proposal.py index a3f8651..64ff0c3 100644 --- a/util/dependency_updater/tools/proposal.py +++ b/util/dependency_updater/tools/proposal.py @@ -23,12 +23,12 @@ class Proposal: self.__dict__[key] = value def __str__(self): - return f"Proposal(change_id='{self.change_id}'," \ - f" change_number={self.change_number}" \ - f" gerrit_status='{self.gerrit_status}'" \ - f" inconsistent_set={self.inconsistent_set}," \ - f" merged_ref={self.merged_ref}," \ - f" proposed yaml={self.proposed_yaml})" + return (f"Proposal(change_id='{self.change_id}'," + f" change_number={self.change_number}" + f" gerrit_status='{self.gerrit_status}'" + f" inconsistent_set={self.inconsistent_set}," + f" merged_ref={self.merged_ref}," + f" proposed yaml={self.proposed_yaml})") def __bool__(self): if self.proposed_yaml or self.change_id or self.inconsistent_set: diff --git a/util/dependency_updater/tools/repo.py b/util/dependency_updater/tools/repo.py index 7d4421c..3b3c20f 100644 --- a/util/dependency_updater/tools/repo.py +++ b/util/dependency_updater/tools/repo.py @@ -59,12 +59,12 @@ class Repo(Namespace): self.to_stage.append(proposal.change_id) def __str__(self): - return f"Repo(id='{self.id}', name='{self.name}'," \ - f" ref='{self.original_ref}', branch='{self.branch}'," \ - f" progress={self.progress}," \ - f" stage_count={self.stage_count}," \ - f" retry_count={self.retry_count}," \ - f" proposal={str(self.proposal)})" + return (f"Repo(id='{self.id}', name='{self.name}'," + f" ref='{self.original_ref}', branch='{self.branch}'," + f" progress={self.progress}," + f" stage_count={self.stage_count}," + f" retry_count={self.retry_count}," + f" proposal={str(self.proposal)})") def __repr__(self): return self.id diff --git a/util/dependency_updater/tools/state.py b/util/dependency_updater/tools/state.py index 02dfe21..9a15b69 100644 --- a/util/dependency_updater/tools/state.py +++ b/util/dependency_updater/tools/state.py @@ -87,8 +87,8 @@ def load_updates_state(config) -> dict[str, Repo]: return {} -def update_state_data(old_state: dict[str, Repo], new_data: dict[str, Repo]) -> dict[ - str, Repo]: +def update_state_data(old_state: dict[str, Repo], + new_data: dict[str, Repo]) -> dict[str, Repo]: """Merge two update set dicts""" updated = old_state for key in new_data.keys(): diff --git a/util/dependency_updater/tools/teams_connector.py b/util/dependency_updater/tools/teams_connector.py index 06b8fcc..580f61e 100644 --- a/util/dependency_updater/tools/teams_connector.py +++ b/util/dependency_updater/tools/teams_connector.py @@ -1,6 +1,5 @@ # Copyright (C) 2021 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -import json import pymsteams as msteams import yaml @@ -72,9 +71,9 @@ class TeamsConnector: def send_teams_webhook_module_failed(self, repo, text_override: str = None, test_failures: str = None, pause_links: bool = False): if self.config.args.simulate: + _text = text_override or f"Dependency update on *{repo.id}* failed in **{repo.branch}**" print(f"SIM: send Teams webhook for {repo.id} with text:" - + (text_override or f"Dependency update on *{repo.id}* failed in **{repo.branch}**") - + '\n' + test_failures) + f"{_text}\n{test_failures}") return if self.endpoint: message_card = msteams.connectorcard(self.endpoint) @@ -113,7 +112,7 @@ class TeamsConnector: if reset_links: reset_section = msteams.cardsection() reset = msteams.potentialaction( - f"Reset round (New qtbase)", "HttpPOST") + "Reset round (New qtbase)", "HttpPOST") reset.payload[ "target"] = "https://qt-cherry-pick-bot.herokuapp.com/reset-submodule-updates" reset.payload["body"] = yaml.dump({"branch": config.args.branch}) diff --git a/util/dependency_updater/tools/toolbox.py b/util/dependency_updater/tools/toolbox.py index c56d813..772cff4 100644 --- a/util/dependency_updater/tools/toolbox.py +++ b/util/dependency_updater/tools/toolbox.py @@ -47,8 +47,8 @@ def make_full_id(repo: Repo, change_id_override: str = "") -> str: """Create gerrit change IDs from smaller bits if necessary Format: repo~branch~change_id""" retstr = "" - if (repo.id in change_id_override or urllib.parse.quote_plus(repo.id) in change_id_override)\ - and (repo.branch in change_id_override or urllib.parse.quote_plus(repo.branch) in change_id_override): + if ((repo.id in change_id_override or urllib.parse.quote_plus(repo.id) in change_id_override) + and (repo.branch in change_id_override or urllib.parse.quote_plus(repo.branch) in change_id_override)): # Already formatted! retstr = change_id_override elif change_id_override: @@ -176,8 +176,10 @@ def search_for_repo(config, repo: Union[str, Repo]) -> Union[None, Repo]: return ret_repo if ret_repo else Repo(guess_id, guess_prefix) -def parse_gitmodules(config: Config, repo: [Repo, str], branch: str = "", ref: str = "") -> dict[ - str, dict[str, str]]: +def parse_gitmodules(config: Config, + repo: [Repo, str], + branch: str = "", + ref: str = "") -> dict[str, dict[str, str]]: """Retrieve .gitmodules and parse it into a dict. :param branch branch: exclusive with ref. Pull from branch head. @@ -195,12 +197,11 @@ def parse_gitmodules(config: Config, repo: [Repo, str], branch: str = "", ref: s gerrit = config.datasources.gerrit_client retdict = dict() try: + _repo = gerrit.projects.get(repo_id) if ref: - gitmodules = gerrit.projects.get(repo_id).get_commit(ref) \ - .get_file_content(".gitmodules") + gitmodules = _repo.get_commit(ref).get_file_content(".gitmodules") else: - gitmodules = gerrit.projects.get(repo_id).branches.get(branch) \ - .get_file_content('.gitmodules') + gitmodules = _repo.branches.get(branch).get_file_content('.gitmodules') except GerritExceptions.NotFoundError: print(f"WARN: {repo_id} does not contain .gitmodules! " f"It probably doesn't have any submodules.") @@ -244,8 +245,8 @@ def get_head(config: Config, repo: Union[Repo, str], pull_head: bool = False) -> gerrit = config.datasources.gerrit_client if type(repo) == str: repo = search_for_repo(config, repo) - if not pull_head and repo.id in config.state_data.keys() \ - and config.state_data[repo.id].progress >= PROGRESS.DONE: + if (not pull_head and repo.id in config.state_data.keys() + and config.state_data[repo.id].progress >= PROGRESS.DONE): if config.state_data[repo.id].proposal.merged_ref: return config.state_data[repo.id].proposal.merged_ref else: @@ -259,7 +260,7 @@ def get_head(config: Config, repo: Union[Repo, str], pull_head: bool = False) -> branch_head = None for branch in branches: try: - branch_head = gerrit.projects.get(repo.id).branches.get("refs/heads/" + branch) + branch_head = gerrit.projects.get(repo.id).branches.get(f"refs/heads/{branch}") if branch != config.args.branch and not config.suppress_warn: print(f"INFO: Using {branch} instead of {config.args.branch} " f"as the reference for {repo}") @@ -390,8 +391,8 @@ def parse_failed_integration_log(config, repo: Repo = None, log_url: str = "") - if test_failures: ret_str += f"Total failed test cases: {len(test_failures)}\n" for fail_case in test_failures.keys(): - ret_str += fail_case + "\n" - ret_str += '\n'.join(test_failures[fail_case]) + '\n' + _failures = '\n'.join(test_failures[fail_case]) + ret_str += f"{fail_case}\n{_failures}\n" # ret_str += "\n" ret_str = strip_agent_from_test_log(ret_str) return ret_str @@ -442,8 +443,8 @@ def parse_log_test_failures(logtext: str) -> dict[str, list[str]]: ret_dict[tstname] = [] # Save a snip of the log with the failed test(s) - logsnip = logtext[ - crash_index: logtext.find("\n", logtext.find("***Failed ", tstnameindex))] + logsnip = logtext[crash_index: logtext.find("\n", logtext.find("***Failed ", + tstnameindex))] ret_dict[tstname].append(logsnip) @@ -479,8 +480,8 @@ def get_dependencies_yaml(config, repo: Repo, fetch_head: bool = False) -> tuple pass if not r: - if repo.id in config.state_data.keys() and not fetch_head and \ - config.state_data[repo.id].progress >= PROGRESS.DONE: + if (repo.id in config.state_data.keys() and not fetch_head + and config.state_data[repo.id].progress >= PROGRESS.DONE): print(f"Using state deps.yaml from merged repo {repo.id}") if config.state_data[repo.id].proposal: return config.state_data[repo.id].proposal.proposed_yaml, config.state_repo[ @@ -490,8 +491,8 @@ def get_dependencies_yaml(config, repo: Repo, fetch_head: bool = False) -> tuple branches = [config.args.branch, "dev", "master"] for branch in branches: try: - r = gerrit.projects.get(repo.id).branches.get( - 'refs/heads/' + branch).get_file_content('dependencies.yaml') + _repo = gerrit.projects.get(repo.id) + r = _repo.branches.get(f"refs/heads/{branch}").get_file_content('dependencies.yaml') found_branch = branch if not branch == config.args.branch: if not config.suppress_warn: @@ -593,9 +594,9 @@ def stage_update(config: Config, repo: Repo) -> bool: else: message = "" else: - message = "Staging this change automatically with the dependency update for this" \ - f" module:\n" \ - f"{gerrit_link_self}" + message = ("Staging this change automatically with the dependency update for this" + f" module:\n" + f"{gerrit_link_self}") if stage_change(config, change_id, message): print(f"{repo.id}: Staged " f"{'submodule update' if repo.proposal.change_id == change_id else 'related change'}" @@ -680,12 +681,12 @@ def gather_costaging_changes(config: Config, repo: Repo) -> list[str]: def search_existing_change(config: Config, repo: Repo, message: str) -> tuple[str, str]: """Try to re-use open changes the bot created where possible instead of spamming new changes""" - changes = config.datasources.gerrit_client.changes \ - .search(f'q=message:"{message}"' - f'+owner:{config.GERRIT_USERNAME}' - f'+(status:open+OR+status:staged+OR+status:integrating)' - f'+branch:{repo.branch}' - f'+repo:{repo.id}') + _query = (f'q=message:"{message}"' + f'+owner:{config.GERRIT_USERNAME}' + f'+(status:open+OR+status:staged+OR+status:integrating)' + f'+branch:{repo.branch}' + f'+repo:{repo.id}') + changes = config.datasources.gerrit_client.changes.search(_query) if changes: change = changes.pop() return change.change_id, change._number @@ -712,9 +713,10 @@ def push_submodule_update(config: Config, repo: Repo, retry: bool = False) -> Pr # This can occur if the round was reset or rewound while a patch was still # integrating. Instead of creating a new change, locate it, compare our yaml file # with the integrating one. - current_patch_deps = yaml.load(bytes.decode(base64.b64decode( - change.get_revision("current").get_commit().get_file_content("dependencies.yaml")), - 'utf-8'), Loader=yaml.Loader) + _commit = change.get_revision("current").get_commit() + current_patch_deps = yaml.load( + bytes.decode(base64.b64decode(_commit.get_file_content("dependencies.yaml")), 'utf-8'), + Loader=yaml.Loader) if current_patch_deps == repo.proposal.proposed_yaml: repo.proposal.gerrit_status = change.status print(f"Currently {change.status} change in {repo.id} is already up-to-date!") @@ -733,8 +735,8 @@ def push_submodule_update(config: Config, repo: Repo, retry: bool = False) -> Pr change.rebase({"base": ""}) print(f"Rebased change {change.change_id}") except GerritExceptions.ConflictError: - if not change.get_revision("current").get_commit().parents[0]["commit"]\ - == get_head(config, repo, True): + if (not change.get_revision("current").get_commit().parents[0]["commit"] + == get_head(config, repo, True)): print("WARN: Failed to rebase change due to conflicts." " Abandoning and recreating the change.") # Failed to rebase because of conflicts @@ -752,7 +754,9 @@ def push_submodule_update(config: Config, repo: Repo, retry: bool = False) -> Pr try: edit.put_change_file_content("dependencies.yaml", deps_yaml_file) file_content_edit = bytes.decode(base64.b64decode(edit.get_change_file_content("dependencies.yaml"))) - print(f"Push file succeeded? {deps_yaml_file == file_content_edit}\n{file_content_edit if deps_yaml_file != file_content_edit else ''}") + _succeeded = deps_yaml_file == file_content_edit + _content = file_content_edit if deps_yaml_file != file_content_edit else '' + print(f"Push file succeeded? {_succeeded}\n{_content}") time.sleep(1) edit.publish({ "notify": "NONE" @@ -792,7 +796,7 @@ def do_try_supermodule_updates(config: Config) -> dict[str, Repo]: def push_supermodule_update(config: Config, retry: bool = False) -> Repo: """Push the meta-update with all the new shas to the supermodule repo""" gerrit = config.datasources.gerrit_client - qt5_name = config.args.repo_prefix + "qt5" + qt5_name = f"{config.args.repo_prefix}qt5" qt5_repo = search_for_repo(config, qt5_name) if qt5_repo.progress >= PROGRESS.IN_PROGRESS: return qt5_repo @@ -874,8 +878,8 @@ def push_supermodule_update(config: Config, retry: bool = False) -> Repo: change.rebase({"base": ""}) print(f"Rebased change {change.change_id}") except GerritExceptions.ConflictError: - if not change.get_revision("current").get_commit().parents[0]["commit"]\ - == get_head(config, qt5_repo, True): + if (not change.get_revision("current").get_commit().parents[0]["commit"] + == get_head(config, qt5_repo, True)): print("WARN: Failed to rebase change due to conflicts." " Abandoning and recreating the change.") # Failed to rebase because of conflicts @@ -904,8 +908,8 @@ def push_supermodule_update(config: Config, retry: bool = False) -> Repo: print(f"Published edit as new patchset on {change.change_id}") qt5_repo.progress = PROGRESS.IN_PROGRESS approve_change_id(change, qt5_repo.id) - config.teams_connector.send_teams_webhook_basic(text=f"Updating {qt5_repo.id} with a consistent" - f" set of submodules in **{qt5_repo.branch}**", repo=qt5_repo) + _text = f"Updating {qt5_repo.id} with a consistent set of submodules in **{qt5_repo.branch}**" + config.teams_connector.send_teams_webhook_basic(text=_text, repo=qt5_repo) except GerritExceptions.ConflictError: print(f"No changes made to {qt5_repo.id}, possible that the current patchset is up-to-date") edit.delete() @@ -913,8 +917,8 @@ def push_supermodule_update(config: Config, retry: bool = False) -> Repo: for repo in qt5_modules.values(): submodule_patch_ref = bytes.decode(base64.b64decode( change.get_revision("current").get_commit().get_file_content(repo.name)), 'utf-8') - if repo.proposal and submodule_patch_ref == repo.proposal.merged_ref\ - and not submodule_patch_ref == repo.original_ref: + if (repo.proposal and submodule_patch_ref == repo.proposal.merged_ref + and not submodule_patch_ref == repo.original_ref): diff = True if not diff: # The current patchset is the same as HEAD. Don't stage empty changes! @@ -945,9 +949,10 @@ def search_pinned_submodule(config: Config, module: Repo, submodule: [str, Repo] ref=module_ref) submodule_name = submodule.name if type(submodule) == Repo else submodule for key, data in gitmodules.items(): - if submodule_name in key or submodule_name in data.get("url"): + data_url = data.get("url") + if submodule_name in key or submodule_name in data_url: print( - f"Found submodule {submodule_name} in {[d for d in [key, 'url: ' + data.get('url')] if submodule_name in d]}") + f"Found submodule {submodule_name} in {[d for d in [key, f'url: {data_url}'] if submodule_name in d]}") # Fetch the pinned submodule ref r = gerrit.projects.get(module.id).get_commit(module_ref).get_file_content( data.get("path")) @@ -1124,8 +1129,8 @@ def push_yocto_update(config: Config, retry: bool = False) -> Repo: return yocto_repo -def acquire_change_edit(config: Config, repo: Repo, subject: str) -> tuple[ - GerritChange, GerritChangeEdit]: +def acquire_change_edit(config: Config, + repo: Repo, subject: str) -> tuple[GerritChange, GerritChangeEdit]: """Create a new codereview change if necessary and acquire an edit on the change""" gerrit = config.datasources.gerrit_client |