1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #16867 from miri64/dist/enh/use-fstrings-where-warned

dist/tools: use f-strings where possible
This commit is contained in:
chrysn 2021-09-17 12:34:17 +02:00 committed by GitHub
commit f93d1276b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 52 deletions

View File

@ -101,11 +101,11 @@ def _get_latest_release(branches):
def _find_remote(repo, user, repo_name):
for remote in repo.remotes:
if (remote.url.endswith("{}/{}.git".format(user, repo_name)) or
remote.url.endswith("{}/{}".format(user, repo_name))):
if (remote.url.endswith(f"{user}/{repo_name}.git") or
remote.url.endswith(f"{user}/{repo_name}")):
return remote
raise ValueError("Could not find remote with URL ending in {}/{}.git"
.format(user, repo_name))
raise ValueError("Could not find remote with URL ending in "
f"{user}/{repo_name}.git")
def _get_upstream(repo):
@ -150,7 +150,7 @@ def main():
# TODO: exception handling
status, user = github_api.user.get()
if status != 200:
print("Could not retrieve user: {}".format(user['message']))
print(f'Could not retrieve user: {user["message"]}')
sys.exit(1)
# Token-scope-check: Is the token is powerful enough to complete
# the Backport?
@ -169,21 +169,19 @@ def main():
username = user['login']
status, pulldata = github_api.repos[ORG][REPO].pulls[args.PR].get()
if status != 200:
print("Commit #{} not found: {}".format(args.PR, pulldata['message']))
print(f'Commit #{args.PR} not found: {pulldata["message"]}')
sys.exit(2)
if not pulldata['merged']:
print("Original PR not yet merged")
sys.exit(0)
print("Fetching for commit: #{}: {}".format(args.PR, pulldata['title']))
print(f'Fetching for commit: #{args.PR}: {pulldata["title"]}')
orig_branch = pulldata['head']['ref']
status, commits = github_api.repos[ORG][REPO].pulls[args.PR].commits.get()
if status != 200:
print("No commits found for #{}: {}".format(args.PR,
commits['message']))
print(f'No commits found for #{args.PR}: {commits["message"]}')
sys.exit(3)
for commit in commits:
print("found {} : {}".format(commit['sha'],
commit['commit']['message']))
print(f'found {commit["sha"]} : {commit["commit"]["message"]}')
# Find latest release branch
if args.release_branch:
@ -192,16 +190,14 @@ def main():
else:
status, branches = github_api.repos[ORG][REPO].branches.get()
if status != 200:
print("Could not retrieve branches for {}/{}: {}"
.format(ORG,
REPO,
branches['message']))
print(f'Could not retrieve branches for {ORG}/{REPO}: '
f'{branches["message"]}')
sys.exit(4)
release_shortname, release_fullname = _get_latest_release(branches)
if not release_fullname:
print("No release branch found, exiting")
sys.exit(5)
print("Backport based on branch {}".format(release_fullname))
print(f"Backport based on branch {release_fullname}")
repo = git.Repo(args.gitdir)
# Fetch current upstream
@ -209,20 +205,20 @@ def main():
if not upstream_remote:
print("No upstream remote found, can't fetch")
sys.exit(6)
print("Fetching {} remote".format(upstream_remote))
print(f"Fetching {upstream_remote} remote")
upstream_remote.fetch()
# Build topic branch in temp dir
new_branch = args.backport_branch_fmt.format(release=release_shortname,
origbranch=orig_branch)
if new_branch in repo.branches:
print("ERROR: Branch {} already exists".format(new_branch))
print(f"ERROR: Branch {new_branch} already exists")
sys.exit(1)
worktree_dir = os.path.join(args.gitdir, WORKTREE_SUBDIR)
repo.git.worktree("add", "-b",
new_branch,
WORKTREE_SUBDIR,
"{}/{}".format(upstream_remote, release_fullname))
f"{upstream_remote}/{release_fullname}")
# transform branch name into Head object for later configuring
new_branch = repo.branches[new_branch]
try:
@ -232,13 +228,13 @@ def main():
bp_repo.git.cherry_pick('-x', commit['sha'])
# Push to github
origin = _find_remote(repo, username, REPO)
print("Pushing branch {} to {}".format(new_branch, origin))
print(f"Pushing branch {new_branch} to {origin}")
if not args.noop:
push_info = origin.push('{0}:{0}'.format(new_branch))
push_info = origin.push(f"{new_branch}:{new_branch}")
new_branch.set_tracking_branch(push_info[0].remote_ref)
except Exception as exc:
# Delete worktree
print("Pruning temporary workdir at {}".format(worktree_dir))
print(f"Pruning temporary workdir at {worktree_dir}")
_delete_worktree(repo, worktree_dir)
# also delete branch created by worktree; this is only possible after
# the worktree was deleted
@ -246,7 +242,7 @@ def main():
raise exc
else:
# Delete worktree
print("Pruning temporary workdir at {}".format(worktree_dir))
print(f"Pruning temporary workdir at {worktree_dir}")
_delete_worktree(repo, worktree_dir)
labels = _get_labels(pulldata)
@ -254,22 +250,19 @@ def main():
if not args.noop:
# Open new PR on github
pull_request = {
'title': "{} [backport {}]".format(pulldata['title'],
release_shortname),
'head': '{}:{}'.format(username, new_branch),
'title': f'{pulldata["title"]} [backport {release_shortname}]',
'head': f'{username}:{new_branch}',
'base': release_fullname,
'body': "# Backport of #{}\n\n{}".format(args.PR,
pulldata['body']),
'body': f'# Backport of #{args.PR}\n\n{pulldata["body"]}',
'maintainer_can_modify': True,
}
status, new_pr = github_api.repos[ORG][REPO].pulls.post(
body=pull_request)
if status != 201:
print("Error creating the new pr: \"{}\". Is \"Public Repo\""
" access enabled for the token"
.format(new_pr['message']))
print(f'Error creating the new pr: "{new_pr["message"]}". '
'Is "Public Repo" access enabled for the token?')
pr_number = new_pr['number']
print("Create PR number #{} for backport".format(pr_number))
print(f"Create PR number #{pr_number} for backport")
github_api.repos[ORG][REPO].issues[pr_number].labels.post(body=labels)
review_request = {"reviewers": [merger]}
github_api.repos[ORG][REPO].pulls[pr_number].\
@ -277,13 +270,12 @@ def main():
# Put commit under old PR
if args.comment and not args.noop:
comment = {"body": "Backport provided in #{}".format(pr_number)}
comment = {"body": f"Backport provided in #{pr_number}"}
status, res = github_api.repos[ORG][REPO].\
issues[args.PR].comments.post(body=comment)
if status != 201:
print("Something went wrong adding the comment: {}"
.format(res['message']))
print("Added comment to #{}".format(args.PR))
print(f'Something went wrong adding the comment: {res["message"]}')
print(f"Added comment to #{args.PR}")
if __name__ == "__main__":

