Merge ~smoser/curtin:cleanup/fix-tip-pycodestyle-invalid-escape-sequences into curtin:master

Proposed by Scott Moser
Status: Merged
Approved by: Scott Moser
Approved revision: f9f227789776c1b0975c9f1675e72a4a3663b130
Merge reported by: Scott Moser
Merged at revision: 2ee52452fa5f3be088aa50b7d3609d5818c1524d
Proposed branch: ~smoser/curtin:cleanup/fix-tip-pycodestyle-invalid-escape-sequences
Merge into: curtin:master
Diff against target: 73 lines (+7/-8)
4 files modified
curtin/block/mdadm.py (+3/-4)
curtin/commands/curthooks.py (+1/-1)
tests/vmtests/__init__.py (+1/-1)
tests/vmtests/test_pollinate_useragent.py (+2/-2)
Reviewer Review Type Date Requested Status
Scott Moser (community) Approve
Chad Smith Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+343012@code.launchpad.net

Commit message

pycodestyle: Fix invalid escape sequences in string literals.

A bit of information from python doc that I got by never having known:
  String literals may optionally be prefixed with a letter `r' or `R';
  such strings are called raw strings and use different rules for
  backslash escape sequences.
  ...
  Unless an `r' or `R' prefix is present, escape sequences in strings are
  interpreted according to rules similar to those used by Standard C.

So basically, any use of \ not followed by one of [\'"abfnrtv]
or \ooo (octal) \xhh (hex) or a newline is invalid. This is most
commonly seen for us in regex. To solve, you either:
 a.) use a raw string r'...'
 b.) correctly escape the \ that was not intended to be interpreted.

Python has deprecated these invalid string literals now
  https://bugs.python.org/issue27364
and pycodestyle is identifying them with a W605 warning.
  https://github.com/PyCQA/pycodestyle/pull/676

To post a comment you must log in.
Revision history for this message
Ryan Harper (raharper) wrote :

IIRC, if we want to use \ escapes, then the string must be a raw string. There are few cases where you drop the r'' and others were you add it.

At least for regex, I think we can consistently use r'', and for other causes (we pass strings to a shell) we've escaped things like $; those, IIRC, don't need escaping unless we're running them through shell.

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Chad Smith (chad.smith) :
Revision history for this message
Chad Smith (chad.smith) :
Revision history for this message
Scott Moser (smoser) :
Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) :
Revision history for this message
Chad Smith (chad.smith) wrote :

LGTM. +1 with whichever direction you take on that last regex change.

review: Approve
f9f2277... by Scott Moser

take ryan's suggestion for ua_val replacement

Revision history for this message
Scott Moser (smoser) wrote :

i added ryan's last suggestion.
and i'm going to merge to fix tip-pycodestyle.

review: Approve
Revision history for this message
Scott Moser (smoser) wrote :

Thank you for your merge proposal.

Your branch has been set to 'Work in progress'.
Please set the branch back to 'Needs Review' after resolving the issues below.

Thanks again,
Your friendly neighborhood curtin robot.

Please fix the following issues:
------------------------------
Commit message lints:
 - Line #4 has 1 too many characters. Line starts with: " such strings are called"...
------------------------------

For more information, see commit message guidelines at
https://cloudinit.readthedocs.io/en/latest/topics/hacking.html#do-these-things-for-each-feature-or-bug

review: Needs Fixing
Revision history for this message
Scott Moser (smoser) wrote :

adjusted message.

review: Approve
Revision history for this message
Scott Moser (smoser) wrote :

An upstream commit landed for this bug.

To view that commit see the following URL:
https://git.launchpad.net/curtin/commit/?id=2ee52452

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/curtin/block/mdadm.py b/curtin/block/mdadm.py
2index b0f5591..2e73e71 100644
3--- a/curtin/block/mdadm.py
4+++ b/curtin/block/mdadm.py
5@@ -483,7 +483,7 @@ def __mdadm_detail_to_dict(input):
6 '''
7 data = {}
8
9- device = re.findall('^(\/dev\/[a-zA-Z0-9-\._]+)', input)
10+ device = re.findall(r'^(\/dev\/[a-zA-Z0-9-\._]+)', input)
11 if len(device) == 1:
12 data.update({'device': device[0]})
13 else:
14@@ -491,9 +491,8 @@ def __mdadm_detail_to_dict(input):
15
16 # FIXME: probably could do a better regex to match the LHS which
17 # has one, two or three words
18- for f in re.findall('(\w+|\w+\ \w+|\w+\ \w+\ \w+)' +
19- '\ \:\ ([a-zA-Z0-9\-\.,: \(\)=\']+)',
20- input, re.MULTILINE):
21+ rem = r'(\w+|\w+\ \w+|\w+\ \w+\ \w+)\ \:\ ([a-zA-Z0-9\-\.,: \(\)=\']+)'
22+ for f in re.findall(rem, input, re.MULTILINE):
23 key = f[0].replace(' ', '_').lower()
24 val = f[1]
25 if key in data:
26diff --git a/curtin/commands/curthooks.py b/curtin/commands/curthooks.py
27index 9e51a65..422fb63 100644
28--- a/curtin/commands/curthooks.py
29+++ b/curtin/commands/curthooks.py
30@@ -336,7 +336,7 @@ def setup_grub(cfg, target):
31 export LANG=C;
32 for d in "$@"; do
33 sgdisk "$d" --print |
34- awk "\$6 == prep { print d \$1 }" "d=$d" prep=4100
35+ awk '$6 == prep { print d $1 }' "d=$d" prep=4100
36 done
37 """)
38 try:
39diff --git a/tests/vmtests/__init__.py b/tests/vmtests/__init__.py
40index 64fc867..b5aa538 100644
41--- a/tests/vmtests/__init__.py
42+++ b/tests/vmtests/__init__.py
43@@ -1529,7 +1529,7 @@ def check_install_log(install_log):
44 # regexps expected in curtin output
45 install_pass = INSTALL_PASS_MSG
46 install_fail = "({})".format("|".join([
47- 'Installation\ failed',
48+ 'Installation failed',
49 'ImportError: No module named.*',
50 'Unexpected error while running command',
51 'E: Unable to locate package.*',
52diff --git a/tests/vmtests/test_pollinate_useragent.py b/tests/vmtests/test_pollinate_useragent.py
53index c076fbc..abd6daf 100644
54--- a/tests/vmtests/test_pollinate_useragent.py
55+++ b/tests/vmtests/test_pollinate_useragent.py
56@@ -24,7 +24,7 @@ class TestPollinateUserAgent(VMBaseClass):
57 self.output_files_exist(["pollinate_print_user_agent"])
58 agent_values = self.load_collect_file("pollinate_print_user_agent")
59 if len(agent_values) == 0:
60- pollver = re.search('pollinate\s(?P<version>\S+)',
61+ pollver = re.search(r'pollinate\s(?P<version>\S+)',
62 self.load_collect_file("debian-packages.txt"))
63 msg = ("pollinate client '%s' does not support "
64 "--print-user-agent'" % pollver.groupdict()['version'])
65@@ -45,7 +45,7 @@ class TestPollinateUserAgent(VMBaseClass):
66 """
67 ua_val = line.split()[0]
68 # escape + and . that are likely in maas/curtin version strings
69- regex = r'%s' % ua_val.replace('+', '\+').replace('.', '\.')
70+ regex = '%s' % ua_val.replace('+', r'\+').replace('.', r'\.')
71 hit = re.search(regex, agent_values)
72 self.assertIsNotNone(hit)
73 self.assertEqual(ua_val, hit.group())

Subscribers

People subscribed via source and target branches