chiark / gitweb /
_u32: Allow LONG_MAX to be stored in a Python int.
[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 derive(target, src, subs):
55   if needs_update_p(target, [src]):
56       out = file(target + '.new', 'w')
57       for line in file(src):
58         out.write(rx_subst.sub((lambda m: subs[m.group(1)]), line))
59       out.close()
60       rename(target + '.new', target)
61
62 derive('base64.pyx', 'codec.pyx.in',
63        {'CLASS': 'Base64', 'PREFIX': 'base64'})
64 derive('base32.pyx', 'codec.pyx.in',
65        {'CLASS': 'Base32', 'PREFIX': 'base32'})
66 derive('hex.pyx', 'codec.pyx.in',
67        {'CLASS': 'Hex', 'PREFIX': 'hex'})
68
69 def mlibext(src):
70   col = src.find('!')
71   if col >= 0:
72     mod = src[:col]
73     srcs = [getsource(s) for s in src[col + 1:].split(',')]
74   else:
75     src = getsource(src)
76     mod, hunoz = src.split('.', 1)
77     srcs = [src]
78
79 mlib = Extension('mLib', ['mLib.pyx', 'atom-base.c', 'array.c'],
80                  
81                  ##extra_compile_args = ['-O0'],
82                  include_dirs = uniquify(incdirs),
83                  library_dirs = uniquify(libdirs),
84                  libraries = uniquify(libs))
85
86 setup(name = 'mLib-python',
87       version = '1.0.0',
88       description = 'Python interface to mLib utilities library',
89       author = 'Straylight/Edgeware',
90       author_email = 'mdw@distorted.org.uk',
91       license = 'GNU General Public License',
92       ext_modules = [mlib],
93       cmdclass = {'build_ext': build_ext})