Skip to contentSkip to conversation

utils/genrandconfig: switch to async/await format

PATCH In review James Hilliard 2 participants 3 messages Feb 6, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 utils/genrandconfig: switch to async/await format

This requires python 3.6 or newer but is a bit cleaner.

---
 utils/genrandconfig | 62 +++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 39 deletions(-)
utils/genrandconfig +23 -39
index b3576f8a51..8947898d88 100755
@@ -28,11 +28,6 @@ import traceback
 from distutils.version import StrictVersion
 import platform
 
-if sys.version_info < (3, 8):
-    from asyncio import coroutine
-else:
-    from types import coroutine
-
 
 class SystemInfo:
     DEFAULT_NEEDED_PROGS = ["make", "git", "gcc", "timeout"]
@@ -60,8 +55,7 @@ class SystemInfo:
         # --
         return None
 
-    @coroutine
-    def has(self, prog):
+    async def has(self, prog):
         """Checks whether a program is available.
         Lazily evaluates missing entries.
 
@@ -76,11 +70,11 @@ class SystemInfo:
         have_it = self.find_prog(prog)
         # java[c] needs special care
         if have_it and prog in ('java', 'javac'):
-            proc = yield from asyncio.create_subprocess_shell(
+            proc = await asyncio.create_subprocess_shell(
                 "%s -version | grep gcj" % prog,
                 stdout=asyncio.subprocess.DEVNULL,
                 stderr=asyncio.subprocess.DEVNULL)
-            ret = yield from proc.wait()
+            ret = await proc.wait()
             if ret != 1:
                 have_it = False
         # --
@@ -159,8 +153,7 @@ def get_toolchain_configs(toolchains_csv, buildrootdir):
     return configs
 
 
-@coroutine
-def is_toolchain_usable(configfile, config):
+async def is_toolchain_usable(configfile, config):
     """Check if the toolchain is actually usable."""
 
     with open(configfile) as configf:
@@ -180,9 +173,9 @@ def is_toolchain_usable(configfile, config):
            'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64=y\n' in configlines or \
            'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64_BE=y\n' in configlines or \
            'BR2_TOOLCHAIN_EXTERNAL_LINARO_ARMEB=y\n' in configlines:
-            proc = yield from asyncio.create_subprocess_exec(
+            proc = await asyncio.create_subprocess_exec(
                 'ldd', '--version', stdout=asyncio.subprocess.PIPE)
-            ldd_version_output, _ = yield from proc.communicate()
+            ldd_version_output, _ = await proc.communicate()
             if proc.returncode:
                 return False
             glibc_version = ldd_version_output.decode().splitlines()[0].split()[-1]
@@ -193,8 +186,7 @@ def is_toolchain_usable(configfile, config):
     return True
 
 
-@coroutine
-def fixup_config(sysinfo, configfile):
+async def fixup_config(sysinfo, configfile):
     """Finalize the configuration and reject any problematic combinations
 
     This function returns 'True' when the configuration has been
@@ -210,8 +202,7 @@ def fixup_config(sysinfo, configfile):
 
     BR2_TOOLCHAIN_EXTERNAL_URL = 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/'
 
-    has_java = yield from sysinfo.has("java")
-    if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not has_java:
+    if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not await sysinfo.has("java"):
         return False
     # The ctng toolchain is affected by PR58854
     if 'BR2_PACKAGE_LTTNG_TOOLS=y\n' in configlines and \
@@ -654,8 +645,7 @@ def fixup_config(sysinfo, configfile):
     return True
 
 
