root/masterdriverz/snakeoil/setup.py @ ferringb%2540gmail.com-20070709104322-tdwpat71segpcwyn

Revision ferringb%2540gmail.com-20070709104322-tdwpat71segpcwyn, 8.1 kB (checked in by Brian Harring <ferringb@…>, 17 months ago)

pull in integration; 0.1_rc2, news updates

Line 
1#!/usr/bin/env python
2
3import os
4import sys
5import errno
6import subprocess
7import unittest
8
9from distutils import core, ccompiler, log, errors
10from distutils.command import build, sdist, build_ext, build_py, build_scripts, install
11from stat import ST_MODE
12
13class OptionalExtension(core.Extension):
14    pass
15
16if os.name == "nt":
17    bzrbin = "bzr.bat"
18else:
19    bzrbin = "bzr"
20
21class mysdist(sdist.sdist):
22
23    """sdist command specifying the right files and generating ChangeLog."""
24
25    user_options = sdist.sdist.user_options + [
26        ('changelog', None, 'create a ChangeLog [default]'),
27        ('no-changelog', None, 'do not create the ChangeLog file'),
28        ]
29
30    boolean_options = sdist.sdist.boolean_options + ['changelog']
31
32    negative_opt = {'no-changelog': 'changelog'}
33    negative_opt.update(sdist.sdist.negative_opt)
34
35    default_format = dict(sdist.sdist.default_format)
36    default_format["posix"] = "bztar"
37
38    def initialize_options(self):
39        sdist.sdist.initialize_options(self)
40        self.changelog = True
41
42    def get_file_list(self):
43        """Get a filelist without doing anything involving MANIFEST files."""
44        # This is copied from the "Recreate manifest" bit of sdist.
45        self.filelist.findall()
46        if self.use_defaults:
47            self.add_defaults()
48
49        # This bit is roughly equivalent to a MANIFEST.in template file.
50        for key, globs in self.distribution.package_data.iteritems():
51            for pattern in globs:
52                self.filelist.include_pattern(os.path.join(key, pattern))
53
54        self.filelist.append("AUTHORS")
55        self.filelist.append("NOTES")
56        self.filelist.append("NEWS")
57        self.filelist.append("COPYING")
58
59        self.filelist.include_pattern('.c', prefix='src')
60        self.filelist.include_pattern('.h', prefix='include/snakeoil')
61
62        for prefix in ['doc', 'dev-notes']:
63            self.filelist.include_pattern('.rst', prefix=prefix)
64            self.filelist.exclude_pattern(os.path.sep + 'index.rst',
65                                          prefix=prefix)
66        self.filelist.append('build_docs.py')
67        self.filelist.include_pattern('*', prefix='examples')
68        self.filelist.include_pattern('*', prefix='bin')
69
70        if self.prune:
71            self.prune_file_list()
72
73        # This is not optional: remove_duplicates needs sorted input.
74        self.filelist.sort()
75        self.filelist.remove_duplicates()
76
77    def make_release_tree(self, base_dir, files):
78        """Create and populate the directory tree that is put in source tars.
79
80        This copies or hardlinks "normal" source files that should go
81        into the release and adds generated files that should not
82        exist in a working tree.
83        """
84        sdist.sdist.make_release_tree(self, base_dir, files)
85        if self.changelog:
86            log.info("regenning ChangeLog (may take a while)")
87            if subprocess.call(
88                [bzrbin, 'log', '--verbose'],
89                stdout=open(os.path.join(base_dir, 'ChangeLog'), 'w')):
90                raise errors.DistutilsExecError('bzr log failed')
91        log.info('generating bzr_verinfo')
92        if subprocess.call(
93            [bzrbin, 'version-info', '--format=python'],
94            stdout=open(os.path.join(
95                    base_dir, 'snakeoil', 'bzr_verinfo.py'), 'w')):
96            raise errors.DistutilsExecError('bzr version-info failed')
97
98
99class snakeoil_build_py(build_py.build_py):
100
101    def run(self):
102        build_py.build_py.run(self)
103        bzr_ver = self.get_module_outfile(
104            self.build_lib, ('snakeoil',), 'bzr_verinfo')
105        if not os.path.exists(bzr_ver):
106            log.info('generating bzr_verinfo')
107            if subprocess.call(
108                [bzrbin, 'version-info', '--format=python'],
109                stdout=open(bzr_ver, 'w')):
110                # Not fatal, just less useful --version output.
111                log.warn('generating bzr_verinfo failed!')
112            else:
113                self.byte_compile([bzr_ver])
114
115class snakeoil_build_ext(build_ext.build_ext):
116
117    user_options = build_ext.build_ext.user_options + [
118        ("build-optional=", "o", "build optional C modules"),
119    ]
120
121    boolean_options = build.build.boolean_options + ["build-optional"]
122
123    def initialize_options(self):
124        build_ext.build_ext.initialize_options(self)
125        self.build_optional = None
126
127    def finalize_options(self):
128        build_ext.build_ext.finalize_options(self)
129        if self.build_optional is None:
130            self.build_optional = True
131        if not self.build_optional:
132            self.extensions = [ext for ext in self.extensions if not isinstance(ext, OptionalExtension)] or None
133
134class TestLoader(unittest.TestLoader):
135
136    """Test loader that knows how to recurse packages."""
137
138    def loadTestsFromModule(self, module):
139        """Recurses if module is actually a package."""
140        paths = getattr(module, '__path__', None)
141        tests = [unittest.TestLoader.loadTestsFromModule(self, module)]
142        if paths is None:
143            # Not a package.
144            return tests[0]
145        for path in paths:
146            for child in os.listdir(path):
147                if (child != '__init__.py' and child.endswith('.py') and
148                    child.startswith('test')):
149                    # Child module.
150                    childname = '%s.%s' % (module.__name__, child[:-3])
151                else:
152                    childpath = os.path.join(path, child)
153                    if not os.path.isdir(childpath):
154                        continue
155                    if not os.path.exists(os.path.join(childpath,
156                                                       '__init__.py')):
157                        continue
158                    # Subpackage.
159                    childname = '%s.%s' % (module.__name__, child)
160                tests.append(self.loadTestsFromName(childname))
161        return self.suiteClass(tests)
162
163
164testLoader = TestLoader()
165
166
167class test(core.Command):
168
169    """Run our unit tests in a built copy.
170
171    Based on code from setuptools.
172    """
173
174    user_options = []
175
176    def initialize_options(self):
177        # Options? What options?
178        pass
179
180    def finalize_options(self):
181        # Options? What options?
182        pass
183
184    def run(self):
185        build_ext = self.reinitialize_command('build_ext')
186        build_ext.inplace = True
187        self.run_command('build_ext')
188        # Somewhat hackish: this calls sys.exit.
189        unittest.main('snakeoil.test', argv=['setup.py', '-v'], testLoader=testLoader)
190
191
192packages = [
193    root.replace(os.path.sep, '.')
194    for root, dirs, files in os.walk('snakeoil')
195    if '__init__.py' in files]
196
197common_includes=[
198    'include/snakeoil/py24-compatibility.h',
199    'include/snakeoil/heapdef.h',
200    'include/snakeoil/common.h',
201    ]
202
203extra_kwargs = dict(
204    extra_compile_args=['-Wall'],
205    depends=common_includes,
206    include_dirs=['include'],
207    )
208
209extensions = []
210if sys.version_info < (2, 5):
211    # Almost unmodified copy from the python 2.5 source.
212    extensions.append(OptionalExtension(
213            'snakeoil._compatibility', ['src/compatibility.c'], **extra_kwargs))
214
215from snakeoil.version import __version__ as VERSION
216
217core.setup(
218    name='snakeoil',
219    version=VERSION,
220    description='misc common functionality, and useful optimizations',
221    url='http://www.pkgcore.org/',
222    packages=packages,
223    ext_modules=[
224        OptionalExtension(
225            'snakeoil.osutils._posix', ['src/posix.c'], **extra_kwargs),
226        OptionalExtension(
227            'snakeoil._klass', ['src/klass.c'], **extra_kwargs),
228        OptionalExtension(
229            'snakeoil._caching', ['src/caching.c'], **extra_kwargs),
230        OptionalExtension(
231            'snakeoil._lists', ['src/lists.c'], **extra_kwargs),
232        OptionalExtension(
233            'snakeoil.osutils._readdir', ['src/readdir.c'], **extra_kwargs),
234        OptionalExtension(
235            'snakeoil._formatters', ['src/formatters.c'], **extra_kwargs),
236        ] + extensions,
237    headers=common_includes,
238    cmdclass={
239        'sdist': mysdist,
240        'build_ext': snakeoil_build_ext,
241        'build_py': snakeoil_build_py,
242        'test': test,
243        },
244    )
Note: See TracBrowser for help on using the browser.