Debian Codegraph @

python/setuputils/misc.py

raw
1 # -----------------------------------------------------------------------------
2 # Copyright (C) PyZMQ Developers
3 # Distributed under the terms of the Modified BSD License.
4 #
5 # This bundling code is largely adapted from pyzmq-static's get.sh by
6 # Brandon Craig-Rhodes, which is itself BSD licensed.
7 # -----------------------------------------------------------------------------
8
9 # -----------------------------------------------------------------------------
10 # These functions were largely adapted from pyzmq's code
11 # PyZMQ Developers, which is itself Modified BSD licensed.
12 # -----------------------------------------------------------------------------
13
14
15 import errno
16 import os
17 import subprocess
18 import sys
19
20 from . import log
21
22
23 def _call_pkg_config(args):
24 """Spawn a subprocess running pkg-config with the given args.
25
26 :param args: list of strings to pass to pkg-config's command line.
27 Refer to pkg-config's documentation for more detail.
28
29 Return the Popen object, or None if the command failed
30 """
31 try:
32 return subprocess.Popen(['pkg-config'] + args,
33 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
34 universal_newlines=True)
35 except OSError as e:
36 if e.errno == errno.ENOENT:
37 log.warn("command not found: pkg-config")
38 else:
39 log.warn("Running pkg-config failed - %s." % e)
40 return None
41
42
43 def pkg_config_version_installed(package, version=None, atleast=None):
44 """Check if version of a package is is installed
45
46 This function returns True/False depending on whether
47 the package is found and is the correct version.
48
49 :param version: The exact version of the package required
50 :param atleast: True if installed package is at least this version
51 """
52
53 if version is None and atleast is None:
54 log.fatal('package version string required')
55 elif version and atleast:
56 log.fatal('Specify either version or atleast, not both')
57
58 check = 'exact' if version else 'atleast'
59 p = _call_pkg_config(['--%s-version=%s' % (check,
60 version or atleast),
61 package])
62 if p:
63 out, err = p.communicate()
64 if p.returncode:
65 log.info("Did not find %s via pkg-config: %s" % (package, err))
66 return False
67 log.info("Using %s version %s (found via pkg-config)" %
68 (package,
69 _call_pkg_config(['--modversion', package]).communicate()[0].splitlines()[0]))
70 return True
71 return False
72
73
74 def pkg_config_get_var(package, name):
75 """Retrieve the value of the named package variable as a string
76 """
77 p = _call_pkg_config(['--variable=%s' % name, package])
78 if not p:
79 log.warn("pkg-config: var %s get failed, package %s", name, package)
80 return ""
81 out, err = p.communicate()
82 if p.returncode:
83 out = ""
84 log.warn(err)
85 return out.splitlines()[0]