-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
214 lines (139 loc) · 4.58 KB
/
setup.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- python -*-
import os, stat, sys
from distutils.core import setup, Extension, Command
from distutils.errors import DistutilsExecError
from distutils.command.install import install as base_install
version = '1.3.0'
bibtex = [
'accents.c',
'author.c',
'bibparse.c',
'biblex.c',
'bibtex.c',
'bibtexmodule.c',
'entry.c',
'field.c',
'reverse.c',
'source.c',
'stringutils.c',
'struct.c'
]
def error (msg):
sys.stderr.write ('setup.py: error: %s\n' % msg)
return
# Obtain path for Glib
includes = []
libs = []
libdirs = []
def pread (cmd):
fd = os.popen (cmd)
data = fd.read ()
return (data, fd.close ())
include, ix = pread ('pkg-config glib-2.0 --cflags')
library, lx = pread ('pkg-config glib-2.0 --libs')
if ix or lx:
error ('cannot find Glib-2.0 installation parameters.')
error ('please check that your glib-2.0 development package')
error ('has been installed.')
sys.exit (1)
# Split the path into pieces
for inc in include.split (' '):
inc = inc.strip ()
if not inc: continue
if inc [:2] == '-I':
includes.append (inc [2:])
for lib in library.split (' '):
lib = lib.strip ()
if not lib: continue
if lib [:2] == '-l':
libs.append (lib [2:])
if lib [:2] == '-L':
libdirs.append (lib [2:])
# Check the state of the generated lex and yacc files
def rebuild (src, deps):
st = os.stat (src) [stat.ST_MTIME]
for dep in deps:
try: dt = os.stat (dep) [stat.ST_MTIME]
except OSError: return True
if st > dt: return True
return False
def rename (src, dst):
try: os.unlink (dst)
except OSError: pass
os.rename (src, dst)
return
if rebuild ('bibparse.y', ['bibparse.c',
'bibparse.h']):
print("rebuilding from bibparse.y")
os.system ('bison -y -d -t -p bibtex_parser_ bibparse.y')
rename ('y.tab.c', 'bibparse.c')
rename ('y.tab.h', 'bibparse.h')
if rebuild ('biblex.l', ['biblex.c']):
print("rebuilding from biblex.l")
os.system ('flex -Pbibtex_parser_ biblex.l')
rename ('lex.bibtex_parser_.c', 'biblex.c')
class run_check (Command):
""" Run all of the tests for the package using uninstalled (local)
files """
description = "Automatically run the test suite for the package."
user_options = []
def initialize_options(self):
self.build_lib = None
return
def finalize_options(self):
# Obtain the build_lib directory from the build command
self.set_undefined_options ('build', ('build_lib', 'build_lib'))
return
def run(self):
# Ensure the extension is built
self.run_command ('build')
# test the uninstalled extensions
libdir = os.path.join (os.getcwd (), self.build_lib)
sys.path.insert (0, libdir)
if sys.version_info < (3,0):
import testsuite
else:
import testsuite3 as testsuite
try:
failures = testsuite.run ()
except RuntimeError as msg:
sys.stderr.write ('error: %s\n' % msg)
raise DistutilsExecError ('please consult the "Troubleshooting" section in the README file.')
if failures > 0:
raise DistutilsExecError ('check failed.')
return
class run_install (base_install):
def run(self):
# The code must pass the tests before being installed
self.run_command ('check')
base_install.run (self)
return
# Actual compilation
setup (name = "python-bibtex",
version = version,
description = "A Python extension to parse BibTeX files",
author = "Frederic Gobry",
author_email = 'gobry@pybliographer.org',
url = 'http://pybliographer.org/',
license = 'GPL',
cmdclass = { 'check': run_check,
'install': run_install },
long_description = \
'''
This module contains two extensions needed for pybliographer:
- a bibtex parser
- a simple binding to GNU Recode
It requires the following libraries to be installed:
- Glib-2.0 (and its development headers)
- GNU Recode 3.5 (and its development headers)
''',
ext_modules = [
Extension("_bibtex", bibtex,
include_dirs = includes,
library_dirs = libdirs,
define_macros = [('G_LOG_DOMAIN', '"BibTeX"')],
libraries = libs + ['recode']),
Extension("_recode", ["recodemodule.c"],
include_dirs = includes,
libraries = ['recode'])
])