scripts: Fix risky uses of non-raw regex strings in Python scripts

Fixes pylint warnings like this one:

    doc/conf.py:325: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'.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
This commit is contained in:
Ulf Magnusson 2019-03-21 21:38:03 +01:00 committed by Kumar Gala
commit a449c98db2
10 changed files with 17 additions and 17 deletions

View file

@ -66,7 +66,7 @@ class SignedOffBy(CommitRule):
flags |= re.IGNORECASE
for line in commit.message.body:
if line.lower().startswith("signed-off-by"):
if not re.search('(^)Signed-off-by: ([-\'\w.]+) ([-\'\w.]+) (.*)', line, flags=flags):
if not re.search(r"(^)Signed-off-by: ([-'\w.]+) ([-'\w.]+) (.*)", line, flags=flags):
return [RuleViolation(self.id, "Signed-off-by: must have a full name", line_nr=1)]
else:
return
@ -106,7 +106,7 @@ class MaxLineLengthExceptions(LineRule):
def validate(self, line, _commit):
max_length = self.options['line-length'].value
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
if line.startswith('Signed-off-by'):
return