Skip to contentSkip to conversation

Switch to Python 3.x flake8

PATCH New Thomas Petazzoni 2 participants 16 messages Aug 12, 2020 – Aug 14, 2020

lore
Share & tools
lore
#0 lore raw

cover Switch to Python 3.x flake8

Hello,

Now that we are using the "async" keyword and related Python 3.x functionality in support/scripts/pkg-stats, our check-flake8 Gitlab CI job no longer passes:

https://gitlab.com/buildroot.org/buildroot/-/jobs/681711009

It borks on the "async" keyword. The purpose of this series is to switch to using the Python 3.x flake8.

Patches 1 to 5 fix existing issues in our Python codebase which were accepted by the Python 2.x flake8, but not the Python 3.x flake8.

Patch 6 changes our Dockerfile to install python3-flake8 instead of python-flake8, and patch 7 changes our Gitlab CI job to use the Python3 flake8 checker.

Once patch 6 is applied, someone will have to rebuild and upload the Docker image that Buildroot's Gitlab CI is using, and re-adjust patch 7 before applying it to use the new Docker image.

Thanks,

Thomas

Thomas Petazzoni (7):
utils/{check-package,checkpackagelib}: consistently use raw strings
for re.compile
support/testing: consistently use raw strings for re.compile support/testing/tests/core/test_timezone.py: fix indentation utils/getdeveloperlib.py: use raw strings for re.compile/re.match utils/scanpypi: use raw strings in re.compile/re.sub support/docker: use python3-flake8 support/misc/gitlab-ci.yml.in: use python3 for flake8

 support/docker/Dockerfile                   |  2 +-
 support/misc/gitlab-ci.yml.in               |  2 +-
 support/testing/infra/__init__.py           |  4 +-
 support/testing/tests/core/test_timezone.py |  8 +--
 utils/check-package                         | 34 ++++++------
 utils/checkpackagelib/lib_config.py         |  4 +-
 utils/checkpackagelib/lib_mk.py             | 60 ++++++++++-----------
 utils/checkpackagelib/lib_patch.py          |  6 +--
 utils/getdeveloperlib.py                    | 10 ++--
 utils/scanpypi                              |  8 +--
 10 files changed, 69 insertions(+), 69 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/7 utils/{check-package, checkpackagelib}: consistently use raw strings for re.compile

Raw strings need to be used when calling re.compile() otherwise Python 3.x flake8 complains with:

W605 invalid escape sequence '\s'

---
 utils/check-package                 | 34 ++++++++--------
 utils/checkpackagelib/lib_config.py |  4 +-
 utils/checkpackagelib/lib_mk.py     | 60 ++++++++++++++---------------
 utils/checkpackagelib/lib_patch.py  |  6 +--
 4 files changed, 52 insertions(+), 52 deletions(-)
utils/check-package +17 -17
index 52317e02f4..dd18d19c25 100755
@@ -46,24 +46,24 @@ def parse_args():
     return parser.parse_args()
 
 
