You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by ro...@apache.org on 2020/12/29 13:39:18 UTC

[buildstream] 04/17: Only import pkg_resources when looking for plugins with Pip

This is an automated email from the ASF dual-hosted git repository.

root pushed a commit to branch sam/meson
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit c025f0e486d317a9ebe76a1f1ba98755e652f1e9
Author: Sam Thursfield <sa...@codethink.co.uk>
AuthorDate: Thu Dec 14 15:12:06 2017 +0000

    Only import pkg_resources when looking for plugins with Pip
    
    There is a cost to importing pkg_resources, so we should only do it when
    necessary.
---
 buildstream/_plugincontext.py |  5 ++++-
 meson-python-packages-check   | 24 +++++++++++++++++++++++
 meson.build                   | 44 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/buildstream/_plugincontext.py b/buildstream/_plugincontext.py
index 1759333..290cd57 100644
--- a/buildstream/_plugincontext.py
+++ b/buildstream/_plugincontext.py
@@ -20,7 +20,6 @@
 
 import os
 import inspect
-import pkg_resources
 
 from ._exceptions import PluginError
 from . import utils
@@ -82,6 +81,10 @@ class PluginContext():
         return source
 
     def _get_pip_plugin_source(self, package_name, kind):
+        # Importing pkg_resources can be a slow operation (see:
+        # https://github.com/pypa/setuptools/issues/510) so we only do it when
+        # needed.
+        import pkg_resources
         defaults = None
         if ('pip', package_name) not in self.alternate_sources:
             # key by a tuple to avoid collision
diff --git a/meson-python-packages-check b/meson-python-packages-check
new file mode 100644
index 0000000..59bfe0c
--- /dev/null
+++ b/meson-python-packages-check
@@ -0,0 +1,24 @@
+# Script to detect if a Python module requirement is satisfied.
+#
+# This should be handled by Meson instead of requiring a script, see:
+# https://github.com/mesonbuild/meson/issues/2377
+
+import pkg_resources
+
+import sys
+
+requirements =  sys.argv[1:]
+
+returncode = 0
+
+for requirement in requirements:
+    try:
+        pkg_resources.require(requirement)
+    except pkg_resources.VersionConflict as e:
+        sys.stderr.write("Wanted Python dependency {}, got {}.\n".format(e.req, e.dist))
+        returncode = 1
+    except pkg_resources.DistributionNotFound as e:
+        sys.stderr.write("Required Python dependency {} was not found.\n".format(e.req))
+        returncode = 1
+
+sys.exit(returncode)
diff --git a/meson.build b/meson.build
index d1eae78..e4f8f9b 100644
--- a/meson.build
+++ b/meson.build
@@ -30,6 +30,50 @@ if not python_version.version_compare('>= 3.4')
   error('BuildStream requires Python >= 3.4')
 endif
 
+# FIXME: We check manually for our Python dependencies as Meson doesn't
+# support doing so yet. See https://github.com/mesonbuild/meson/issues/2377
+
+setuptools_check = run_command(python, '-c', 'import pkg_resources')
+if setuptools_check.returncode() != 0
+  error('Python Setuptools was not found; the pkg_resources module is required.\n' +
+        setuptools_check.stderr().strip())
+endif
+
+install_requires = [
+  'blessings',
+  'Click',
+  'jinja2',
+  'pluginbase',
+  'psutil',
+  'ruamel.yaml',
+]
+
+setup_requires = [
+  'setuptools_scm',
+]
+
+test_requires = [
+  'pep8',
+  # Pin coverage to 4.2 for now, we're experiencing
+  # random crashes with 4.4.2
+  'coverage == 4.4.0',
+  'pytest >= 3.1.0',
+  'pytest-cov',
+  'pytest-datafiles',
+  'pytest-env',
+  'pytest-pep8',
+  # Provide option to run tests in parallel, less reliable
+  'pytest-xdist',
+]
+
+missing_dependency_errors = []
+
+message('Checking for Python dependencies')
+modules_check = run_command(python, 'meson-python-packages-check', install_requires + setup_requires + test_requires)
+if modules_check.returncode() != 0
+  error('Python dependency requirements are not satisfied:\n' + modules_check.stderr().strip())
+endif
+
 ##################################################################
 # Bubblewrap requirements
 ##################################################################