root/masterdriverz/snakeoil-formatters/setup.py @ masterdriverz%2540gentoo.org-20070520162637-v33q6ky5folsdsb4

Revision masterdriverz%2540gentoo.org-20070520162637-v33q6ky5folsdsb4, 8.0 kB (checked in by Charlie Shepherd <masterdriverz@…>, 18 months ago)

Add cpy version of snakeoil.formatters.PlainTextFormatter?, plus random tweaks to snakeoil.formatters

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