-@coroutine
-def gen_config(args):
+async def gen_config(args):
     """Generate a new random configuration
 
     This function generates the configuration, by choosing a random
@@ -713,8 +703,7 @@ def gen_config(args):
 
     # Randomly enable BR2_REPRODUCIBLE 10% of times
     # also enable tar filesystem images for testing
-    has_diffoscope = yield from sysinfo.has("diffoscope")
-    if has_diffoscope and randint(0, 10) == 0:
+    if await sysinfo.has("diffoscope") and randint(0, 10) == 0:
         configlines.append("BR2_REPRODUCIBLE=y\n")
         configlines.append("BR2_TARGET_ROOTFS_TAR=y\n")
 
@@ -728,14 +717,13 @@ def gen_config(args):
     with open(configfile, "w+") as configf:
         configf.writelines(configlines)
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
         "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "olddefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
     if ret:
         return ret
 
-    toolchain_usable = yield from is_toolchain_usable(configfile, toolchainconfig)
-    if not toolchain_usable:
+    if not await is_toolchain_usable(configfile, toolchainconfig):
         return 2
 
     # Now, generate the random selection of packages, and fixup
@@ -749,37 +737,33 @@ def gen_config(args):
                   file=sys.stderr)
             return 1
         bounded_loop -= 1
-        make_rand = [
+        proc = await asyncio.create_subprocess_exec(
             "make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
             "KCONFIG_SEED=0x%s" % hexlify(os.urandom(4)).decode("ascii").upper(),
             "KCONFIG_PROBABILITY=%d" % randint(1, 20),
-            "randpackageconfig" if args.toolchains_csv else "randconfig"
-        ]
-        proc = yield from asyncio.create_subprocess_exec(*make_rand)
-        ret = yield from proc.wait()
+            "randpackageconfig" if args.toolchains_csv else "randconfig")
+        ret = await proc.wait()
         if ret:
             return ret
 
-        ret = yield from fixup_config(sysinfo, configfile)
-        if ret:
+        if await fixup_config(sysinfo, configfile):
             break
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
         "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "olddefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
     if ret:
         return ret
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
         "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "savedefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
     if ret:
         return ret
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
         "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "dependencies")
-    ret = yield from proc.wait()
-    return ret
+    return await proc.wait()
 
 
 if __name__ == '__main__':
Signature · 6 lines
2.34.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw
Quoted 1 line · James Hilliard

This requires python 3.6 or newer but is a bit cleaner.

Does it? According to [1]

"The async def type of coroutine was added in Python 3.5"

Regards,
Arnout

[1] https://docs.python.org/3.5/library/asyncio-task.html#coroutines

Quoted 164 lines
---
  utils/genrandconfig | 62 +++++++++++++++++----------------------------
  1 file changed, 23 insertions(+), 39 deletions(-)
utils/genrandconfig +21 -36
index b3576f8a51..8947898d88 100755
@@ -28,11 +28,6 @@ import traceback
  from distutils.version import StrictVersion
  import platform
 
-if sys.version_info < (3, 8):
-    from asyncio import coroutine
-else:
-    from types import coroutine
-
 
  class SystemInfo:
      DEFAULT_NEEDED_PROGS = ["make", "git", "gcc", "timeout"]
@@ -60,8 +55,7 @@ class SystemInfo:
          # --
          return None
 
-    @coroutine
-    def has(self, prog):
+    async def has(self, prog):
          """Checks whether a program is available.
          Lazily evaluates missing entries.
 
@@ -76,11 +70,11 @@ class SystemInfo:
          have_it = self.find_prog(prog)
          # java[c] needs special care
          if have_it and prog in ('java', 'javac'):
