Skip to content

Commit 7d8fa0b

Browse files
ulfalizernashif
authored andcommitted
scripts: Fix risky uses of non-raw regex strings in Python scripts
Fixes pylint warnings like this one: scripts/check_compliance.py:326:0: W1401: Anomalous backslash in string: '\s'. String constant might be missing an r prefix. (anomalous-backslash-in-string) The reason for this warning is that backslash escapes are interpreted in non-raw (non-r-prefixed) strings. For example, '\a' and r'\a' are not the same string (first one has a single ASCII bell character, second one has two characters). It just happens that there's no \s (or \., or \/) escape for example, and '\s' turns into two characters (as needed for a regex). It's risky to rely on stuff like that regexes though. Best to make them raw strings unless they're super trivial. Also note that '\s' and '\\s' turn into the same string. Another tip: A literal ' can be put into a string with "blah'blah" instead of 'blah\'blah'. Piggyback removal of a redundant semicolon. zephyrproject-rtos/ci-tools#37 (just to link) Signed-off-by: Ulf Magnusson <[email protected]>
1 parent d08fecc commit 7d8fa0b

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

scripts/ci/check_compliance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def parse_codeowners(self, git_root, codeowners):
430430
with open(codeowners, "r") as codeo:
431431
for line in codeo.readlines():
432432
if not line.startswith("#") and line != "\n":
433-
match = re.match("([^\s]+)\s+(.*)", line)
433+
match = re.match(r"([^\s]+)\s+(.*)", line)
434434
if match:
435435
add_base = False
436436
path = match.group(1)
@@ -633,14 +633,14 @@ def run(self):
633633
sha = ""
634634
parsed_addr = None
635635
for line in commit.split("\n"):
636-
match = re.search("^commit\s([^\s]*)", line)
636+
match = re.search(r"^commit\s([^\s]*)", line)
637637
if match:
638638
sha = match.group(1)
639-
match = re.search("^Author:\s(.*)", line)
639+
match = re.search(r"^Author:\s(.*)", line)
640640
if match:
641641
author = match.group(1)
642642
parsed_addr = parseaddr(author)
643-
match = re.search("signed-off-by:\s(.*)", line, re.IGNORECASE)
643+
match = re.search(r"signed-off-by:\s(.*)", line, re.IGNORECASE)
644644
if match:
645645
signed.append(match.group(1))
646646

scripts/ci/gitlint/zephyr_commit_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def validate(self, commit):
7171
flags |= re.IGNORECASE
7272
for line in commit.message.body:
7373
if line.lower().startswith("signed-off-by"):
74-
if not re.search('(^)Signed-off-by: ([-\'\w.]+) ([-\'\w.]+) (.*)', line, flags=flags):
74+
if not re.search(r"(^)Signed-off-by: ([-'\w.]+) ([-'\w.]+) (.*)", line, flags=flags):
7575
return [RuleViolation(self.id, "Signed-off-by: must have a full name", line_nr=1)]
7676
else:
7777
return
@@ -111,7 +111,7 @@ class MaxLineLengthExceptions(LineRule):
111111

112112
def validate(self, line, _commit):
113113
max_length = self.options['line-length'].value
114-
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
114+
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
115115
if line.startswith('Signed-off-by'):
116116
return
117117

scripts/ci/merge_junit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def merge_results(xml_files):
3535

3636
for file_name in xml_files:
3737
tree = ET.parse(file_name)
38-
test_suite = tree.findall('testsuite')[0];
38+
test_suite = tree.findall('testsuite')[0]
3939
failures += int(test_suite.attrib['failures'])
4040
tests += int(test_suite.attrib['tests'])
4141
errors += int(test_suite.attrib['errors'])

0 commit comments

Comments
 (0)