-CONFIG_IN_FILENAME = re.compile("Config\.\S*$")
-DO_CHECK_INTREE = re.compile("|".join([
-    "Config.in",
-    "arch/",
-    "boot/",
-    "fs/",
-    "linux/",
-    "package/",
-    "system/",
-    "toolchain/",
+CONFIG_IN_FILENAME = re.compile(r"Config\.\S*$")
+DO_CHECK_INTREE = re.compile(r"|".join([
+    r"Config.in",
+    r"arch/",
+    r"boot/",
+    r"fs/",
+    r"linux/",
+    r"package/",
+    r"system/",
+    r"toolchain/",
     ]))
-DO_NOT_CHECK_INTREE = re.compile("|".join([
-    "boot/barebox/barebox\.mk$",
-    "fs/common\.mk$",
-    "package/doc-asciidoc\.mk$",
-    "package/pkg-\S*\.mk$",
-    "toolchain/helpers\.mk$",
-    "toolchain/toolchain-external/pkg-toolchain-external\.mk$",
+DO_NOT_CHECK_INTREE = re.compile(r"|".join([
+    r"boot/barebox/barebox\.mk$",
+    r"fs/common\.mk$",
+    r"package/doc-asciidoc\.mk$",
+    r"package/pkg-\S*\.mk$",
+    r"toolchain/helpers\.mk$",
+    r"toolchain/toolchain-external/pkg-toolchain-external\.mk$",
     ]))
 
 
utils/checkpackagelib/lib_config.py +2 -2
index 55c8589d71..c348eec399 100644
@@ -152,8 +152,8 @@ class CommentsMenusPackagesOrder(_CheckFunction):
 
 
 class HelpText(_CheckFunction):
-    HELP_TEXT_FORMAT = re.compile("^\t  .{,62}$")
-    URL_ONLY = re.compile("^(http|https|git)://\S*$")
+    HELP_TEXT_FORMAT = re.compile(r"^\t  .{,62}$")
+    URL_ONLY = re.compile(r"^(http|https|git)://\S*$")
 
     def before(self):
         self.help_text = False
utils/checkpackagelib/lib_mk.py +30 -30

105 lines not on this page · raw message

utils/checkpackagelib/lib_patch.py +3 -3
index 438353ad3b..e4e914b7f0 100644
@@ -11,7 +11,7 @@ from checkpackagelib.lib import NewlineAtEof           # noqa: F401
 
 
 class ApplyOrder(_CheckFunction):
-    APPLY_ORDER = re.compile("\d{1,4}-[^/]*$")
+    APPLY_ORDER = re.compile(r"\d{1,4}-[^/]*$")
 
     def before(self):
         if not self.APPLY_ORDER.match(os.path.basename(self.filename)):
@@ -21,7 +21,7 @@ class ApplyOrder(_CheckFunction):
 
 
 class NumberedSubject(_CheckFunction):
-    NUMBERED_PATCH = re.compile("Subject:\s*\[PATCH\s*\d+/\d+\]")
+    NUMBERED_PATCH = re.compile(r"Subject:\s*\[PATCH\s*\d+/\d+\]")
 
     def before(self):
         self.git_patch = False
@@ -44,7 +44,7 @@ class NumberedSubject(_CheckFunction):
 
 
 class Sob(_CheckFunction):
-    SOB_ENTRY = re.compile("^Signed-off-by: .*$")
+    SOB_ENTRY = re.compile(r"^Signed-off-by: .*$")
 
     def before(self):
         self.found = False
Signature · 1 line
2.26.2
#3 lore raw

PATCH 2/7 support/testing: consistently use raw strings for re.compile

In reply to Thomas Petazzoni

Otherwise Python 3.x flake8 complains with:

W605 invalid escape sequence '\s'

---
 support/testing/infra/__init__.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
support/testing/infra/__init__.py +2 -2
index 6392aa679b..6522a265f3 100644
@@ -78,7 +78,7 @@ def get_elf_arch_tag(builddir, prefix, fpath, tag):
     cmd = ["host/bin/{}-readelf".format(prefix),
            "-A", os.path.join("target", fpath)]
     out = run_cmd_on_host(builddir, cmd)
-    regexp = re.compile("^  {}: (.*)$".format(tag))
+    regexp = re.compile(r"^  {}: (.*)$".format(tag))
     for line in out.splitlines():
         m = regexp.match(line)
         if not m:
@@ -105,7 +105,7 @@ def get_elf_prog_interpreter(builddir, prefix, fpath):
     cmd = ["host/bin/{}-readelf".format(prefix),
            "-l", os.path.join("target", fpath)]
     out = run_cmd_on_host(builddir, cmd)
-    regexp = re.compile("^ *\[Requesting program interpreter: (.*)\]$")
+    regexp = re.compile(r"^ *\[Requesting program interpreter: (.*)\]$")
     for line in out.splitlines():
         m = regexp.match(line)
         if not m:
Signature · 1 line
2.26.2
#5 lore raw

PATCH 3/7 support/testing/tests/core/test_timezone.py: fix indentation

In reply to Thomas Petazzoni

Fixes:

support/testing/tests/core/test_timezone.py:7:9: E117 over-indented

---
 support/testing/tests/core/test_timezone.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
support/testing/tests/core/test_timezone.py +4 -4
index 050624e0aa..f661519196 100644
@@ -4,10 +4,10 @@ import infra.basetest
 
 
 def boot_armv5_cpio(emulator, builddir):
-        img = os.path.join(builddir, "images", "rootfs.cpio")
-        emulator.boot(arch="armv5", kernel="builtin",
-                      options=["-initrd", img])
-        emulator.login()
+    img = os.path.join(builddir, "images", "rootfs.cpio")
+    emulator.boot(arch="armv5", kernel="builtin",
+                  options=["-initrd", img])
+    emulator.login()
 
 
 class TestNoTimezone(infra.basetest.BRTest):
Signature · 1 line
2.26.2
#7 lore raw

PATCH 4/7 utils/getdeveloperlib.py: use raw strings for re.compile/re.match

In reply to Thomas Petazzoni

Fixes the following Python 3.x flake8 warning:

W605 invalid escape sequence '\s'

---
 utils/getdeveloperlib.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
utils/getdeveloperlib.py +5 -5
index 239ffa340b..dc0cc07cc7 100644
@@ -10,7 +10,7 @@ import unittest
 # Patch parsing functions
 #
 
-FIND_INFRA_IN_PATCH = re.compile("^\+\$\(eval \$\((host-)?([^-]*)-package\)\)$")
+FIND_INFRA_IN_PATCH = re.compile(r"^\+\$\(eval \$\((host-)?([^-]*)-package\)\)$")
 
 
 def analyze_patch(patch):
@@ -33,7 +33,7 @@ def analyze_patch(patch):
     return (files, infras)
 
 
-FIND_INFRA_IN_MK = re.compile("^\$\(eval \$\((host-)?([^-]*)-package\)\)$")
+FIND_INFRA_IN_MK = re.compile(r"^\$\(eval \$\((host-)?([^-]*)-package\)\)$")
 
 
 def fname_get_package_infra(fname):
@@ -178,7 +178,7 @@ def parse_arches_from_config_in(fname):
                 parsing_arches = True
                 continue
             if parsing_arches:
-                m = re.match("^\s*default \"([^\"]*)\".*", line)
+                m = re.match(r"^\s*default \"([^\"]*)\".*", line)
                 if m:
                     arches.add(m.group(1))
                 else:
@@ -192,7 +192,7 @@ def parse_developer_architectures(fnames):
     developer is working on."""
     arches = set()
     for fname in fnames:
-        if not re.match("^.*/arch/Config\.in\..*$", fname):
+        if not re.match(r"^.*/arch/Config\.in\..*$", fname):
             continue
         arches = arches | parse_arches_from_config_in(fname)
     return arches
@@ -201,7 +201,7 @@ def parse_developer_architectures(fnames):
 def parse_developer_infras(fnames):
     infras = set()
     for fname in fnames:
-        m = re.match("^package/pkg-([^.]*).mk$", fname)
+        m = re.match(r"^package/pkg-([^.]*).mk$", fname)
         if m:
             infras.add(m.group(1))
     return infras
Signature · 1 line
2.26.2
#9 lore raw

PATCH 5/7 utils/scanpypi: use raw strings in re.compile/re.sub

In reply to Thomas Petazzoni

Fixes the following Python 3.x flake8 warning:

W605 invalid escape sequence '\w'

---
 utils/scanpypi | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
utils/scanpypi +4 -4
index 212dbea85e..51bc249f9e 100755
@@ -96,10 +96,10 @@ def pkg_buildroot_name(pkg_name):
     Keyword arguments:
     pkg_name -- String to rename
     """
-    name = re.sub('[^\w-]', '', pkg_name.lower())
+    name = re.sub(r'[^\w-]', '', pkg_name.lower())
     name = name.replace('_', '-')
     prefix = 'python-'
-    pattern = re.compile('^(?!' + prefix + ')(.+?)$')
+    pattern = re.compile(r'^(?!' + prefix + ')(.+?)$')
     name = pattern.sub(r'python-\1', name)
     return name
 
@@ -337,7 +337,7 @@ class BuildrootPackage():
             self.pkg_req = None
             return set()
         self.pkg_req = self.setup_metadata['install_requires']
-        self.pkg_req = [re.sub('([-.\w]+).*', r'\1', req)
+        self.pkg_req = [re.sub(r'([-.\w]+).*', r'\1', req)
                         for req in self.pkg_req]
 
         # get rid of commented lines and also strip the package strings
@@ -451,7 +451,7 @@ class BuildrootPackage():
                 "Mozilla Public License 2.0": "MPL-2.0",
                 "Zope Public License": "ZPL"
                 }
-            regexp = re.compile('^License :* *.* *:+ (.*)( \(.*\))?$')
+            regexp = re.compile(r'^License :* *.* *:+ (.*)( \(.*\))?$')
             classifiers_licenses = [regexp.sub(r"\1", lic)
                                     for lic in self.metadata['info']['classifiers']
                                     if regexp.match(lic)]
Signature · 1 line
2.26.2
#11 lore raw

PATCH 6/7 support/docker: use python3-flake8

In reply to Thomas Petazzoni

support/scripts/pkg-stats now uses some Python 3.x only constructs ("async" and related keywords), so we must use the Python 3.x flake8.

---
 support/docker/Dockerfile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
support/docker/Dockerfile +1 -1
index 03acde85d2..8c555dcf7a 100644
@@ -36,10 +36,10 @@ RUN apt-get install -y --no-install-recommends \
         libncurses5-dev \
         locales \
         mercurial \
-        python-flake8 \
         python-nose2 \
         python-pexpect \
         python3 \
+        python3-flake8 \
         python3-nose2 \
         python3-pexpect \
         qemu-system-arm \
Signature · 1 line
2.26.2
#12 lore raw

PATCH 6/7 support/docker: use python3-flake8

Hello Thomas,

Quoted 13 lines · Thomas Petazzoni

support/scripts/pkg-stats now uses some Python 3.x only constructs ("async" and related keywords), so we must use the Python 3.x flake8.

---
  support/docker/Dockerfile | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
support/docker/Dockerfile +0 -0
index 03acde85d2..8c555dcf7a 100644

support/docker/Dockerfile RUN apt-get install -y --no-install-recommends \ · line 40

Quoted 6 lines
support/docker/Dockerfile +0 -1
@@ -36,10 +36,10 @@ RUN apt-get install -y --no-install-recommends \
          libncurses5-dev \
          locales \
          mercurial \
-        python-flake8 \
          python-nose2 \
          python-pexpect \

Out of curiosity, why keeping the py2 packages here ? Couldn't all the scripts be run in python3 now ?

support/docker/Dockerfile RUN apt-get install -y --no-install-recommends \ · line 45

Quoted 6 lines
support/docker/Dockerfile +1 -0
          python3 \
+        python3-flake8 \
          python3-nose2 \
          python3-pexpect \
          qemu-system-arm \

Kind regards,

Titouan

#13 lore raw

PATCH 6/7 support/docker: use python3-flake8

Quoted 6 lines · On Fri, 14 Aug 2020 00:20:18 +0200 Titouan Christophe…
Quoted 6 lines
support/docker/Dockerfile +0 -0
index 03acde85d2..8c555dcf7a 100644
@@ -36,10 +36,10 @@ RUN apt-get install -y --no-install-recommends \
          libncurses5-dev \
Quoted 8 lines

support/docker/Dockerfile RUN apt-get install -y --no-install-recommends \ · line 40

Quoted 5 lines
support/docker/Dockerfile +0 -1
          locales \
          mercurial \
-        python-flake8 \
          python-nose2 \
          python-pexpect \  

Out of curiosity, why keeping the py2 packages here ? Couldn't all the scripts be run in python3 now ?

The runtime test infrastructure is using Python 3.x nowadays, so I think we could drop those Python 2.x dependencies.

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#14 lore raw

PATCH 7/7 support/misc/gitlab-ci.yml.in: use python3 for flake8

In reply to Thomas Petazzoni

The pkg-stats script now uses Python3 only constructs (the "async" keyword) and therefore fails to pass the Python2 flake8 test.

Let's use the Python3 flake8 instead.

Fixes:

https://gitlab.com/buildroot.org/buildroot/-/jobs/681711009

---
 support/misc/gitlab-ci.yml.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
support/misc/gitlab-ci.yml.in +1 -1
index 7218ea027e..75ffaab8fe 100644
@@ -25,7 +25,7 @@ check-flake8:
         - find * -type f -print0 | xargs -0 file | grep 'Python script' | cut -d':' -f1 >> files.txt
         - sort -u files.txt | tee files.processed
     script:
-        - python -m flake8 --statistics --count --max-line-length=132 $(cat files.processed)
+        - python3 -m flake8 --statistics --count --max-line-length=132 $(cat files.processed)
     after_script:
         - wc -l files.processed
 
Signature · 1 line
2.26.2