Whitespace normalization.
[python/dscho.git] / Lib / distutils / command / bdist_rpm.py
blob4be99994978769e9a43be59dbbc41e683e43e25f
1 """distutils.command.bdist_rpm
3 Implements the Distutils 'bdist_rpm' command (create RPM source and binary
4 distributions)."""
6 # This module should be kept compatible with Python 1.5.2.
8 __revision__ = "$Id$"
10 import sys, os, string
11 import glob
12 from types import *
13 from distutils.core import Command
14 from distutils.debug import DEBUG
15 from distutils.util import get_platform
16 from distutils.file_util import write_file
17 from distutils.errors import *
18 from distutils import log
20 class bdist_rpm (Command):
22 description = "create an RPM distribution"
24 user_options = [
25 ('bdist-base=', None,
26 "base directory for creating built distributions"),
27 ('rpm-base=', None,
28 "base directory for creating RPMs (defaults to \"rpm\" under "
29 "--bdist-base; must be specified for RPM 2)"),
30 ('dist-dir=', 'd',
31 "directory to put final RPM files in "
32 "(and .spec files if --spec-only)"),
33 ('python=', None,
34 "path to Python interpreter to hard-code in the .spec file "
35 "(default: \"python\")"),
36 ('fix-python', None,
37 "hard-code the exact path to the current Python interpreter in "
38 "the .spec file"),
39 ('spec-only', None,
40 "only regenerate spec file"),
41 ('source-only', None,
42 "only generate source RPM"),
43 ('binary-only', None,
44 "only generate binary RPM"),
45 ('use-bzip2', None,
46 "use bzip2 instead of gzip to create source distribution"),
48 # More meta-data: too RPM-specific to put in the setup script,
49 # but needs to go in the .spec file -- so we make these options
50 # to "bdist_rpm". The idea is that packagers would put this
51 # info in setup.cfg, although they are of course free to
52 # supply it on the command line.
53 ('distribution-name=', None,
54 "name of the (Linux) distribution to which this "
55 "RPM applies (*not* the name of the module distribution!)"),
56 ('group=', None,
57 "package classification [default: \"Development/Libraries\"]"),
58 ('release=', None,
59 "RPM release number"),
60 ('serial=', None,
61 "RPM serial number"),
62 ('vendor=', None,
63 "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
64 "[default: maintainer or author from setup script]"),
65 ('packager=', None,
66 "RPM packager (eg. \"Jane Doe <jane@example.net>\")"
67 "[default: vendor]"),
68 ('doc-files=', None,
69 "list of documentation files (space or comma-separated)"),
70 ('changelog=', None,
71 "RPM changelog"),
72 ('icon=', None,
73 "name of icon file"),
74 ('provides=', None,
75 "capabilities provided by this package"),
76 ('requires=', None,
77 "capabilities required by this package"),
78 ('conflicts=', None,
79 "capabilities which conflict with this package"),
80 ('build-requires=', None,
81 "capabilities required to build this package"),
82 ('obsoletes=', None,
83 "capabilities made obsolete by this package"),
85 # Actions to take when building RPM
86 ('keep-temp', 'k',
87 "don't clean up RPM build directory"),
88 ('no-keep-temp', None,
89 "clean up RPM build directory [default]"),
90 ('use-rpm-opt-flags', None,
91 "compile with RPM_OPT_FLAGS when building from source RPM"),
92 ('no-rpm-opt-flags', None,
93 "do not pass any RPM CFLAGS to compiler"),
94 ('rpm3-mode', None,
95 "RPM 3 compatibility mode (default)"),
96 ('rpm2-mode', None,
97 "RPM 2 compatibility mode"),
100 boolean_options = ['keep-temp', 'use-rpm-opt-flags', 'rpm3-mode']
102 negative_opt = {'no-keep-temp': 'keep-temp',
103 'no-rpm-opt-flags': 'use-rpm-opt-flags',
104 'rpm2-mode': 'rpm3-mode'}
107 def initialize_options (self):
108 self.bdist_base = None
109 self.rpm_base = None
110 self.dist_dir = None
111 self.python = None
112 self.fix_python = None
113 self.spec_only = None
114 self.binary_only = None
115 self.source_only = None
116 self.use_bzip2 = None
118 self.distribution_name = None
119 self.group = None
120 self.release = None
121 self.serial = None
122 self.vendor = None
123 self.packager = None
124 self.doc_files = None
125 self.changelog = None
126 self.icon = None
128 self.prep_script = None
129 self.build_script = None
130 self.install_script = None
131 self.clean_script = None
132 self.verify_script = None
133 self.pre_install = None
134 self.post_install = None
135 self.pre_uninstall = None
136 self.post_uninstall = None
137 self.prep = None
138 self.provides = None
139 self.requires = None
140 self.conflicts = None
141 self.build_requires = None
142 self.obsoletes = None
144 self.keep_temp = 0
145 self.use_rpm_opt_flags = 1
146 self.rpm3_mode = 1
148 # initialize_options()
151 def finalize_options (self):
152 self.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
153 if self.rpm_base is None:
154 if not self.rpm3_mode:
155 raise DistutilsOptionError, \
156 "you must specify --rpm-base in RPM 2 mode"
157 self.rpm_base = os.path.join(self.bdist_base, "rpm")
159 if self.python is None:
160 if self.fix_python:
161 self.python = sys.executable
162 else:
163 self.python = "python"
164 elif self.fix_python:
165 raise DistutilsOptionError, \
166 "--python and --fix-python are mutually exclusive options"
168 if os.name != 'posix':
169 raise DistutilsPlatformError, \
170 ("don't know how to create RPM "
171 "distributions on platform %s" % os.name)
172 if self.binary_only and self.source_only:
173 raise DistutilsOptionError, \
174 "cannot supply both '--source-only' and '--binary-only'"
176 # don't pass CFLAGS to pure python distributions
177 if not self.distribution.has_ext_modules():
178 self.use_rpm_opt_flags = 0
180 self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
181 self.finalize_package_data()
183 # finalize_options()
185 def finalize_package_data (self):
186 self.ensure_string('group', "Development/Libraries")
187 self.ensure_string('vendor',
188 "%s <%s>" % (self.distribution.get_contact(),
189 self.distribution.get_contact_email()))
190 self.ensure_string('packager')
191 self.ensure_string_list('doc_files')
192 if type(self.doc_files) is ListType:
193 for readme in ('README', 'README.txt'):
194 if os.path.exists(readme) and readme not in self.doc_files:
195 self.doc_files.append(readme)
197 self.ensure_string('release', "1")
198 self.ensure_string('serial') # should it be an int?
200 self.ensure_string('distribution_name')
202 self.ensure_string('changelog')
203 # Format changelog correctly
204 self.changelog = self._format_changelog(self.changelog)
206 self.ensure_filename('icon')
208 self.ensure_filename('prep_script')
209 self.ensure_filename('build_script')
210 self.ensure_filename('install_script')
211 self.ensure_filename('clean_script')
212 self.ensure_filename('verify_script')
213 self.ensure_filename('pre_install')
214 self.ensure_filename('post_install')
215 self.ensure_filename('pre_uninstall')
216 self.ensure_filename('post_uninstall')
218 # XXX don't forget we punted on summaries and descriptions -- they
219 # should be handled here eventually!
221 # Now *this* is some meta-data that belongs in the setup script...
222 self.ensure_string_list('provides')
223 self.ensure_string_list('requires')
224 self.ensure_string_list('conflicts')
225 self.ensure_string_list('build_requires')
226 self.ensure_string_list('obsoletes')
228 # finalize_package_data ()
231 def run (self):
233 if DEBUG:
234 print "before _get_package_data():"
235 print "vendor =", self.vendor
236 print "packager =", self.packager
237 print "doc_files =", self.doc_files
238 print "changelog =", self.changelog
240 # make directories
241 if self.spec_only:
242 spec_dir = self.dist_dir
243 self.mkpath(spec_dir)
244 else:
245 rpm_dir = {}
246 for d in ('SOURCES', 'SPECS', 'BUILD', 'RPMS', 'SRPMS'):
247 rpm_dir[d] = os.path.join(self.rpm_base, d)
248 self.mkpath(rpm_dir[d])
249 spec_dir = rpm_dir['SPECS']
251 # Spec file goes into 'dist_dir' if '--spec-only specified',
252 # build/rpm.<plat> otherwise.
253 spec_path = os.path.join(spec_dir,
254 "%s.spec" % self.distribution.get_name())
255 self.execute(write_file,
256 (spec_path,
257 self._make_spec_file()),
258 "writing '%s'" % spec_path)
260 if self.spec_only: # stop if requested
261 return
263 # Make a source distribution and copy to SOURCES directory with
264 # optional icon.
265 sdist = self.reinitialize_command('sdist')
266 if self.use_bzip2:
267 sdist.formats = ['bztar']
268 else:
269 sdist.formats = ['gztar']
270 self.run_command('sdist')
272 source = sdist.get_archive_files()[0]
273 source_dir = rpm_dir['SOURCES']
274 self.copy_file(source, source_dir)
276 if self.icon:
277 if os.path.exists(self.icon):
278 self.copy_file(self.icon, source_dir)
279 else:
280 raise DistutilsFileError, \
281 "icon file '%s' does not exist" % self.icon
284 # build package
285 log.info("building RPMs")
286 rpm_cmd = ['rpm']
287 if os.path.exists('/usr/bin/rpmbuild') or \
288 os.path.exists('/bin/rpmbuild'):
289 rpm_cmd = ['rpmbuild']
290 if self.source_only: # what kind of RPMs?
291 rpm_cmd.append('-bs')
292 elif self.binary_only:
293 rpm_cmd.append('-bb')
294 else:
295 rpm_cmd.append('-ba')
296 if self.rpm3_mode:
297 rpm_cmd.extend(['--define',
298 '_topdir %s/%s' % (os.getcwd(), self.rpm_base),])
299 if not self.keep_temp:
300 rpm_cmd.append('--clean')
301 rpm_cmd.append(spec_path)
302 self.spawn(rpm_cmd)
304 # XXX this is a nasty hack -- we really should have a proper way to
305 # find out the names of the RPM files created; also, this assumes
306 # that RPM creates exactly one source and one binary RPM.
307 if not self.dry_run:
308 if not self.binary_only:
309 srpms = glob.glob(os.path.join(rpm_dir['SRPMS'], "*.rpm"))
310 assert len(srpms) == 1, \
311 "unexpected number of SRPM files found: %s" % srpms
312 self.move_file(srpms[0], self.dist_dir)
314 if not self.source_only:
315 rpms = glob.glob(os.path.join(rpm_dir['RPMS'], "*/*.rpm"))
316 debuginfo = glob.glob(os.path.join(rpm_dir['RPMS'], \
317 "*/*debuginfo*.rpm"))
318 if debuginfo:
319 rpms.remove(debuginfo[0])
320 assert len(rpms) == 1, \
321 "unexpected number of RPM files found: %s" % rpms
322 self.move_file(rpms[0], self.dist_dir)
323 if debuginfo:
324 self.move_file(debuginfo[0], self.dist_dir)
325 # run()
328 def _make_spec_file(self):
329 """Generate the text of an RPM spec file and return it as a
330 list of strings (one per line).
332 # definitions and headers
333 spec_file = [
334 '%define name ' + self.distribution.get_name(),
335 '%define version ' + self.distribution.get_version(),
336 '%define release ' + self.release,
338 'Summary: ' + self.distribution.get_description(),
341 # put locale summaries into spec file
342 # XXX not supported for now (hard to put a dictionary
343 # in a config file -- arg!)
344 #for locale in self.summaries.keys():
345 # spec_file.append('Summary(%s): %s' % (locale,
346 # self.summaries[locale]))
348 spec_file.extend([
349 'Name: %{name}',
350 'Version: %{version}',
351 'Release: %{release}',])
353 # XXX yuck! this filename is available from the "sdist" command,
354 # but only after it has run: and we create the spec file before
355 # running "sdist", in case of --spec-only.
356 if self.use_bzip2:
357 spec_file.append('Source0: %{name}-%{version}.tar.bz2')
358 else:
359 spec_file.append('Source0: %{name}-%{version}.tar.gz')
361 spec_file.extend([
362 'License: ' + self.distribution.get_license(),
363 'Group: ' + self.group,
364 'BuildRoot: %{_tmppath}/%{name}-buildroot',
365 'Prefix: %{_prefix}', ])
367 # noarch if no extension modules
368 if not self.distribution.has_ext_modules():
369 spec_file.append('BuildArchitectures: noarch')
371 for field in ('Vendor',
372 'Packager',
373 'Provides',
374 'Requires',
375 'Conflicts',
376 'Obsoletes',
378 val = getattr(self, string.lower(field))
379 if type(val) is ListType:
380 spec_file.append('%s: %s' % (field, string.join(val)))
381 elif val is not None:
382 spec_file.append('%s: %s' % (field, val))
385 if self.distribution.get_url() != 'UNKNOWN':
386 spec_file.append('Url: ' + self.distribution.get_url())
388 if self.distribution_name:
389 spec_file.append('Distribution: ' + self.distribution_name)
391 if self.build_requires:
392 spec_file.append('BuildRequires: ' +
393 string.join(self.build_requires))
395 if self.icon:
396 spec_file.append('Icon: ' + os.path.basename(self.icon))
398 spec_file.extend([
400 '%description',
401 self.distribution.get_long_description()
404 # put locale descriptions into spec file
405 # XXX again, suppressed because config file syntax doesn't
406 # easily support this ;-(
407 #for locale in self.descriptions.keys():
408 # spec_file.extend([
409 # '',
410 # '%description -l ' + locale,
411 # self.descriptions[locale],
412 # ])
414 # rpm scripts
415 # figure out default build script
416 def_build = "%s setup.py build" % self.python
417 if self.use_rpm_opt_flags:
418 def_build = 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
420 # insert contents of files
422 # XXX this is kind of misleading: user-supplied options are files
423 # that we open and interpolate into the spec file, but the defaults
424 # are just text that we drop in as-is. Hmmm.
426 script_options = [
427 ('prep', 'prep_script', "%setup"),
428 ('build', 'build_script', def_build),
429 ('install', 'install_script',
430 ("%s setup.py install "
431 "--root=$RPM_BUILD_ROOT "
432 "--record=INSTALLED_FILES") % self.python),
433 ('clean', 'clean_script', "rm -rf $RPM_BUILD_ROOT"),
434 ('verifyscript', 'verify_script', None),
435 ('pre', 'pre_install', None),
436 ('post', 'post_install', None),
437 ('preun', 'pre_uninstall', None),
438 ('postun', 'post_uninstall', None),
441 for (rpm_opt, attr, default) in script_options:
442 # Insert contents of file referred to, if no file is referred to
443 # use 'default' as contents of script
444 val = getattr(self, attr)
445 if val or default:
446 spec_file.extend([
448 '%' + rpm_opt,])
449 if val:
450 spec_file.extend(string.split(open(val, 'r').read(), '\n'))
451 else:
452 spec_file.append(default)
455 # files section
456 spec_file.extend([
458 '%files -f INSTALLED_FILES',
459 '%defattr(-,root,root)',
462 if self.doc_files:
463 spec_file.append('%doc ' + string.join(self.doc_files))
465 if self.changelog:
466 spec_file.extend([
468 '%changelog',])
469 spec_file.extend(self.changelog)
471 return spec_file
473 # _make_spec_file ()
475 def _format_changelog(self, changelog):
476 """Format the changelog correctly and convert it to a list of strings
478 if not changelog:
479 return changelog
480 new_changelog = []
481 for line in string.split(string.strip(changelog), '\n'):
482 line = string.strip(line)
483 if line[0] == '*':
484 new_changelog.extend(['', line])
485 elif line[0] == '-':
486 new_changelog.append(line)
487 else:
488 new_changelog.append(' ' + line)
490 # strip trailing newline inserted by first changelog entry
491 if not new_changelog[0]:
492 del new_changelog[0]
494 return new_changelog
496 # _format_changelog()
498 # class bdist_rpm