File: misc.py

package info (click to toggle)
qpid-proton 0.37.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,384 kB
  • sloc: ansic: 37,828; cpp: 37,140; python: 15,302; ruby: 6,018; xml: 477; sh: 320; pascal: 52; makefile: 18
file content (85 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -----------------------------------------------------------------------------
#  Copyright (C) PyZMQ Developers
#  Distributed under the terms of the Modified BSD License.
#
#  This bundling code is largely adapted from pyzmq-static's get.sh by
#  Brandon Craig-Rhodes, which is itself BSD licensed.
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
#  These functions were largely adapted from pyzmq's code
#  PyZMQ Developers, which is itself Modified BSD licensed.
# -----------------------------------------------------------------------------


import errno
import os
import subprocess
import sys

from . import log


def _call_pkg_config(args):
    """Spawn a subprocess running pkg-config with the given args.

    :param args: list of strings to pass to pkg-config's command line.
    Refer to pkg-config's documentation for more detail.

    Return the Popen object, or None if the command failed
    """
    try:
        return subprocess.Popen(['pkg-config'] + args,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                universal_newlines=True)
    except OSError as e:
        if e.errno == errno.ENOENT:
            log.warn("command not found: pkg-config")
        else:
            log.warn("Running pkg-config failed - %s." % e)
    return None


def pkg_config_version_installed(package, version=None, atleast=None):
    """Check if version of a package is is installed

    This function returns True/False depending on whether
    the package is found and is the correct version.

    :param version: The exact version of the package required
    :param atleast: True if installed package is at least this version
    """

    if version is None and atleast is None:
        log.fatal('package version string required')
    elif version and atleast:
        log.fatal('Specify either version or atleast, not both')

    check = 'exact' if version else 'atleast'
    p = _call_pkg_config(['--%s-version=%s' % (check,
                                               version or atleast),
                          package])
    if p:
        out, err = p.communicate()
        if p.returncode:
            log.info("Did not find %s via pkg-config: %s" % (package, err))
            return False
        log.info("Using %s version %s (found via pkg-config)" %
                 (package,
                  _call_pkg_config(['--modversion', package]).communicate()[0].splitlines()[0]))
        return True
    return False


def pkg_config_get_var(package, name):
    """Retrieve the value of the named package variable as a string
    """
    p = _call_pkg_config(['--variable=%s' % name, package])
    if not p:
        log.warn("pkg-config: var %s get failed, package %s", name, package)
        return ""
    out, err = p.communicate()
    if p.returncode:
        out = ""
        log.warn(err)
    return out.splitlines()[0]