-            proc = yield from asyncio.create_subprocess_shell(
+            proc = await asyncio.create_subprocess_shell(
                  "%s -version | grep gcj" % prog,
                  stdout=asyncio.subprocess.DEVNULL,
                  stderr=asyncio.subprocess.DEVNULL)
-            ret = yield from proc.wait()
+            ret = await proc.wait()
              if ret != 1:
                  have_it = False
          # --
@@ -159,8 +153,7 @@ def get_toolchain_configs(toolchains_csv, buildrootdir):
      return configs
 
 
-@coroutine
-def is_toolchain_usable(configfile, config):
+async def is_toolchain_usable(configfile, config):
      """Check if the toolchain is actually usable."""
 
      with open(configfile) as configf:
@@ -180,9 +173,9 @@ def is_toolchain_usable(configfile, config):
             'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64=y\n' in configlines or \
             'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64_BE=y\n' in configlines or \
             'BR2_TOOLCHAIN_EXTERNAL_LINARO_ARMEB=y\n' in configlines:
-            proc = yield from asyncio.create_subprocess_exec(
+            proc = await asyncio.create_subprocess_exec(
                  'ldd', '--version', stdout=asyncio.subprocess.PIPE)
-            ldd_version_output, _ = yield from proc.communicate()
+            ldd_version_output, _ = await proc.communicate()
              if proc.returncode:
                  return False
              glibc_version = ldd_version_output.decode().splitlines()[0].split()[-1]
@@ -193,8 +186,7 @@ def is_toolchain_usable(configfile, config):
      return True
 
 
-@coroutine
-def fixup_config(sysinfo, configfile):
+async def fixup_config(sysinfo, configfile):
      """Finalize the configuration and reject any problematic combinations
 
      This function returns 'True' when the configuration has been
@@ -210,8 +202,7 @@ def fixup_config(sysinfo, configfile):
 
      BR2_TOOLCHAIN_EXTERNAL_URL = 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/'
 
-    has_java = yield from sysinfo.has("java")
-    if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not has_java:
+    if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not await sysinfo.has("java"):
          return False
      # The ctng toolchain is affected by PR58854
      if 'BR2_PACKAGE_LTTNG_TOOLS=y\n' in configlines and \
@@ -654,8 +645,7 @@ def fixup_config(sysinfo, configfile):
      return True
 
 
-@coroutine
-def gen_config(args):
+async def gen_config(args):
      """Generate a new random configuration
 
      This function generates the configuration, by choosing a random
@@ -713,8 +703,7 @@ def gen_config(args):
 
      # Randomly enable BR2_REPRODUCIBLE 10% of times
      # also enable tar filesystem images for testing
-    has_diffoscope = yield from sysinfo.has("diffoscope")
-    if has_diffoscope and randint(0, 10) == 0:
+    if await sysinfo.has("diffoscope") and randint(0, 10) == 0:
          configlines.append("BR2_REPRODUCIBLE=y\n")
          configlines.append("BR2_TARGET_ROOTFS_TAR=y\n")
 
@@ -728,14 +717,13 @@ def gen_config(args):
      with open(configfile, "w+") as configf:
          configf.writelines(configlines)
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "olddefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
      if ret:
          return ret
 
-    toolchain_usable = yield from is_toolchain_usable(configfile, toolchainconfig)
-    if not toolchain_usable:
+    if not await is_toolchain_usable(configfile, toolchainconfig):
          return 2
 
      # Now, generate the random selection of packages, and fixup
@@ -749,37 +737,33 @@ def gen_config(args):
                    file=sys.stderr)
              return 1
          bounded_loop -= 1
-        make_rand = [
+        proc = await asyncio.create_subprocess_exec(
              "make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
              "KCONFIG_SEED=0x%s" % hexlify(os.urandom(4)).decode("ascii").upper(),
              "KCONFIG_PROBABILITY=%d" % randint(1, 20),
-            "randpackageconfig" if args.toolchains_csv else "randconfig"
-        ]
-        proc = yield from asyncio.create_subprocess_exec(*make_rand)
-        ret = yield from proc.wait()
+            "randpackageconfig" if args.toolchains_csv else "randconfig")
+        ret = await proc.wait()
          if ret:
              return ret
 
-        ret = yield from fixup_config(sysinfo, configfile)
-        if ret:
+        if await fixup_config(sysinfo, configfile):
              break
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "olddefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
      if ret:
          return ret
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "savedefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
      if ret:
          return ret

utils/genrandconfig def gen_config(args): · line 769

Quoted 10 lines
utils/genrandconfig +2 -3
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "dependencies")
-    ret = yield from proc.wait()
-    return ret
+    return await proc.wait()
 
 
  if __name__ == '__main__':

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#2 lore raw
Quoted 9 lines · Arnout Vandecappelle
Quoted 1 line · James Hilliard

This requires python 3.6 or newer but is a bit cleaner.

Does it? According to [1]

"The async def type of coroutine was added in Python 3.5"

Hmm, yeah, must have mixed that up with something else that needed 3.6.

Quoted 181 lines

Regards,
Arnout

[1] https://docs.python.org/3.5/library/asyncio-task.html#coroutines

Quoted 173 lines
---
  utils/genrandconfig | 62 +++++++++++++++++----------------------------
  1 file changed, 23 insertions(+), 39 deletions(-)
utils/genrandconfig +23 -39
index b3576f8a51..8947898d88 100755
@@ -28,11 +28,6 @@ import traceback
  from distutils.version import StrictVersion
  import platform
 
-if sys.version_info < (3, 8):
-    from asyncio import coroutine
-else:
-    from types import coroutine
-
 
  class SystemInfo:
      DEFAULT_NEEDED_PROGS = ["make", "git", "gcc", "timeout"]
@@ -60,8 +55,7 @@ class SystemInfo:
          # --
          return None
 
