chiark / gitweb /
Initial import.
[mLib-python] / setup.py
1 from distutils.core import setup, Extension
2 from Pyrex.Distutils import build_ext
3 from os import *
4 from errno import *
5 import sre
6 import sys
7 from sys import stdin, stdout, stderr
8
9 incdirs = []
10 libdirs = []
11 libs = []
12
13 def progoutput(cmd):
14   p = popen(cmd)
15   out = p.readline()
16   if p.read() != '': raise 'extra junk from %s' % cmd
17   p.close()
18   return out.rstrip('\n')
19
20 def libconfig(lib, ver):
21   config = lib + '-config'
22   if system('%s --check %s' % (config, ver)):
23     raise '%s version %s not found' % (lib, ver)
24   version = progoutput('%s --version' % config)
25   for i in progoutput('%s --cflags' % config).split():
26     if i[:2] == '-I': incdirs.append(i[2:])
27     else: raise 'strange cflags item %s' % i
28   for i in progoutput('%s --libs' % config).split():
29     if i[:2] == '-L': libdirs.append(i[2:])
30     elif i[:2] == '-l': libs.append(i[2:])
31     else: raise 'strange libs item %s' % i
32
33 def uniquify(l):
34   u = {}
35   o = []
36   for i in l:
37     if i not in u:
38       o.append(i)
39       u[i] = 1
40   return o
41
42 libconfig('catacomb', '2.1.0')
43 libconfig('mLib', '2.0.3')
44
45 def needs_update_p(target, sources):
46   if not path.exists(target): return True
47   t_target = stat(target).st_mtime
48   for s in sources:
49     if stat(s).st_mtime > t_target: return True
50   return False
51
52 rx_subst = sre.compile(r'\%(\w+)\%')
53
54 def getsource(src):
55   br = src.find('[')
56   if br >= 0:
57     if src[-1] != ']':
58       raise SyntaxError, 'bad auto file'
59     subst = src[br + 1:-1]
60     src = src[:br]
61     x = subst.split(':')
62     infile = x[0]
63     if needs_update_p(src, [infile]):
64       print 'creating %s from %s...' % (src, infile)
65       d = dict([i.split('/', 1) for i in x[1:]])
66       out = file(src + '.new', 'w')
67       for line in file(infile):
68         out.write(rx_subst.sub((lambda m: d[m.group(1)]), line))
69       out.close()
70       rename(src + '.new', src)
71   return src
72
73 def mlibext(src):
74   col = src.find('!')
75   if col >= 0:
76     mod = src[:col]
77     srcs = [getsource(s) for s in src[col + 1:].split(',')]
78   else:
79     src = getsource(src)
80     mod, hunoz = src.split('.', 1)
81     srcs = [src]
82   return Extension('mLib.' + mod, srcs,
83                    ##extra_compile_args = ['-O0'],
84                    include_dirs = uniquify(incdirs),
85                    library_dirs = uniquify(libdirs),
86                    libraries = uniquify(libs))
87
88 setup(name = 'mLib-python',
89       version = '1.0.0',
90       description = 'Python interface to mLib utilities library',
91       author = 'Straylight/Edgeware',
92       author_email = 'mdw@distorted.org.uk',
93       license = 'GNU General Public License',
94       packages = ['mLib'],
95       ext_modules = [mlibext(x) for x in '''
96         select.pyx crc32.pyx unihash.pyx report.pyx
97         base64.pyx[codec.pyx.in:PREFIX/base64]
98         base32.pyx[codec.pyx.in:PREFIX/base32]
99         hex.pyx[codec.pyx.in:PREFIX/hex]
100         array.c sym.pyx atom!atom-base.c,atom.pyx
101         '''.split()],
102       cmdclass = {'build_ext': build_ext})