root/pyyaml/trunk/setup.py

Revision 364, 11.7 kB (checked in by xi, 1 year ago)

Minor formatting cleanup.

Line 
1
2 NAME = 'PyYAML'
3 VERSION = '3.09'
4 DESCRIPTION = "YAML parser and emitter for Python"
5 LONG_DESCRIPTION = """\
6 YAML is a data serialization format designed for human readability
7 and interaction with scripting languages.  PyYAML is a YAML parser
8 and 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
13 allow 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     "Programming Language :: Python :: 2",
30     "Programming Language :: Python :: 2.3",
31     "Programming Language :: Python :: 2.4",
32     "Programming Language :: Python :: 2.5",
33     "Programming Language :: Python :: 2.6",
34     "Programming Language :: Python :: 3",
35     "Programming Language :: Python :: 3.0",
36     "Programming Language :: Python :: 3.1",
37     "Topic :: Software Development :: Libraries :: Python Modules",
38     "Topic :: Text Processing :: Markup",
39 ]
40
41
42 LIBYAML_CHECK = """
43 #include <yaml.h>
44
45 int main(void) {
46     yaml_parser_t parser;
47     yaml_emitter_t emitter;
48
49     yaml_parser_initialize(&parser);
50     yaml_parser_delete(&parser);
51
52     yaml_emitter_initialize(&emitter);
53     yaml_emitter_delete(&emitter);
54
55     return 0;
56 }
57 """
58
59
60 import sys, os.path
61
62 from distutils import log
63 from distutils.core import setup, Command
64 from distutils.core import Distribution as _Distribution
65 from distutils.core import Extension as _Extension
66 from distutils.dir_util import mkpath
67 from distutils.command.build_ext import build_ext as _build_ext
68 from distutils.command.bdist_rpm import bdist_rpm as _bdist_rpm
69 from distutils.errors import CompileError, LinkError, DistutilsPlatformError
70
71 if 'setuptools.extension' in sys.modules:
72     _Extension = sys.modules['setuptools.extension']._Extension
73     sys.modules['distutils.core'].Extension = _Extension
74     sys.modules['distutils.extension'].Extension = _Extension
75     sys.modules['distutils.command.build_ext'].Extension = _Extension
76
77 with_pyrex = None
78 if sys.version_info[0] < 3:
79     try:
80         from Cython.Distutils.extension import Extension as _Extension
81         from Cython.Distutils import build_ext as _build_ext
82         with_pyrex = 'cython'
83     except ImportError:
84         try:
85             # Pyrex cannot build _yaml.c at the moment,
86             # but it may get fixed eventually.
87             from Pyrex.Distutils import Extension as _Extension
88             from Pyrex.Distutils import build_ext as _build_ext
89             with_pyrex = 'pyrex'
90         except ImportError:
91             pass
92
93
94 class Distribution(_Distribution):
95
96     def __init__(self, attrs=None):
97         _Distribution.__init__(self, attrs)
98         if not self.ext_modules:
99             return
100         for idx in range(len(self.ext_modules)-1, -1, -1):
101             ext = self.ext_modules[idx]
102             if not isinstance(ext, Extension):
103                 continue
104             setattr(self, ext.attr_name, None)
105             self.global_options = [
106                     (ext.option_name, None,
107                         "include %s (default if %s is available)"
108                         % (ext.feature_description, ext.feature_name)),
109                     (ext.neg_option_name, None,
110                         "exclude %s" % ext.feature_description),
111             ] + self.global_options
112             self.negative_opt = self.negative_opt.copy()
113             self.negative_opt[ext.neg_option_name] = ext.option_name
114
115     def has_ext_modules(self):
116         if not self.ext_modules:
117             return False
118         for ext in self.ext_modules:
119             with_ext = self.ext_status(ext)
120             if with_ext is None or with_ext:
121                 return True
122         return False
123
124     def ext_status(self, ext):
125         if isinstance(ext, Extension):
126             with_ext = getattr(self, ext.attr_name)
127             return with_ext
128         else:
129             return True
130
131
132 class Extension(_Extension):
133
134     def __init__(self, name, sources, feature_name, feature_description,
135             feature_check, **kwds):
136         if not with_pyrex:
137             for filename in sources[:]:
138                 base, ext = os.path.splitext(filename)
139                 if ext == '.pyx':
140                     sources.remove(filename)
141                     sources.append('%s.c' % base)
142         _Extension.__init__(self, name, sources, **kwds)
143         self.feature_name = feature_name
144         self.feature_description = feature_description
145         self.feature_check = feature_check
146         self.attr_name = 'with_' + feature_name.replace('-', '_')
147         self.option_name = 'with-' + feature_name
148         self.neg_option_name = 'without-' + feature_name
149
150
151 class build_ext(_build_ext):
152
153     def run(self):
154         optional = True
155         disabled = True
156         for ext in self.extensions:
157             with_ext = self.distribution.ext_status(ext)
158             if with_ext is None:
159                 disabled = False
160             elif with_ext:
161                 optional = False
162                 disabled = False
163                 break
164         if disabled:
165             return
166         try:
167             _build_ext.run(self)
168         except DistutilsPlatformError:
169             exc = sys.exc_info()[1]
170             if optional:
171                 log.warn(str(exc))
172                 log.warn("skipping build_ext")
173             else:
174                 raise
175
176     def get_source_files(self):
177         self.check_extensions_list(self.extensions)
178         filenames = []
179         for ext in self.extensions:
180             if with_pyrex == 'pyrex':
181                 self.pyrex_sources(ext.sources, ext)
182             elif with_pyrex == 'cython':
183                 self.cython_sources(ext.sources, ext)
184             for filename in ext.sources:
185                 filenames.append(filename)
186                 base = os.path.splitext(filename)[0]
187                 for ext in ['c', 'h', 'pyx', 'pxd']:
188                     filename = '%s.%s' % (base, ext)
189                     if filename not in filenames and os.path.isfile(filename):
190                         filenames.append(filename)
191         return filenames
192
193     def get_outputs(self):
194         self.check_extensions_list(self.extensions)
195         outputs = []
196         for ext in self.extensions:
197             fullname = self.get_ext_fullname(ext.name)
198             filename = os.path.join(self.build_lib,
199                                     self.get_ext_filename(fullname))
200             if os.path.isfile(filename):
201                 outputs.append(filename)
202         return outputs
203
204     def build_extensions(self):
205         self.check_extensions_list(self.extensions)
206         for ext in self.extensions:
207             with_ext = self.distribution.ext_status(ext)
208             if with_ext is None:
209                 with_ext = self.check_extension_availability(ext)
210             if not with_ext:
211                 continue
212             if with_pyrex == 'pyrex':
213                 ext.sources = self.pyrex_sources(ext.sources, ext)
214             elif with_pyrex == 'cython':
215                 ext.sources = self.cython_sources(ext.sources, ext)
216             self.build_extension(ext)
217
218     def check_extension_availability(self, ext):
219         cache = os.path.join(self.build_temp, 'check_%s.out' % ext.feature_name)
220         if not self.force and os.path.isfile(cache):
221             data = open(cache).read().strip()
222             if data == '1':
223                 return True
224             elif data == '0':
225                 return False
226         mkpath(self.build_temp)
227         src = os.path.join(self.build_temp, 'check_%s.c' % ext.feature_name)
228         open(src, 'w').write(ext.feature_check)
229         log.info("checking if %s is compilable" % ext.feature_name)
230         try:
231             [obj] = self.compiler.compile([src],
232                     macros=ext.define_macros+[(undef,) for undef in ext.undef_macros],
233                     include_dirs=ext.include_dirs,
234                     extra_postargs=(ext.extra_compile_args or []),
235                     depends=ext.depends)
236         except CompileError:
237             log.warn("")
238             log.warn("%s is not found or a compiler error: forcing --%s"
239                      % (ext.feature_name, ext.neg_option_name))
240             log.warn("(if %s is installed correctly, you may need to"
241                     % ext.feature_name)
242             log.warn(" specify the option --include-dirs or uncomment and")
243             log.warn(" modify the parameter include_dirs in setup.cfg)")
244             open(cache, 'w').write('0\n')
245             return False
246         prog = 'check_%s' % ext.feature_name
247         log.info("checking if %s is linkable" % ext.feature_name)
248         try:
249             self.compiler.link_executable([obj], prog,
250                     output_dir=self.build_temp,
251                     libraries=ext.libraries,
252                     library_dirs=ext.library_dirs,
253                     runtime_library_dirs=ext.runtime_library_dirs,
254                     extra_postargs=(ext.extra_link_args or []))
255         except LinkError:
256             log.warn("")
257             log.warn("%s is not found or a linker error: forcing --%s"
258                      % (ext.feature_name, ext.neg_option_name))
259             log.warn("(if %s is installed correctly, you may need to"
260                     % ext.feature_name)
261             log.warn(" specify the option --library-dirs or uncomment and")
262             log.warn(" modify the parameter library_dirs in setup.cfg)")
263             open(cache, 'w').write('0\n')
264             return False
265         open(cache, 'w').write('1\n')
266         return True
267
268
269 class bdist_rpm(_bdist_rpm):
270
271     def _make_spec_file(self):
272         argv0 = sys.argv[0]
273         features = []
274         for ext in self.distribution.ext_modules:
275             if not isinstance(ext, Extension):
276                 continue
277             with_ext = getattr(self.distribution, ext.attr_name)
278             if with_ext is None:
279                 continue
280             if with_ext:
281                 features.append('--'+ext.option_name)
282             else:
283                 features.append('--'+ext.neg_option_name)
284         sys.argv[0] = ' '.join([argv0]+features)
285         spec_file = _bdist_rpm._make_spec_file(self)
286         sys.argv[0] = argv0
287         return spec_file
288
289
290 class test(Command):
291
292     user_options = []
293
294     def initialize_options(self):
295         pass
296
297     def finalize_options(self):
298         pass
299
300     def run(self):
301         build_cmd = self.get_finalized_command('build')
302         build_cmd.run()
303         sys.path.insert(0, build_cmd.build_lib)
304         if sys.version_info[0] < 3:
305             sys.path.insert(0, 'tests/lib')
306         else:
307             sys.path.insert(0, 'tests/lib3')
308         import test_all
309         test_all.main([])
310
311
312 if __name__ == '__main__':
313
314     setup(
315         name=NAME,
316         version=VERSION,
317         description=DESCRIPTION,
318         long_description=LONG_DESCRIPTION,
319         author=AUTHOR,
320         author_email=AUTHOR_EMAIL,
321         license=LICENSE,
322         platforms=PLATFORMS,
323         url=URL,
324         download_url=DOWNLOAD_URL,
325         classifiers=CLASSIFIERS,
326
327         package_dir={'': {2: 'lib', 3: 'lib3'}[sys.version_info[0]]},
328         packages=['yaml'],
329         ext_modules=[
330             Extension('_yaml', ['ext/_yaml.pyx'],
331                 'libyaml', "LibYAML bindings", LIBYAML_CHECK,
332                 libraries=['yaml']),
333         ],
334
335         distclass=Distribution,
336
337         cmdclass={
338             'build_ext': build_ext,
339             'bdist_rpm': bdist_rpm,
340             'test': test,
341         },
342     )
343
Note: See TracBrowser for help on using the browser.