View File

@ -182,10 +182,10 @@ def check_is_board(riotdir, board):
:returns: board name
"""
if board == 'common':
raise ValueError("'%s' is not a board" % board)
raise ValueError(f"'{board}' is not a board")
board_dir = os.path.join(riotdir, 'boards', board)
if not os.path.isdir(board_dir):
raise ValueError("Cannot find '%s' in %s/boards" % (board, riotdir))
raise ValueError(f"Cannot find '{board}' in {riotdir}/boards")
return board
@ -254,7 +254,7 @@ class RIOTApplication():
logging.basicConfig(stream=self.log_stream)
else:
self.testcase = None
self.logger = logging.getLogger('%s.%s' % (board, appdir))
self.logger = logging.getLogger(f'{board}.{appdir}')
# Currently not handling absolute directories or outside of RIOT
assert is_in_directory(self.resultdir, resultdir), \
@ -392,7 +392,7 @@ class RIOTApplication():
create_directory(self.resultdir, clean=True)
self._skip(
'disabled_has_no_tests',
"{} has no tests".format(self.appdir)
f"{self.appdir} has no tests"
)
return
@ -422,7 +422,7 @@ class RIOTApplication():
else:
self._skip(
'skip.no_test',
"{} has no tests".format(self.appdir)
f"{self.appdir} has no tests"
)
self.logger.info('Success')
@ -479,7 +479,7 @@ class RIOTApplication():
# Run setup-tasks, output is only kept in case of error
for taskname, taskargs in setuptasks.items():
taskname = '%s.%s' % (name, taskname)
taskname = f'{name}.{taskname}'
self.logger.info('Run %s', taskname)
try:
self.make(taskargs)
@ -503,7 +503,7 @@ class RIOTApplication():
Returns `output` if it is there, None if not.
"""
try:
with open(self._outfile('%s.success' % name),
with open(self._outfile(f'{name}.success'),
encoding='utf-8') as outputfd:
self.logger.info('Nothing to be done for %s', name)
return outputfd.read()
@ -515,7 +515,7 @@ class RIOTApplication():
"""Handle exception during make step `name`."""
output = ' '.join(err.cmd) + '\n'
output += err.output + '\n'
output += 'Return value: %s\n' % err.returncode
output += f'Return value: {err.returncode}\n'
outfile = self._write_resultfile(name, 'failed', output)
self.logger.warning(output)
@ -523,10 +523,10 @@ class RIOTApplication():
if self.testcase:
self.testcase.stderr += err.output + '\n'
if name == "test":
self.testcase.add_failure_info("{} failed".format(err.cmd),
self.testcase.add_failure_info(f"{err.cmd} failed",
err.output)
else:
self.testcase.add_error_info("{} had an error".format(err.cmd),
self.testcase.add_error_info(f"{err.cmd} had an error",
err.output)
raise ErrorInTest(name, self, outfile)
@ -537,7 +537,7 @@ class RIOTApplication():
"""
# Delete previous status files
resultfiles = glob.glob(self._outfile('%s.*' % name))
resultfiles = glob.glob(self._outfile(f'{name}.*'))
for resultfile in resultfiles:
try:
os.remove(resultfile)
@ -545,7 +545,7 @@ class RIOTApplication():
pass
# Create new file
filename = '%s.%s' % (name, status)
filename = f'{name}.{status}'
outfile = self._outfile(filename)
with open(outfile, 'w+', encoding='utf-8',
@ -595,9 +595,9 @@ def _test_failed_summary(errors, relpathstart=None):
summary = ''
for step, errs in sorted(errors_dict.items()):
summary += 'Failures during %s:\n' % step
summary += f'Failures during {step}:\n'
for appdir, errorfile in errs:
summary += '- [%s](%s)\n' % (appdir, errorfile)
summary += f'- [{appdir}]({errorfile})\n'
# Separate sections with a new line
summary += '\n'
@ -755,7 +755,7 @@ def main(args):
with open(report_file, "w+", encoding="utf-8") as report:
junit_xml.TestSuite.to_file(
report,
[junit_xml.TestSuite('compile_and_test_for_{}'.format(board),
[junit_xml.TestSuite(f'compile_and_test_for_{board}',
[app.testcase for app in applications])]
)
if num_errors:

View File

@ -10,7 +10,7 @@ def test_help_message():
script = 'compile_and_test_for_board.py'
# Read the help message from executing the script
help_bytes = subprocess.check_output(['./%s' % script, '--help'])
help_bytes = subprocess.check_output([f'./{script}', '--help'])
help_msg = help_bytes.decode('utf-8')
docstring = compile_and_test_for_board.__doc__