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