root/pyyaml/trunk/setup.py

Revision 294, 8.8 kB (checked in by xi, 2 years ago)

Python 2.3 and win32 compatibility fixes in setup.py.

Line 
1
2 NAME = 'PyYAML'
3 VERSION = '3.06'
4 DESCRIPTION = "YAML parser and emitter for Python"
5 LONG_DESCRIPTION = """\
6 YAML is a data serialization format designed for human readability and
7 interaction with scripting languages.  PyYAML is a YAML parser and
8 emitter for Python.
9
10 PyYAML features a complete YAML 1.1 parser, Unicode support, pickle
11 support, capable extension API, and sensible error messages.  PyYAML
12 supports standard YAML tags and provides Python-specific tags that allow
13 to represent an arbitrary Python object.
14
15 PyYAML is applicable for a broad range of tasks from complex
16 configuration files to object serialization and persistance."""
17 AUTHOR = "Kirill Simonov"
18 AUTHOR_EMAIL = 'xi@resolvent.net'
19 LICENSE = "MIT"
20 PLATFORMS = "Any"
21 URL = "http://pyyaml.org/wiki/PyYAML"
22 DOWNLOAD_URL = "http://pyyaml.org/download/pyyaml/%s-%s.tar.gz" % (NAME, VERSION)
23 CLASSIFIERS = [
24     "Development Status :: 5 - Production/Stable",
25     "Intended Audience :: Developers",
26     "License :: OSI Approved :: MIT License",
27     "Operating System :: OS Independent",
28     "Programming Language :: Python",
29     "Topic :: Software Development :: Libraries :: Python Modules",
30     "Topic :: Text Processing :: Markup",
31 ]
32
33
34 LIBYAML_CHECK = """
35 #include <yaml.h>
36
37 int main(void) {
38     yaml_parser_t parser;
39     yaml_emitter_t emitter;
40
41     yaml_parser_initialize(&parser);
42     yaml_parser_delete(&parser);
43
44     yaml_emitter_initialize(&emitter);
45     yaml_emitter_delete(&emitter);
46
47     return 0;
48 }
49 """
50
51
52 from distutils import log
53 from distutils.core import setup, Command
54 from distutils.core import Distribution as _Distribution
55 from distutils.core import Extension as _Extension
56 from distutils.dir_util import mkpath
57 from distutils.command.build_ext import build_ext as _build_ext
58 from distutils.errors import CompileError, LinkError, DistutilsPlatformError
59
60 try:
61     from Pyrex.Distutils import Extension as _Extension
62     from Pyrex.Distutils import build_ext as _build_ext
63     with_pyrex = True
64 except ImportError:
65     with_pyrex = False
66
67 import sys, os.path
68
69
70 class Distribution(_Distribution):
71
72     def __init__(self, attrs=None):
73         _Distribution.__init__(self, attrs)
74         if not self.ext_modules:
75             return
76         for idx in range(len(self.ext_modules)-1, -1, -1):
77             ext = self.ext_modules[idx]
78             if not isinstance(ext, Extension):
79                 continue
80             setattr(self, ext.attr_name, None)
81             self.global_options = [
82                     (ext.option_name, None,
83                         "include %s (default if %s is available)"
84                         % (ext.feature_description, ext.feature_name)),
85                     (ext.neg_option_name, None,
86                         "exclude %s" % ext.feature_description),
87             ] + self.global_options
88             self.negative_opt = self.negative_opt.copy()
89             self.negative_opt[ext.neg_option_name] = ext.option_name
90
91
92 class Extension(_Extension):
93
94     def __init__(self, name, sources, feature_name, feature_description,
95             feature_check, **kwds):
96         if not with_pyrex:
97             for filename in sources[:]:
98                 base, ext = os.path.splitext(filename)
99                 if ext == '.pyx':
100                     sources.remove(filename)
101                     sources.append('%s.c' % base)
102         _Extension.__init__(self, name, sources, **kwds)
103         self.feature_name = feature_name
104         self.feature_description = feature_description
105         self.feature_check = feature_check
106         self.attr_name = 'with_' + feature_name.replace('-', '_')
107         self.option_name = 'with-' + feature_name
108         self.neg_option_name = 'without-' + feature_name
109
110
111 class build_ext(_build_ext):
112
113     def run(self):
114         optional = True
115         disabled = True
116         for ext in self.extensions:
117             if isinstance(ext, Extension):
118                 with_ext = getattr(self.distribution, ext.attr_name)
119                 if with_ext is None:
120                     disabled = False
121                 elif with_ext:
122                     optional = False
123                     disabled = False
124             else:
125                 optional = False
126                 disabled = False
127                 break
128         if disabled:
129             return
130         try:
131             _build_ext.run(self)
132         except DistutilsPlatformError, exc:
133             if optional:
134                 log.warn(str(exc))
135                 log.warn("skipping build_ext")
136             else:
137                 raise
138
139     def get_source_files(self):
140         self.check_extensions_list(self.extensions)
141         filenames = []
142         for ext in self.extensions:
143             if with_pyrex:
144                 self.pyrex_sources(ext.sources, ext)
145             for filename in ext.sources:
146                 filenames.append(filename)
147                 base = os.path.splitext(filename)[0]
148                 for ext in ['c', 'h', 'pyx', 'pxd']:
149                     filename = '%s.%s' % (base, ext)
150                     if filename not in filenames and os.path.isfile(filename):
151                         filenames.append(filename)
152         return filenames
153
154     def build_extensions(self):
155         self.check_extensions_list(self.extensions)
156         for ext in self.extensions:
157             if isinstance(ext, Extension):
158                 with_ext = getattr(self.distribution, ext.attr_name)
159                 if with_ext is None:
160                     with_ext = self.check_extension_availability(ext)
161                 if not with_ext:
162                     continue
163             if with_pyrex:
164                 ext.sources = self.pyrex_sources(ext.sources, ext)
165             self.build_extension(ext)
166
167     def check_extension_availability(self, ext):
168         cache = os.path.join(self.build_temp, 'check_%s.out' % ext.feature_name)
169         if not self.force and os.path.isfile(cache):
170             data = open(cache).read().strip()
171             if data == '1':
172                 return True
173             elif data == '0':
174                 return False
175         mkpath(self.build_temp)
176         src = os.path.join(self.build_temp, 'check_%s.c' % ext.feature_name)
177         open(src, 'w').write(ext.feature_check)
178         log.info("checking if %s compiles" % ext.feature_name)
179         try:
180             [obj] = self.compiler.compile([src],
181                     macros=ext.define_macros+[(undef,) for undef in ext.undef_macros],
182                     include_dirs=ext.include_dirs,
183                     extra_postargs=(ext.extra_compile_args or []),
184                     depends=ext.depends)
185         except CompileError:
186             log.warn("%s appears not to be installed" % ext.feature_name)
187             log.warn("(if %s is installed, you may need to specify"
188                     % ext.feature_name)
189             log.warn(" the option --include-dirs or uncomment and modify")
190             log.warn(" the parameter include_dirs in setup.cfg)")
191             open(cache, 'w').write('0\n')
192             return False
193         prog = 'check_%s' % ext.feature_name
194         log.info("checking if %s links" % ext.feature_name)
195         try:
196             self.compiler.link_executable([obj], prog,
197                     output_dir=self.build_temp,
198                     libraries=ext.libraries,
199                     library_dirs=ext.library_dirs,
200                     runtime_library_dirs=ext.runtime_library_dirs,
201                     extra_postargs=(ext.extra_link_args or []))
202         except LinkError:
203             log.warn("unable to link against %s" % ext.feature_name)
204             log.warn("(if %s is installed correctly, you may need to specify"
205                     % ext.feature_name)
206             log.warn(" the option --library-dirs or uncomment and modify")
207             log.warn(" the parameter library_dirs in setup.cfg)")
208             open(cache, 'w').write('0\n')
209             return False
210         open(cache, 'w').write('1\n')
211         return True
212
213
214 class test(Command):
215
216     user_options = []
217
218     def initialize_options(self):
219         pass
220
221     def finalize_options(self):
222         pass
223
224     def run(self):
225         build_cmd = self.get_finalized_command('build')
226         build_cmd.run()
227         sys.path.insert(0, build_cmd.build_lib)
228         sys.path.insert(0, 'tests')
229         import test_all
230         test_all.main()
231
232
233 if __name__ == '__main__':
234
235     setup(
236         name=NAME,
237         version=VERSION,
238         description=DESCRIPTION,
239         long_description=LONG_DESCRIPTION,
240         author=AUTHOR,
241         author_email=AUTHOR_EMAIL,
242         license=LICENSE,
243         platforms=PLATFORMS,
244         url=URL,
245         download_url=DOWNLOAD_URL,
246         classifiers=CLASSIFIERS,
247
248         package_dir={'': 'lib'},
249         packages=['yaml'],
250         ext_modules=[
251             Extension('_yaml', ['ext/_yaml.pyx'],
252                 'libyaml', "LibYAML bindings", LIBYAML_CHECK,
253                 libraries=['yaml']),
254         ],
255
256         distclass=Distribution,
257         cmdclass={
258             'build_ext': build_ext,
259             'test': test,
260         },
261     )
262
Note: See TracBrowser for help on using the browser.