-    @coroutine
-    def has(self, prog):
+    async def has(self, prog):
          """Checks whether a program is available.
          Lazily evaluates missing entries.
 
@@ -76,11 +70,11 @@ class SystemInfo:
          have_it = self.find_prog(prog)
          # java[c] needs special care
          if have_it and prog in ('java', 'javac'):
-            proc = yield from asyncio.create_subprocess_shell(
+            proc = await asyncio.create_subprocess_shell(
                  "%s -version | grep gcj" % prog,
                  stdout=asyncio.subprocess.DEVNULL,
                  stderr=asyncio.subprocess.DEVNULL)
-            ret = yield from proc.wait()
+            ret = await proc.wait()
              if ret != 1:
                  have_it = False
          # --
@@ -159,8 +153,7 @@ def get_toolchain_configs(toolchains_csv, buildrootdir):
      return configs
 
 
-@coroutine
-def is_toolchain_usable(configfile, config):
+async def is_toolchain_usable(configfile, config):
      """Check if the toolchain is actually usable."""
 
      with open(configfile) as configf:
@@ -180,9 +173,9 @@ def is_toolchain_usable(configfile, config):
             'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64=y\n' in configlines or \
             'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64_BE=y\n' in configlines or \
             'BR2_TOOLCHAIN_EXTERNAL_LINARO_ARMEB=y\n' in configlines:
-            proc = yield from asyncio.create_subprocess_exec(
+            proc = await asyncio.create_subprocess_exec(
                  'ldd', '--version', stdout=asyncio.subprocess.PIPE)
-            ldd_version_output, _ = yield from proc.communicate()
+            ldd_version_output, _ = await proc.communicate()
              if proc.returncode:
                  return False
              glibc_version = ldd_version_output.decode().splitlines()[0].split()[-1]
@@ -193,8 +186,7 @@ def is_toolchain_usable(configfile, config):
      return True
 
 
-@coroutine
-def fixup_config(sysinfo, configfile):
+async def fixup_config(sysinfo, configfile):
      """Finalize the configuration and reject any problematic combinations
 
      This function returns 'True' when the configuration has been
@@ -210,8 +202,7 @@ def fixup_config(sysinfo, configfile):
 
      BR2_TOOLCHAIN_EXTERNAL_URL = 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/'
 
-    has_java = yield from sysinfo.has("java")
-    if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not has_java:
+    if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not await sysinfo.has("java"):
          return False
      # The ctng toolchain is affected by PR58854
      if 'BR2_PACKAGE_LTTNG_TOOLS=y\n' in configlines and \
@@ -654,8 +645,7 @@ def fixup_config(sysinfo, configfile):
      return True
 
 
-@coroutine
-def gen_config(args):
+async def gen_config(args):
      """Generate a new random configuration
 
      This function generates the configuration, by choosing a random
@@ -713,8 +703,7 @@ def gen_config(args):
 
      # Randomly enable BR2_REPRODUCIBLE 10% of times
      # also enable tar filesystem images for testing
-    has_diffoscope = yield from sysinfo.has("diffoscope")
-    if has_diffoscope and randint(0, 10) == 0:
+    if await sysinfo.has("diffoscope") and randint(0, 10) == 0:
          configlines.append("BR2_REPRODUCIBLE=y\n")
          configlines.append("BR2_TARGET_ROOTFS_TAR=y\n")
 
@@ -728,14 +717,13 @@ def gen_config(args):
      with open(configfile, "w+") as configf:
          configf.writelines(configlines)
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "olddefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
      if ret:
          return ret
 
-    toolchain_usable = yield from is_toolchain_usable(configfile, toolchainconfig)
-    if not toolchain_usable:
+    if not await is_toolchain_usable(configfile, toolchainconfig):
          return 2
 
      # Now, generate the random selection of packages, and fixup
@@ -749,37 +737,33 @@ def gen_config(args):
                    file=sys.stderr)
              return 1
          bounded_loop -= 1
-        make_rand = [
+        proc = await asyncio.create_subprocess_exec(
              "make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
              "KCONFIG_SEED=0x%s" % hexlify(os.urandom(4)).decode("ascii").upper(),
              "KCONFIG_PROBABILITY=%d" % randint(1, 20),
-            "randpackageconfig" if args.toolchains_csv else "randconfig"
-        ]
-        proc = yield from asyncio.create_subprocess_exec(*make_rand)
-        ret = yield from proc.wait()
+            "randpackageconfig" if args.toolchains_csv else "randconfig")
+        ret = await proc.wait()
          if ret:
              return ret
 
-        ret = yield from fixup_config(sysinfo, configfile)
-        if ret:
+        if await fixup_config(sysinfo, configfile):
              break
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "olddefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
      if ret:
          return ret
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "savedefconfig")
-    ret = yield from proc.wait()
+    ret = await proc.wait()
      if ret:
          return ret
 
-    proc = yield from asyncio.create_subprocess_exec(
+    proc = await asyncio.create_subprocess_exec(
          "make", "O=%s" % args.outputdir, "-C", args.buildrootdir, "dependencies")
-    ret = yield from proc.wait()
-    return ret
+    return await proc.wait()
 
 
Quoted 1 line

utils/genrandconfig def gen_config(args): · line 769

Quoted 1 line
utils/genrandconfig +0 -0
  if __name__ == '__main__':

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot