-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdist_rpm5.py
237 lines (210 loc) · 9.59 KB
/
bdist_rpm5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from distutils.command.bdist_rpm import bdist_rpm
import distutils.command.sdist
from distutils.sysconfig import get_config_var
from distutils.filelist import FileList
import string, os, sys
from types import *
from glob import glob
import rpm
class bdist_rpm5(bdist_rpm):
def _make_spec_file(self):
"""Generate the text of an RPM spec file and return it as a
list of strings (one per line).
"""
sdist = self.reinitialize_command('sdist')
sdist.finalize_options()
sdist.filelist = FileList()
sdist.get_file_list()
manifest = sdist.filelist.files
# definitions and headers
name = self.distribution.get_name()
version = self.distribution.get_version().replace('-','_')
release = self.release.replace('-','_')
summary = self.distribution.get_description().strip().strip('.')
spec_file = [
'%define\toname\t'+name,
]
if name[:2] == "py":
spec_file.append('%define\tmodule\t' + name[2:])
module = '%{module}'
else:
module = '%{oname}'
spec_file.extend([
'',
'Name:\t\tpython-' + module,
'Version:\t' + version,
'Release:\t' + release,
'Summary:\t' + summary,
'Source0:\thttp://pypi.python.org/packages/source/%c/%%{oname}/%%{oname}-%%{version}.tar' % name[0],
])
# XXX yuck! this filename is available from the "sdist" command,
# but only after it has run: and we create the spec file before
# running "sdist", in case of --spec-only.
if sdist.formats and 'xztar' in sdist.formats:
spec_file[-1] += '.xz'
elif sdist.formats and 'bztar' in sdist.formats:
spec_file[-1] += '.bz2'
else:
spec_file[-1] += '.gz'
license = self.distribution.get_license()
if license == "UNKNOWN":
classifiers = self.distribution.get_classifiers()
for classifier in classifiers:
values = classifier.split(" :: ")
if values[0] == "License":
license = values[-1]
license.replace("GPL ", "GPLv").strip()
spec_file.extend([
'License:\t' + license,
'Group:\t\t' + self.group,])
if self.distribution.get_url() != 'UNKNOWN':
spec_file.append('Url:\t\t' + self.distribution.get_url())
doc_names = ['README', 'CHANGES','ChangeLog', 'NEWS', 'THANKS',
'HISTORY', 'AUTHORS', 'BUGS', 'ReleaseNotes', 'DISCLAIMER',
'TODO', 'TROUBLESHOOTING', 'IDEAS', 'HACKING', 'WISHLIST',
'CREDITS', 'PROJECTS', 'LEGAL', 'KNOWN_BUGS',
'MISSING_FEATURES', 'FAQ', 'ANNOUNCE', 'FEATURES', 'WHATSNEW']
license_names = ['LICENSE', 'COPYRIGHT', 'COPYING']
common_licenses = glob('/usr/share/common-licenses/*')
for i in range(len(common_licenses)):
common_licenses[i] = os.path.basename(common_licenses[i])
doc_names.extend(license_names)
doc_suffixes = ('.doc', '.htm', '.txt', '.pdf', '.odt')
self.doc_files = []
all_files = []
if self.distribution.data_files:
all_files.extend(self.distribution.data_files)
if manifest:
all_files.extend(manifest)
if all_files:
for data_file in all_files:
done = False
for doc_name in doc_names:
if doc_name.lower() in data_file.lower():
if doc_name in license_names and license in common_licenses:
done = True
break
self.doc_files.append(data_file)
done = True
break
if done:
continue
for doc_suffix in doc_suffixes:
ext = os.path.splitext(data_file.lower())[1]
if ext.lower().startswith(doc_suffix.lower()):
self.doc_files.append(data_file)
break
if not self.force_arch:
# noarch if no extension modules
if not self.distribution.has_ext_modules():
spec_file.append('BuildArch:\tnoarch')
else:
spec_file.append( 'BuildArch:\t%s' % self.force_arch )
for field in ('Provides',
'Requires',
'Conflicts',
'Obsoletes',
):
val = getattr(self, string.lower(field))
if type(val) is ListType:
spec_file.append('%s: %s' % (field, string.join(val)))
elif val is not None:
spec_file.append('%s: %s' % (field, val))
build_requires = []
if self.distribution.has_ext_modules():
build_requires.append('python-devel')
# Ugly, but should mostly work... :p
if 'setuptools' in str(self.distribution.__dict__) or 'setuptools' in str(sdist.__dict__):
build_requires.append('python-setuptools')
if build_requires:
spec_file.append('BuildRequires:\t' +
string.join(build_requires))
if self.build_requires:
spec_file.append('BuildRequires:\t' +
string.join(self.build_requires))
spec_file.extend([
'',
'%description',
self.distribution.get_long_description().strip()
])
# insert contents of files
# XXX this is kind of misleading: user-supplied options are files
# that we open and interpolate into the spec file, but the defaults
# are just text that we drop in as-is. Hmmm.
if 'test_suite' in self.distribution.__dict__ and self.distribution.test_suite:
verify_script = "python setup.py test"
else:
verify_script = None
script_options = [
('prep', 'prep_script', "%setup -q -n %{oname}-%{version}"),
('build', 'build_script', "python setup.py build"),
('install', 'install_script',
("python setup.py install "
"--root=%{buildroot}")),
('check', 'verify_script', verify_script),
('pre', 'pre_install', None),
('post', 'post_install', None),
('preun', 'pre_uninstall', None),
('postun', 'post_uninstall', None),
]
for (rpm_opt, attr, default) in script_options:
# Insert contents of file referred to, if no file is referred to
# use 'default' as contents of script
val = getattr(self, attr)
if val or default:
spec_file.extend([
'',
'%' + rpm_opt,])
if val:
spec_file.extend(string.split(open(val, 'r').read(), '\n'))
else:
spec_file.append(default)
# files section
spec_file.extend([
'',
'%files',
])
for doc_file in self.doc_files:
spec_file.append('%doc ' + doc_file)
if self.distribution.has_ext_modules():
site_pkgs = '%{py_platsitedir}'
else:
site_pkgs = '%{py_puresitedir}'
if self.distribution.has_scripts():
for script in self.distribution.scripts:
if type(script) == StringType:
spec_file.append(os.path.join('%{_bindir}', os.path.basename(script)))
site_pkgs_files = []
if self.distribution.data_files:
for data_file in self.distribution.data_files:
site_pkgs_files.append(os.path.join(site_pkgs, data_file))
if 'entry_points' in self.distribution.__dict__ and self.distribution.entry_points:
if type(self.distribution.entry_points) is DictType:
for entry_points in self.distribution.entry_points:
for entry_point in self.distribution.entry_points[entry_points]:
site_pkgs_files.append(os.path.join('%{_bindir}', os.path.basename(entry_point.split('=')[0])))
if 'py_modules' in self.distribution.__dict__ and self.distribution.py_modules:
for py_module in self.distribution.py_modules:
py_module = py_module.replace('.', os.path.sep)
site_pkgs_files.append(os.path.join(site_pkgs, py_module + '.py*'))
if 'packages' in self.distribution.__dict__ and self.distribution.packages:
for package in self.distribution.packages:
package = package.replace('.', os.path.sep)
#spec_file.append('%dir ' + os.path.join(site_pkgs, package))
site_pkgs_files.append(os.path.join(site_pkgs, package, '*.py*'))
if self.distribution.has_ext_modules():
for ext_module in self.distribution.ext_modules:
ext_module = ext_module.name.replace('.', os.path.sep)
site_pkgs_files.append(os.path.join(site_pkgs, ext_module + get_config_var('SHLIB_EXT').replace('"', '')))
site_pkgs_files.sort()
for f in site_pkgs_files:
spec_file.append(f)
spec_file.append(os.path.join(site_pkgs, name.replace('-', '_') + '*.egg-info'))
packager = rpm.expandMacro('%packager')
spec_file.extend([
'',
'%changelog',
'* Thu Oct 21 2010 %s %s-%s' % (packager, version, release),
'- Initial release',])
return spec_file
# _make_spec_file ()