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