Workaround for bug 280341.
[gentoolkit.git] / setup.py
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import sys
6 import distutils
7 from distutils import core, log
8 from glob import glob
9
10 __version__ = os.getenv('VERSION', default='9999')
11
12 cwd = os.getcwd()
13
14 # Bash files that need `VERSION=""` subbed, relative to this dir:
15 bash_scripts = [os.path.join(cwd, path) for path in (
16         'bin/euse',
17         'bin/revdep-rebuild'
18 )]
19
20 # Python files that need `__version__ = ""` subbed, relative to this dir:
21 python_scripts = [os.path.join(cwd, path) for path in (
22         'bin/eclean',
23         'bin/epkginfo',
24         'bin/glsa-check',
25         'pym/gentoolkit/equery/__init__.py'
26 )]
27
28
29 class set_version(core.Command):
30         """Set python __version__ and bash VERSION to our __version__."""
31         description = "hardcode scripts' version using VERSION from environment"
32         user_options = []  # [(long_name, short_name, desc),]
33
34         def initialize_options (self):
35                 pass
36
37         def finalize_options (self):
38                 pass
39
40         def run(self):
41                 ver = 'svn' if __version__ == '9999' else __version__
42                 def sub(files, pattern):
43                         for f in files:
44                                 updated_file = []
45                                 with open(f) as s:
46                                         for line in s:
47                                                 newline = re.sub(pattern, '"%s"' % ver, line, 1)
48                                                 if newline != line:
49                                                         log.info("%s: %s" % (f, newline))
50                                                 updated_file.append(newline)
51                                 with open(f, 'w') as s:
52                                         s.writelines(updated_file)
53                 quote = r'[\'"]{1}'
54                 bash_re = r'(?<=VERSION=)' + quote + '[^\'"]*' + quote
55                 sub(bash_scripts, bash_re)
56                 python_re = r'(?<=^__version__ = )' + quote + '[^\'"]*' + quote
57                 sub(python_scripts, python_re)
58
59
60 def     load_test():
61         """Only return the real test class if it's actually being run so that we
62         don't depend on snakeoil just to install."""
63
64         desc = "run the test suite"
65         if 'test' in sys.argv[1:]:
66                 try:
67                         from snakeoil import distutils_extensions
68                 except ImportError:
69                         sys.stderr.write("Error: We depend on dev-python/snakeoil ")
70                         sys.stderr.write("to run tests.\n")
71                         sys.exit(1)
72                 class test(distutils_extensions.test):
73                         description = desc
74                         default_test_namespace = 'gentoolkit.test'
75         else:
76                 class test(core.Command):
77                         description = desc
78
79         return test
80
81
82 packages = [
83         '.'.join(root.split(os.sep)[1:])
84         for root, dirs, files in os.walk('pym/gentoolkit')
85         if '__init__.py' in files
86 ]
87
88 core.setup(
89         name='gentoolkit',
90         version=__version__,
91         description='Set of tools that work with and enhance portage.',
92         author='',
93         author_email='',
94         maintainer='Gentoo Portage Tools Team',
95         maintainer_email='tools-portage@gentoo.org',
96         url='http://www.gentoo.org/proj/en/portage/tools/index.xml',
97         download_url='http://distfiles.gentoo.org/distfiles/gentoolkit-%s.tar.gz'\
98                 % __version__,
99         package_dir={'': 'pym'},
100         packages=packages,
101         scripts=(glob('bin/*')),
102         data_files=(
103                 ('/etc/env.d', ['data/99gentoolkit-env']),
104                 ('/etc/revdep-rebuild', ['data/revdep-rebuild/99revdep-rebuild']),
105                 ('/etc/eclean', glob('data/eclean/*')),
106                 ('/usr/share/man/man1', glob('man/*'))
107         ),
108         cmdclass={
109                 'test': load_test(),
110                 'set_version': set_version,
111         },
112 )
113
114 # vim: set ts=4 sw=4 tw=79: