~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to buildutils/config.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor, Julian Taylor, Jakub Wilk
  • Date: 2013-05-09 15:07:29 UTC
  • mfrom: (2.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130509150729-1xf0eb5wrokflfm7
Tags: 2.2.0.1-2
[ Julian Taylor ]
* upload to unstable
* python-gevent-dbg is fixed, enable python2 dbg autopkgtests
* cython0.19-compat.patch: fix build with cython 0.19

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Config functions"""
 
2
#-----------------------------------------------------------------------------
 
3
#  Copyright (C) 2011 Brian Granger, Min Ragan-Kelley
 
4
#
 
5
#  This file is part of pyzmq, copied and adapted from h5py.
 
6
#  h5py source used under the New BSD license
 
7
#
 
8
#  h5py: <http://code.google.com/p/h5py/>
 
9
#
 
10
#  Distributed under the terms of the New BSD License.  The full license is in
 
11
#  the file COPYING.BSD, distributed as part of this software.
 
12
#-----------------------------------------------------------------------------
 
13
 
 
14
import sys
 
15
import os
 
16
import json
 
17
 
 
18
try:
 
19
    from configparser import ConfigParser
 
20
except:
 
21
    from ConfigParser import ConfigParser
 
22
 
 
23
pjoin = os.path.join
 
24
from .msg import debug, fatal, warn
 
25
 
 
26
#-----------------------------------------------------------------------------
 
27
# Utility functions (adapted from h5py: http://h5py.googlecode.com)
 
28
#-----------------------------------------------------------------------------
 
29
 
 
30
 
 
31
def load_config(name):
 
32
    """Load config dict from JSON"""
 
33
    fname = pjoin('conf', name+'.json')
 
34
    if not os.path.exists(fname):
 
35
        return None
 
36
    try:
 
37
        with open(fname) as f:
 
38
            cfg = json.load(f)
 
39
    except Exception as e:
 
40
        warn("Couldn't load %s: %s" % (fname, e))
 
41
        cfg = None
 
42
    return cfg
 
43
 
 
44
 
 
45
def save_config(name, data):
 
46
    """Save config dict to JSON"""
 
47
    if not os.path.exists('conf'):
 
48
        os.mkdir('conf')
 
49
    fname = pjoin('conf', name+'.json')
 
50
    with open(fname, 'w') as f:
 
51
        json.dump(data, f, indent=2)
 
52
 
 
53
 
 
54
def v_str(v_tuple):
 
55
    """turn (2,0,1) into '2.0.1'."""
 
56
    return ".".join(str(x) for x in v_tuple)
 
57
 
 
58
def get_eargs():
 
59
    """ Look for options in environment vars """
 
60
 
 
61
    settings = {}
 
62
 
 
63
    zmq = os.environ.get("ZMQ_DIR", '')
 
64
    if zmq != '':
 
65
        debug("Found environ var ZMQ_DIR=%s" % zmq)
 
66
        settings['zmq'] = zmq
 
67
 
 
68
    return settings
 
69
 
 
70
def get_cfg_args():
 
71
    """ Look for options in setup.cfg """
 
72
 
 
73
    settings = {}
 
74
    zmq = ''
 
75
    if not os.path.exists('setup.cfg'):
 
76
        return settings
 
77
    cfg = ConfigParser()
 
78
    cfg.read('setup.cfg')
 
79
    if 'build_ext' in cfg.sections() and \
 
80
                cfg.has_option('build_ext', 'include_dirs'):
 
81
        includes = cfg.get('build_ext', 'include_dirs')
 
82
        include = includes.split(os.pathsep)[0]
 
83
        if include.endswith('include') and os.path.isdir(include):
 
84
            zmq = include[:-8]
 
85
    if zmq != '':
 
86
        debug("Found ZMQ=%s in setup.cfg" % zmq)
 
87
        settings['zmq'] = zmq
 
88
 
 
89
    return settings
 
90
 
 
91
def get_cargs():
 
92
    """ Look for global options in the command line """
 
93
    settings = load_config('buildconf')
 
94
    if settings is None:  settings = {}
 
95
    for arg in sys.argv[:]:
 
96
        if arg.find('--zmq=') == 0:
 
97
            zmq = arg.split('=')[-1]
 
98
            if zmq.lower() in ('default', 'auto', ''):
 
99
                settings.pop('zmq', None)
 
100
            else:
 
101
                settings['zmq'] = zmq
 
102
            sys.argv.remove(arg)
 
103
    save_config('buildconf', settings)
 
104
    return settings
 
105
 
 
106
def discover_settings():
 
107
    """ Discover custom settings for ZMQ path"""
 
108
    settings = get_cfg_args()       # lowest priority
 
109
    settings.update(get_eargs())
 
110
    settings.update(get_cargs())    # highest priority
 
111
    return settings.get('zmq')