2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +00:00

Auto-format Python files with black

This patch is strictly the result of:
$ black $(git ls-files '*.py')

There have been no manual changes.
This commit is contained in:
Tom Krizek
2022-06-07 16:27:23 +02:00
parent 5d2b7cab08
commit c9cb8ae9eb
36 changed files with 1520 additions and 908 deletions

View File

@@ -15,24 +15,30 @@ import re
# Helper functions and variables
def added_lines(target_branch, paths):
import subprocess
subprocess.check_output(['/usr/bin/git', 'fetch', '--depth', '1', 'origin',
target_branch])
diff = subprocess.check_output(['/usr/bin/git', 'diff', 'FETCH_HEAD..',
'--'] + paths)
subprocess.check_output(
["/usr/bin/git", "fetch", "--depth", "1", "origin", target_branch]
)
diff = subprocess.check_output(
["/usr/bin/git", "diff", "FETCH_HEAD..", "--"] + paths
)
added_lines = []
for line in diff.splitlines():
if line.startswith(b'+') and not line.startswith(b'+++'):
if line.startswith(b"+") and not line.startswith(b"+++"):
added_lines.append(line)
return added_lines
def lines_containing(lines, string):
return [l for l in lines if bytes(string, 'utf-8') in l]
changes_issue_or_mr_id_regex = re.compile(br'\[(GL [#!]|RT #)[0-9]+\]')
relnotes_issue_or_mr_id_regex = re.compile(br':gl:`[#!][0-9]+`')
release_notes_regex = re.compile(r'doc/(arm|notes)/notes-.*\.(rst|xml)')
def lines_containing(lines, string):
return [l for l in lines if bytes(string, "utf-8") in l]
changes_issue_or_mr_id_regex = re.compile(rb"\[(GL [#!]|RT #)[0-9]+\]")
relnotes_issue_or_mr_id_regex = re.compile(rb":gl:`[#!][0-9]+`")
release_notes_regex = re.compile(r"doc/(arm|notes)/notes-.*\.(rst|xml)")
modified_files = danger.git.modified_files
mr_labels = danger.gitlab.mr.labels
@@ -75,33 +81,39 @@ fixup_error_logged = False
for commit in danger.git.commits:
message_lines = commit.message.splitlines()
subject = message_lines[0]
if (not fixup_error_logged and
(subject.startswith('fixup!') or
subject.startswith('Apply suggestion'))):
fail('Fixup commits are still present in this merge request. '
'Please squash them before merging.')
fixup_error_logged = True
if len(subject) > 72 and not subject.startswith('Merge branch '):
warn(
f'Subject line for commit {commit.sha} is too long: '
f'```{subject}``` ({len(subject)} > 72 characters).'
if not fixup_error_logged and (
subject.startswith("fixup!") or subject.startswith("Apply suggestion")
):
fail(
"Fixup commits are still present in this merge request. "
"Please squash them before merging."
)
if subject[-1] == '.':
fail(f'Trailing dot found in the subject of commit {commit.sha}.')
fixup_error_logged = True
if len(subject) > 72 and not subject.startswith("Merge branch "):
warn(
f"Subject line for commit {commit.sha} is too long: "
f"```{subject}``` ({len(subject)} > 72 characters)."
)
if subject[-1] == ".":
fail(f"Trailing dot found in the subject of commit {commit.sha}.")
if len(message_lines) > 1 and message_lines[1]:
fail(f'No empty line after subject for commit {commit.sha}.')
if (len(message_lines) < 3 and
'fixup! ' not in subject and
' CHANGES ' not in subject and
' release note' not in subject):
warn(f'Please write a log message for commit {commit.sha}.')
fail(f"No empty line after subject for commit {commit.sha}.")
if (
len(message_lines) < 3
and "fixup! " not in subject
and " CHANGES " not in subject
and " release note" not in subject
):
warn(f"Please write a log message for commit {commit.sha}.")
for line in message_lines[2:]:
if (len(line) > 72 and
not line.startswith(' ') and
not re.match(r'\[[0-9]+\]', line)):
if (
len(line) > 72
and not line.startswith(" ")
and not re.match(r"\[[0-9]+\]", line)
):
warn(
f'Line too long in log message for commit {commit.sha}: '
f'```{line}``` ({len(line)} > 72 characters).'
f"Line too long in log message for commit {commit.sha}: "
f"```{line}``` ({len(line)} > 72 characters)."
)
###############################################################################
@@ -111,7 +123,7 @@ for commit in danger.git.commits:
# FAIL if the merge request is not assigned to any milestone.
if not danger.gitlab.mr.milestone:
fail('Please assign this merge request to a milestone.')
fail("Please assign this merge request to a milestone.")
###############################################################################
# VERSION LABELS
@@ -129,15 +141,19 @@ if not danger.gitlab.mr.milestone:
# request is not a backport, version labels are used for indicating
# backporting preferences.)
backport_label_set = 'Backport' in mr_labels
version_labels = [l for l in mr_labels if l.startswith('v9.')]
backport_label_set = "Backport" in mr_labels
version_labels = [l for l in mr_labels if l.startswith("v9.")]
if backport_label_set and len(version_labels) != 1:
fail('The *Backport* label is set for this merge request. '
'Please also set exactly one version label (*v9.x*).')
fail(
"The *Backport* label is set for this merge request. "
"Please also set exactly one version label (*v9.x*)."
)
if not backport_label_set and not version_labels:
fail('If this merge request is a backport, set the *Backport* label and '
'a single version label (*v9.x*) indicating the target branch. '
'If not, set version labels for all targeted backport branches.')
fail(
"If this merge request is a backport, set the *Backport* label and "
"a single version label (*v9.x*) indicating the target branch. "
"If not, set version labels for all targeted backport branches."
)
###############################################################################
# OTHER LABELS
@@ -151,12 +167,16 @@ if not backport_label_set and not version_labels:
# remind developers about the need to set the latter on merge requests which
# passed review.)
if 'Review' not in mr_labels:
warn('This merge request does not have the *Review* label set. '
'Please set it if you would like the merge request to be reviewed.')
elif 'LGTM (Merge OK)' not in mr_labels:
warn('This merge request is currently in review. '
'It should not be merged until it is marked with the *LGTM* label.')
if "Review" not in mr_labels:
warn(
"This merge request does not have the *Review* label set. "
"Please set it if you would like the merge request to be reviewed."
)
elif "LGTM (Merge OK)" not in mr_labels:
warn(
"This merge request is currently in review. "
"It should not be merged until it is marked with the *LGTM* label."
)
###############################################################################
# 'CHANGES' FILE
@@ -178,25 +198,31 @@ elif 'LGTM (Merge OK)' not in mr_labels:
# * The merge request adds a new CHANGES entry that is not a placeholder and
# does not contain any GitLab/RT issue/MR identifiers.
changes_modified = 'CHANGES' in modified_files
no_changes_label_set = 'No CHANGES' in mr_labels
changes_modified = "CHANGES" in modified_files
no_changes_label_set = "No CHANGES" in mr_labels
if not changes_modified and not no_changes_label_set:
fail('This merge request does not modify `CHANGES`. '
'Add a `CHANGES` entry or set the *No CHANGES* label.')
fail(
"This merge request does not modify `CHANGES`. "
"Add a `CHANGES` entry or set the *No CHANGES* label."
)
if changes_modified and no_changes_label_set:
fail('This merge request modifies `CHANGES`. '
'Revert `CHANGES` modifications or unset the *No Changes* label.')
fail(
"This merge request modifies `CHANGES`. "
"Revert `CHANGES` modifications or unset the *No Changes* label."
)
changes_added_lines = added_lines(target_branch, ['CHANGES'])
placeholders_added = lines_containing(changes_added_lines, '[placeholder]')
changes_added_lines = added_lines(target_branch, ["CHANGES"])
placeholders_added = lines_containing(changes_added_lines, "[placeholder]")
identifiers_found = filter(changes_issue_or_mr_id_regex.search, changes_added_lines)
if changes_added_lines:
if placeholders_added:
if target_branch != 'main':
fail('This MR adds at least one placeholder entry to `CHANGES`. '
'It should be targeting the `main` branch.')
if target_branch != "main":
fail(
"This MR adds at least one placeholder entry to `CHANGES`. "
"It should be targeting the `main` branch."
)
elif not any(identifiers_found):
fail('No valid issue/MR identifiers found in added `CHANGES` entries.')
fail("No valid issue/MR identifiers found in added `CHANGES` entries.")
###############################################################################
# RELEASE NOTES
@@ -221,25 +247,31 @@ if changes_added_lines:
# identifiers are found in the lines added to the release notes by this
# MR.
release_notes_regex = re.compile(r'doc/(arm|notes)/notes-.*\.(rst|xml)')
release_notes_regex = re.compile(r"doc/(arm|notes)/notes-.*\.(rst|xml)")
release_notes_changed = list(filter(release_notes_regex.match, modified_files))
release_notes_label_set = 'Release Notes' in mr_labels
release_notes_label_set = "Release Notes" in mr_labels
if not release_notes_changed:
if release_notes_label_set:
fail('This merge request has the *Release Notes* label set. '
'Add a release note or unset the *Release Notes* label.')
elif 'Customer' in mr_labels:
warn('This merge request has the *Customer* label set. '
'Add a release note unless the changes introduced are trivial.')
fail(
"This merge request has the *Release Notes* label set. "
"Add a release note or unset the *Release Notes* label."
)
elif "Customer" in mr_labels:
warn(
"This merge request has the *Customer* label set. "
"Add a release note unless the changes introduced are trivial."
)
if release_notes_changed and not release_notes_label_set:
fail('This merge request modifies release notes. '
'Revert release note modifications or set the *Release Notes* label.')
fail(
"This merge request modifies release notes. "
"Revert release note modifications or set the *Release Notes* label."
)
if release_notes_changed:
notes_added_lines = added_lines(target_branch, release_notes_changed)
identifiers_found = filter(relnotes_issue_or_mr_id_regex.search, notes_added_lines)
if notes_added_lines and not any(identifiers_found):
warn('No valid issue/MR identifiers found in added release notes.')
warn("No valid issue/MR identifiers found in added release notes.")
else:
notes_added_lines = []
@@ -251,13 +283,17 @@ else:
# identifier is missing from either the added CHANGES entry or the added
# release note.
if lines_containing(changes_added_lines, '[security]'):
if not lines_containing(changes_added_lines, '(CVE-20'):
fail('This merge request fixes a security issue. '
'Please add a CHANGES entry which includes a CVE identifier.')
if not lines_containing(notes_added_lines, 'CVE-20'):
fail('This merge request fixes a security issue. '
'Please add a release note which includes a CVE identifier.')
if lines_containing(changes_added_lines, "[security]"):
if not lines_containing(changes_added_lines, "(CVE-20"):
fail(
"This merge request fixes a security issue. "
"Please add a CHANGES entry which includes a CVE identifier."
)
if not lines_containing(notes_added_lines, "CVE-20"):
fail(
"This merge request fixes a security issue. "
"Please add a release note which includes a CVE identifier."
)
###############################################################################
# PAIRWISE TESTING
@@ -266,13 +302,16 @@ if lines_containing(changes_added_lines, '[security]'):
# FAIL if the merge request adds any new ./configure switch without an
# associated annotation used for pairwise testing.
configure_added_lines = added_lines(target_branch, ['configure.ac'])
switches_added = (lines_containing(configure_added_lines, 'AC_ARG_ENABLE') +
lines_containing(configure_added_lines, 'AC_ARG_WITH'))
annotations_added = lines_containing(configure_added_lines, '# [pairwise: ')
configure_added_lines = added_lines(target_branch, ["configure.ac"])
switches_added = lines_containing(
configure_added_lines, "AC_ARG_ENABLE"
) + lines_containing(configure_added_lines, "AC_ARG_WITH")
annotations_added = lines_containing(configure_added_lines, "# [pairwise: ")
if len(switches_added) > len(annotations_added):
fail('This merge request adds at least one new `./configure` switch that '
'is not annotated for pairwise testing purposes.')
fail(
"This merge request adds at least one new `./configure` switch that "
"is not annotated for pairwise testing purposes."
)
###############################################################################
# USER-VISIBLE LOG LEVELS
@@ -281,16 +320,18 @@ if len(switches_added) > len(annotations_added):
# WARN if the merge request adds new user-visible log messages (INFO or above)
user_visible_log_levels = [
'ISC_LOG_INFO',
'ISC_LOG_NOTICE',
'ISC_LOG_WARNING',
'ISC_LOG_ERROR',
'ISC_LOG_CRITICAL',
"ISC_LOG_INFO",
"ISC_LOG_NOTICE",
"ISC_LOG_WARNING",
"ISC_LOG_ERROR",
"ISC_LOG_CRITICAL",
]
source_added_lines = added_lines(target_branch, ['*.[ch]'])
source_added_lines = added_lines(target_branch, ["*.[ch]"])
for log_level in user_visible_log_levels:
if (lines_containing(source_added_lines, log_level)):
warn('This merge request adds new user-visible log messages with '
'level INFO or above. Please double-check log levels and make '
'sure none of the messages added is a leftover debug message.')
if lines_containing(source_added_lines, log_level):
warn(
"This merge request adds new user-visible log messages with "
"level INFO or above. Please double-check log levels and make "
"sure none of the messages added is a leftover debug message."
)
break