Some platforms have rl_completion_append_character but not rl_completion_suppress_append.
[python.git] / Lib / distutils / command / bdist_rpm.py
blob1eccc6a48a9a73cbb3cdd9f13677163d508bbe5a
1 """distutils.command.bdist_rpm
3 Implements the Distutils 'bdist_rpm' command (create RPM source and binary
4 distributions)."""
6 __revision__ = "$Id$"
8 import sys, os, string
9 from types import *
10 from distutils.core import Command
11 from distutils.debug import DEBUG
12 from distutils.util import get_platform
13 from distutils.file_util import write_file
14 from distutils.errors import *
15 from distutils.sysconfig import get_python_version
16 from distutils import log
18 class bdist_rpm (Command):
20 description = "create an RPM distribution"
22 user_options = [
23 ('bdist-base=', None,
24 "base directory for creating built distributions"),
25 ('rpm-base=', None,
26 "base directory for creating RPMs (defaults to \"rpm\" under "
27 "--bdist-base; must be specified for RPM 2)"),
28 ('dist-dir=', 'd',
29 "directory to put final RPM files in "
30 "(and .spec files if --spec-only)"),
31 ('python=', None,
32 "path to Python interpreter to hard-code in the .spec file "
33 "(default: \"python\")"),
34 ('fix-python', None,
35 "hard-code the exact path to the current Python interpreter in "
36 "the .spec file"),
37 ('spec-only', None,
38 "only regenerate spec file"),
39 ('source-only', None,
40 "only generate source RPM"),
41 ('binary-only', None,
42 "only generate binary RPM"),
43 ('use-bzip2', None,
44 "use bzip2 instead of gzip to create source distribution"),
46 # More meta-data: too RPM-specific to put in the setup script,
47 # but needs to go in the .spec file -- so we make these options
48 # to "bdist_rpm". The idea is that packagers would put this
49 # info in setup.cfg, although they are of course free to
50 # supply it on the command line.
51 ('distribution-name=', None,
52 "name of the (Linux) distribution to which this "
53 "RPM applies (*not* the name of the module distribution!)"),
54 ('group=', None,
55 "package classification [default: \"Development/Libraries\"]"),
56 ('release=', None,
57 "RPM release number"),
58 ('serial=', None,
59 "RPM serial number"),
60 ('vendor=', None,
61 "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
62 "[default: maintainer or author from setup script]"),
63 ('packager=', None,
64 "RPM packager (eg. \"Jane Doe <jane@example.net>\")"
65 "[default: vendor]"),
66 ('doc-files=', None,
67 "list of documentation files (space or comma-separated)"),
68 ('changelog=', None,
69 "RPM changelog"),
70 ('icon=', None,
71 "name of icon file"),
72 ('provides=', None,
73 "capabilities provided by this package"),
74 ('requires=', None,
75 "capabilities required by this package"),
76 ('conflicts=', None,
77 "capabilities which conflict with this package"),
78 ('build-requires=', None,
79 "capabilities required to build this package"),
80 ('obsoletes=', None,
81 "capabilities made obsolete by this package"),
82 ('no-autoreq', None,
83 "do not automatically calculate dependencies"),
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"),
99 # Add the hooks necessary for specifying custom scripts
100 ('prep-script=', None,
101 "Specify a script for the PREP phase of RPM building"),
102 ('build-script=', None,
103 "Specify a script for the BUILD phase of RPM building"),
105 ('pre-install=', None,
106 "Specify a script for the pre-INSTALL phase of RPM building"),
107 ('install-script=', None,
108 "Specify a script for the INSTALL phase of RPM building"),
109 ('post-install=', None,
110 "Specify a script for the post-INSTALL phase of RPM building"),
112 ('pre-uninstall=', None,
113 "Specify a script for the pre-UNINSTALL phase of RPM building"),
114 ('post-uninstall=', None,
115 "Specify a script for the post-UNINSTALL phase of RPM building"),
117 ('clean-script=', None,
118 "Specify a script for the CLEAN phase of RPM building"),
120 ('verify-script=', None,
121 "Specify a script for the VERIFY phase of the RPM build"),
123 # Allow a packager to explicitly force an architecture
124 ('force-arch=', None,
125 "Force an architecture onto the RPM build process"),
127 ('quiet', 'q',
128 "Run the INSTALL phase of RPM building in quiet mode"),
131 boolean_options = ['keep-temp', 'use-rpm-opt-flags', 'rpm3-mode',
132 'no-autoreq', 'quiet']
134 negative_opt = {'no-keep-temp': 'keep-temp',
135 'no-rpm-opt-flags': 'use-rpm-opt-flags',
136 'rpm2-mode': 'rpm3-mode'}
139 def initialize_options (self):
140 self.bdist_base = None
141 self.rpm_base = None
142 self.dist_dir = None
143 self.python = None
144 self.fix_python = None
145 self.spec_only = None
146 self.binary_only = None
147 self.source_only = None
148 self.use_bzip2 = None
150 self.distribution_name = None
151 self.group = None
152 self.release = None
153 self.serial = None
154 self.vendor = None
155 self.packager = None
156 self.doc_files = None
157 self.changelog = None
158 self.icon = None
160 self.prep_script = None
161 self.build_script = None
162 self.install_script = None
163 self.clean_script = None
164 self.verify_script = None
165 self.pre_install = None
166 self.post_install = None
167 self.pre_uninstall = None
168 self.post_uninstall = None
169 self.prep = None
170 self.provides = None
171 self.requires = None
172 self.conflicts = None
173 self.build_requires = None
174 self.obsoletes = None
176 self.keep_temp = 0
177 self.use_rpm_opt_flags = 1
178 self.rpm3_mode = 1
179 self.no_autoreq = 0
181 self.force_arch = None
182 self.quiet = 0
184 # initialize_options()
187 def finalize_options (self):
188 self.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
189 if self.rpm_base is None:
190 if not self.rpm3_mode:
191 raise DistutilsOptionError, \
192 "you must specify --rpm-base in RPM 2 mode"
193 self.rpm_base = os.path.join(self.bdist_base, "rpm")
195 if self.python is None:
196 if self.fix_python:
197 self.python = sys.executable
198 else:
199 self.python = "python"
200 elif self.fix_python:
201 raise DistutilsOptionError, \
202 "--python and --fix-python are mutually exclusive options"
204 if os.name != 'posix':
205 raise DistutilsPlatformError, \
206 ("don't know how to create RPM "
207 "distributions on platform %s" % os.name)
208 if self.binary_only and self.source_only:
209 raise DistutilsOptionError, \
210 "cannot supply both '--source-only' and '--binary-only'"
212 # don't pass CFLAGS to pure python distributions
213 if not self.distribution.has_ext_modules():
214 self.use_rpm_opt_flags = 0
216 self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
217 self.finalize_package_data()
219 # finalize_options()
221 def finalize_package_data (self):
222 self.ensure_string('group', "Development/Libraries")
223 self.ensure_string('vendor',
224 "%s <%s>" % (self.distribution.get_contact(),
225 self.distribution.get_contact_email()))
226 self.ensure_string('packager')
227 self.ensure_string_list('doc_files')
228 if type(self.doc_files) is ListType:
229 for readme in ('README', 'README.txt'):
230 if os.path.exists(readme) and readme not in self.doc_files:
231 self.doc_files.append(readme)
233 self.ensure_string('release', "1")
234 self.ensure_string('serial') # should it be an int?
236 self.ensure_string('distribution_name')
238 self.ensure_string('changelog')
239 # Format changelog correctly
240 self.changelog = self._format_changelog(self.changelog)
242 self.ensure_filename('icon')
244 self.ensure_filename('prep_script')
245 self.ensure_filename('build_script')
246 self.ensure_filename('install_script')
247 self.ensure_filename('clean_script')
248 self.ensure_filename('verify_script')
249 self.ensure_filename('pre_install')
250 self.ensure_filename('post_install')
251 self.ensure_filename('pre_uninstall')
252 self.ensure_filename('post_uninstall')
254 # XXX don't forget we punted on summaries and descriptions -- they
255 # should be handled here eventually!
257 # Now *this* is some meta-data that belongs in the setup script...
258 self.ensure_string_list('provides')
259 self.ensure_string_list('requires')
260 self.ensure_string_list('conflicts')
261 self.ensure_string_list('build_requires')
262 self.ensure_string_list('obsoletes')
264 self.ensure_string('force_arch')
265 # finalize_package_data ()
268 def run (self):
270 if DEBUG:
271 print "before _get_package_data():"
272 print "vendor =", self.vendor
273 print "packager =", self.packager
274 print "doc_files =", self.doc_files
275 print "changelog =", self.changelog
277 # make directories
278 if self.spec_only:
279 spec_dir = self.dist_dir
280 self.mkpath(spec_dir)
281 else:
282 rpm_dir = {}
283 for d in ('SOURCES', 'SPECS', 'BUILD', 'RPMS', 'SRPMS'):
284 rpm_dir[d] = os.path.join(self.rpm_base, d)
285 self.mkpath(rpm_dir[d])
286 spec_dir = rpm_dir['SPECS']
288 # Spec file goes into 'dist_dir' if '--spec-only specified',
289 # build/rpm.<plat> otherwise.
290 spec_path = os.path.join(spec_dir,
291 "%s.spec" % self.distribution.get_name())
292 self.execute(write_file,
293 (spec_path,
294 self._make_spec_file()),
295 "writing '%s'" % spec_path)
297 if self.spec_only: # stop if requested
298 return
300 # Make a source distribution and copy to SOURCES directory with
301 # optional icon.
302 saved_dist_files = self.distribution.dist_files[:]
303 sdist = self.reinitialize_command('sdist')
304 if self.use_bzip2:
305 sdist.formats = ['bztar']
306 else:
307 sdist.formats = ['gztar']
308 self.run_command('sdist')
309 self.distribution.dist_files = saved_dist_files
311 source = sdist.get_archive_files()[0]
312 source_dir = rpm_dir['SOURCES']
313 self.copy_file(source, source_dir)
315 if self.icon:
316 if os.path.exists(self.icon):
317 self.copy_file(self.icon, source_dir)
318 else:
319 raise DistutilsFileError, \
320 "icon file '%s' does not exist" % self.icon
323 # build package
324 log.info("building RPMs")
325 rpm_cmd = ['rpm']
326 if os.path.exists('/usr/bin/rpmbuild') or \
327 os.path.exists('/bin/rpmbuild'):
328 rpm_cmd = ['rpmbuild']
330 if self.source_only: # what kind of RPMs?
331 rpm_cmd.append('-bs')
332 elif self.binary_only:
333 rpm_cmd.append('-bb')
334 else:
335 rpm_cmd.append('-ba')
336 if self.rpm3_mode:
337 rpm_cmd.extend(['--define',
338 '_topdir %s' % os.path.abspath(self.rpm_base)])
339 if not self.keep_temp:
340 rpm_cmd.append('--clean')
342 if self.quiet:
343 rpm_cmd.append('--quiet')
345 rpm_cmd.append(spec_path)
346 # Determine the binary rpm names that should be built out of this spec
347 # file
348 # Note that some of these may not be really built (if the file
349 # list is empty)
350 nvr_string = "%{name}-%{version}-%{release}"
351 src_rpm = nvr_string + ".src.rpm"
352 non_src_rpm = "%{arch}/" + nvr_string + ".%{arch}.rpm"
353 q_cmd = r"rpm -q --qf '%s %s\n' --specfile '%s'" % (
354 src_rpm, non_src_rpm, spec_path)
356 out = os.popen(q_cmd)
357 binary_rpms = []
358 source_rpm = None
359 while 1:
360 line = out.readline()
361 if not line:
362 break
363 l = string.split(string.strip(line))
364 assert(len(l) == 2)
365 binary_rpms.append(l[1])
366 # The source rpm is named after the first entry in the spec file
367 if source_rpm is None:
368 source_rpm = l[0]
370 status = out.close()
371 if status:
372 raise DistutilsExecError("Failed to execute: %s" % repr(q_cmd))
374 self.spawn(rpm_cmd)
376 if not self.dry_run:
377 if not self.binary_only:
378 srpm = os.path.join(rpm_dir['SRPMS'], source_rpm)
379 assert(os.path.exists(srpm))
380 self.move_file(srpm, self.dist_dir)
382 if not self.source_only:
383 for rpm in binary_rpms:
384 rpm = os.path.join(rpm_dir['RPMS'], rpm)
385 if os.path.exists(rpm):
386 self.move_file(rpm, self.dist_dir)
387 # run()
389 def _dist_path(self, path):
390 return os.path.join(self.dist_dir, os.path.basename(path))
392 def _make_spec_file(self):
393 """Generate the text of an RPM spec file and return it as a
394 list of strings (one per line).
396 # definitions and headers
397 spec_file = [
398 '%define name ' + self.distribution.get_name(),
399 '%define version ' + self.distribution.get_version().replace('-','_'),
400 '%define unmangled_version ' + self.distribution.get_version(),
401 '%define release ' + self.release.replace('-','_'),
403 'Summary: ' + self.distribution.get_description(),
406 # put locale summaries into spec file
407 # XXX not supported for now (hard to put a dictionary
408 # in a config file -- arg!)
409 #for locale in self.summaries.keys():
410 # spec_file.append('Summary(%s): %s' % (locale,
411 # self.summaries[locale]))
413 spec_file.extend([
414 'Name: %{name}',
415 'Version: %{version}',
416 'Release: %{release}',])
418 # XXX yuck! this filename is available from the "sdist" command,
419 # but only after it has run: and we create the spec file before
420 # running "sdist", in case of --spec-only.
421 if self.use_bzip2:
422 spec_file.append('Source0: %{name}-%{unmangled_version}.tar.bz2')
423 else:
424 spec_file.append('Source0: %{name}-%{unmangled_version}.tar.gz')
426 spec_file.extend([
427 'License: ' + self.distribution.get_license(),
428 'Group: ' + self.group,
429 'BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot',
430 'Prefix: %{_prefix}', ])
432 if not self.force_arch:
433 # noarch if no extension modules
434 if not self.distribution.has_ext_modules():
435 spec_file.append('BuildArch: noarch')
436 else:
437 spec_file.append( 'BuildArch: %s' % self.force_arch )
439 for field in ('Vendor',
440 'Packager',
441 'Provides',
442 'Requires',
443 'Conflicts',
444 'Obsoletes',
446 val = getattr(self, string.lower(field))
447 if type(val) is ListType:
448 spec_file.append('%s: %s' % (field, string.join(val)))
449 elif val is not None:
450 spec_file.append('%s: %s' % (field, val))
453 if self.distribution.get_url() != 'UNKNOWN':
454 spec_file.append('Url: ' + self.distribution.get_url())
456 if self.distribution_name:
457 spec_file.append('Distribution: ' + self.distribution_name)
459 if self.build_requires:
460 spec_file.append('BuildRequires: ' +
461 string.join(self.build_requires))
463 if self.icon:
464 spec_file.append('Icon: ' + os.path.basename(self.icon))
466 if self.no_autoreq:
467 spec_file.append('AutoReq: 0')
469 spec_file.extend([
471 '%description',
472 self.distribution.get_long_description()
475 # put locale descriptions into spec file
476 # XXX again, suppressed because config file syntax doesn't
477 # easily support this ;-(
478 #for locale in self.descriptions.keys():
479 # spec_file.extend([
480 # '',
481 # '%description -l ' + locale,
482 # self.descriptions[locale],
483 # ])
485 # rpm scripts
486 # figure out default build script
487 def_setup_call = "%s %s" % (self.python,os.path.basename(sys.argv[0]))
488 def_build = "%s build" % def_setup_call
489 if self.use_rpm_opt_flags:
490 def_build = 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
492 # insert contents of files
494 # XXX this is kind of misleading: user-supplied options are files
495 # that we open and interpolate into the spec file, but the defaults
496 # are just text that we drop in as-is. Hmmm.
498 install_cmd = ('%s install -O1 --root=$RPM_BUILD_ROOT '
499 '--record=INSTALLED_FILES') % def_setup_call
501 script_options = [
502 ('prep', 'prep_script', "%setup -n %{name}-%{unmangled_version}"),
503 ('build', 'build_script', def_build),
504 ('install', 'install_script', install_cmd),
505 ('clean', 'clean_script', "rm -rf $RPM_BUILD_ROOT"),
506 ('verifyscript', 'verify_script', None),
507 ('pre', 'pre_install', None),
508 ('post', 'post_install', None),
509 ('preun', 'pre_uninstall', None),
510 ('postun', 'post_uninstall', None),
513 for (rpm_opt, attr, default) in script_options:
514 # Insert contents of file referred to, if no file is referred to
515 # use 'default' as contents of script
516 val = getattr(self, attr)
517 if val or default:
518 spec_file.extend([
520 '%' + rpm_opt,])
521 if val:
522 spec_file.extend(string.split(open(val, 'r').read(), '\n'))
523 else:
524 spec_file.append(default)
527 # files section
528 spec_file.extend([
530 '%files -f INSTALLED_FILES',
531 '%defattr(-,root,root)',
534 if self.doc_files:
535 spec_file.append('%doc ' + string.join(self.doc_files))
537 if self.changelog:
538 spec_file.extend([
540 '%changelog',])
541 spec_file.extend(self.changelog)
543 return spec_file
545 # _make_spec_file ()
547 def _format_changelog(self, changelog):
548 """Format the changelog correctly and convert it to a list of strings
550 if not changelog:
551 return changelog
552 new_changelog = []
553 for line in string.split(string.strip(changelog), '\n'):
554 line = string.strip(line)
555 if line[0] == '*':
556 new_changelog.extend(['', line])
557 elif line[0] == '-':
558 new_changelog.append(line)
559 else:
560 new_changelog.append(' ' + line)
562 # strip trailing newline inserted by first changelog entry
563 if not new_changelog[0]:
564 del new_changelog[0]
566 return new_changelog
568 # _format_changelog()
570 # class bdist_rpm