[jhbuild] get_installed_pkgconfigs: improve error handling, add logging



commit ecd100420f017ea2ab5d53bbb9c79ce051c5eb94
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Sun Jan 12 23:08:41 2020 +0100

    get_installed_pkgconfigs: improve error handling, add logging
    
    Port to newer Python 3 API, don't wrap everything with try/catch,
    and add some logging for failed pkg-config commands.
    
    See #54

 jhbuild/utils/systeminstall.py | 60 ++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 29 deletions(-)
---
diff --git a/jhbuild/utils/systeminstall.py b/jhbuild/utils/systeminstall.py
index 24ace1be..610be80d 100644
--- a/jhbuild/utils/systeminstall.py
+++ b/jhbuild/utils/systeminstall.py
@@ -35,38 +35,40 @@ from . import _, udecode
 def get_installed_pkgconfigs(config):
     """Returns a dictionary mapping pkg-config names to their current versions on the system."""
     pkgversions = {}
+    cmd = ['pkg-config', '--list-all']
     try:
-        proc = subprocess.Popen(['pkg-config', '--list-all'], stdout=subprocess.PIPE, close_fds=True)
-        stdout = udecode(proc.communicate()[0])
-        proc.wait()
-        pkgs = []
-        for line in TextIO(stdout):
-            pkg, rest = line.split(None, 1)
-            pkgs.append(pkg)
-
-        # see if we can get the versions "the easy way"
-        try:
-            stdout = subprocess.check_output(['pkg-config', '--modversion'] + pkgs)
-            stdout = udecode(stdout)
-            versions = stdout.splitlines()
-            if len(versions) == len(pkgs):
-                return dict(zip(pkgs, versions))
-        except (subprocess.CalledProcessError, OSError):
-            pass
-
-        # We have to rather inefficiently repeatedly fork to work around
-        # broken pkg-config installations - if any package has a missing
-        # dependency pkg-config will fail entirely.
-        for pkg in pkgs:
-            args = ['pkg-config', '--modversion']
-            args.append(pkg)
-            proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
-            stdout = proc.communicate()[0]
-            stdout = udecode(stdout)
-            proc.wait()
-            pkgversions[pkg] = stdout.strip()
+        stdout = subprocess.check_output(cmd, universal_newlines=True)
     except (subprocess.CalledProcessError, OSError): # pkg-config not installed
+        logging.error("{} failed".format(cmd))
+        return pkgversions
+
+    pkgs = []
+    for line in stdout.splitlines():
+        pkg, rest = line.split(None, 1)
+        pkgs.append(pkg)
+
+    # see if we can get the versions "the easy way"
+    try:
+        stdout = subprocess.check_output(['pkg-config', '--modversion'] + pkgs, universal_newlines=True)
+    except (subprocess.CalledProcessError, OSError):
         pass
+    else:
+        versions = stdout.splitlines()
+        if len(versions) == len(pkgs):
+            return dict(zip(pkgs, versions))
+
+    # We have to rather inefficiently repeatedly fork to work around
+    # broken pkg-config installations - if any package has a missing
+    # dependency pkg-config will fail entirely.
+    for pkg in pkgs:
+        cmd = ['pkg-config', '--modversion', pkg]
+        try:
+            stdout = subprocess.check_output(cmd, universal_newlines=True)
+        except (subprocess.CalledProcessError, OSError):
+            logging.error("{} failed".format(cmd))
+            continue
+        pkgversions[pkg] = stdout.strip()
+
     return pkgversions
 
 def get_uninstalled_pkgconfigs(uninstalled):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]