diff options
author | David Cournapeau <cournape@gmail.com> | 2009-07-26 11:08:14 +0000 |
---|---|---|
committer | David Cournapeau <cournape@gmail.com> | 2009-07-26 11:08:14 +0000 |
commit | 9530c356e4dff9991fd4d0eefe2bc740e35e8d69 (patch) | |
tree | f41d5994cd34c6d74f94541bcfe4da49a9bd0831 /numpy/distutils/npy_pkg_config.py | |
parent | dc23f77c86ce98b9099f8aeb396e7a5fdb6ec810 (diff) | |
download | numpy-9530c356e4dff9991fd4d0eefe2bc740e35e8d69.tar.gz |
we now implement cflags, libs, variables handling and basic depends - the code is a mess, though.
Diffstat (limited to 'numpy/distutils/npy_pkg_config.py')
-rw-r--r-- | numpy/distutils/npy_pkg_config.py | 170 |
1 files changed, 117 insertions, 53 deletions
diff --git a/numpy/distutils/npy_pkg_config.py b/numpy/distutils/npy_pkg_config.py index d93a374aa..b508ca696 100644 --- a/numpy/distutils/npy_pkg_config.py +++ b/numpy/distutils/npy_pkg_config.py @@ -3,12 +3,6 @@ import re VAR = re.compile('\$\{([a-zA-Z0-9_-]+)\}') -cfg = 'config.ini' - -config = SafeConfigParser() -n = config.read(cfg) -#print "Reading: ", n - class FormatError(IOError): def __init__(self, msg): self.msg = msg @@ -16,8 +10,8 @@ class FormatError(IOError): def __str__(self): return self.msg -class MetaInfo(object): - def __init__(self, name, description, version, requires=None): +class LibraryInfo(object): + def __init__(self, name, description, version, sections, requires=None): self.name = name self.description = description if requires: @@ -25,6 +19,20 @@ class MetaInfo(object): else: self.requires = [] self.version = version + self._sections = sections + + def sections(self): + return self._sections.keys() + + def cflags(self, section="default"): + if not self._sections[section].has_key("cflags"): + return "" + return self._sections[section]["cflags"] + + def libs(self, section="default"): + if not self._sections[section].has_key("libs"): + return "" + return self._sections[section]["libs"] def __str__(self): m = ['Name: %s' % self.name] @@ -33,21 +41,13 @@ class MetaInfo(object): m.append('Requires:') else: m.append('Requires: %s' % ",".join(self.requires)) + m.append('Version: %s' % self.version) return "\n".join(m) - -class PathsInfo(object): - def __init__(self, prefix=None, exec_prefix=None, libdir=None, includedir=None): - self._raw_data = {} - if prefix: - self._raw_data['prefix'] = prefix - if exec_prefix: - self._raw_data['exec_prefix'] = exec_prefix - if libdir: - self._raw_data['libdir'] = libdir - if includedir: - self._raw_data['includedir'] = includedir +class VariableSet(object): + def __init__(self, d): + self._raw_data = dict([(k, v) for k, v in d.items()]) self._re = {} self._re_sub = {} @@ -88,42 +88,106 @@ def parse_meta(config): "but not found" % k) if not d.has_key('requires'): - d['requires'] = None + d['requires'] = [] - return MetaInfo(name=d['name'], description=d['description'], version=d['version'], - requires=d['requires']) + return d -def parse_paths(config): - if not config.has_section('default'): - raise FormatError("No default section found !") +def parse_variables(config): + if not config.has_section('variables'): + raise FormatError("No variables section found !") d = {} - paths = ['prefix', 'exec_prefix', 'libdir', 'includedir'] - for p in paths: - try: - d[p] = config.get('default', p) - except NoOptionError: - pass - - return PathsInfo(**d) - -meta = parse_meta(config) -paths_info = parse_paths(config) - -def get_libs(config, paths_info): - l = config.get('default', 'Libs') - return paths_info.interpolate(l) - -def get_cflags(config, paths_info): - c = config.get('default', 'Cflags') - return paths_info.interpolate(c) - -def get_version(meta): - ver = meta.version - print ver + for name, value in config.items("variables"): + d[name] = value -print get_libs(config, paths_info) -print get_cflags(config, paths_info) -print get_version(meta) -#print config.items('default') + return VariableSet(d) + +def parse_sections(config): + return meta_d, r + +def pkg_to_filename(pkg_name): + return "%s.ini" % pkg_name + +# TODO: +# - implements --cflags, --libs +# - implements version comparison (modversion + atleast) +# - implements non default section + +def read_config(filename): + config = SafeConfigParser() + n = config.read(filename) + if not len(n) >= 1: + raise IOError("Could not find file %s" % filename) + + meta_d = parse_meta(config) + varset = parse_variables(config) + + # Parse "normal" sections + secs = config.sections() + secs = [s for s in secs if not s in ["meta", "variables"]] + + r = {} + + # XXX: this is a mess + # XXX: cache the LibraryInfo instances + for s in secs: + d = {} + if config.has_option(s, "depends"): + tmp = read_config(pkg_to_filename(config.get(s, "depends"))) + for name, value in config.items(s): + d[name] = varset.interpolate(value) + if config.has_option(s, "depends") and not name == "depends": + if s in tmp.sections(): + d[name] += ' %s' % getattr(tmp, name)(s) + r[s] = d + + return LibraryInfo(name=meta_d["name"], description=meta_d["description"], + version=meta_d["version"], sections=r) + +if __name__ == '__main__': + import sys + from optparse import OptionParser + import glob + + parser = OptionParser() + parser.add_option("--cflags", dest="cflags", action="store_true", + help="output all preprocessor and compiler flags") + parser.add_option("--libs", dest="libs", action="store_true", + help="output all linker flags") + parser.add_option("--use-section", dest="section", + help="use this section instead of default for options") + parser.add_option("--version", dest="version", action="store_true", + help="output version") + parser.add_option("--atleast-version", dest="min_version", + help="Minimal version") + parser.add_option("--list-all", dest="list_all", action="store_true", + help="Minimal version") + + (options, args) = parser.parse_args(sys.argv) + + if len(args) < 2: + raise ValueError("Expect package name on the command line:") + + if options.list_all: + files = glob.glob("*.ini") + for f in files: + info = read_config(f) + print "%s\t%s - %s" % (info.name, info.name, info.description) + pkg_name = args[1] + fname = pkg_to_filename(pkg_name) + info = read_config(fname) + + if options.section: + section = options.section + else: + section = "default" + + if options.cflags: + print info.cflags(section) + if options.libs: + print info.libs(section) + if options.version: + print info.version + if options.min_version: + print info.version >= options.min_version |