source: pyyaml/trunk/setup.py @ 306

Revision 306, 10.4 KB checked in by xi, 5 years ago (diff)

Added a comment on Python 3 support.

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