1 """distutils.command.bdist_rpm
3 Implements the Distutils 'bdist_rpm' command (create RPM source and binary
12 from distutils
.core
import Command
13 from distutils
.debug
import DEBUG
14 from distutils
.file_util
import write_file
15 from distutils
.errors
import (DistutilsOptionError
, DistutilsPlatformError
,
16 DistutilsFileError
, DistutilsExecError
)
17 from distutils
import log
19 class bdist_rpm (Command
):
21 description
= "create an RPM distribution"
25 "base directory for creating built distributions"),
27 "base directory for creating RPMs (defaults to \"rpm\" under "
28 "--bdist-base; must be specified for RPM 2)"),
30 "directory to put final RPM files in "
31 "(and .spec files if --spec-only)"),
33 "path to Python interpreter to hard-code in the .spec file "
34 "(default: \"python\")"),
36 "hard-code the exact path to the current Python interpreter in "
39 "only regenerate spec file"),
41 "only generate source RPM"),
43 "only generate binary RPM"),
45 "use bzip2 instead of gzip to create source distribution"),
47 # More meta-data: too RPM-specific to put in the setup script,
48 # but needs to go in the .spec file -- so we make these options
49 # to "bdist_rpm". The idea is that packagers would put this
50 # info in setup.cfg, although they are of course free to
51 # supply it on the command line.
52 ('distribution-name=', None,
53 "name of the (Linux) distribution to which this "
54 "RPM applies (*not* the name of the module distribution!)"),
56 "package classification [default: \"Development/Libraries\"]"),
58 "RPM release number"),
62 "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
63 "[default: maintainer or author from setup script]"),
65 "RPM packager (eg. \"Jane Doe <jane@example.net>\")"
68 "list of documentation files (space or comma-separated)"),
74 "capabilities provided by this package"),
76 "capabilities required by this package"),
78 "capabilities which conflict with this package"),
79 ('build-requires=', None,
80 "capabilities required to build this package"),
82 "capabilities made obsolete by this package"),
84 "do not automatically calculate dependencies"),
86 # Actions to take when building RPM
88 "don't clean up RPM build directory"),
89 ('no-keep-temp', None,
90 "clean up RPM build directory [default]"),
91 ('use-rpm-opt-flags', None,
92 "compile with RPM_OPT_FLAGS when building from source RPM"),
93 ('no-rpm-opt-flags', None,
94 "do not pass any RPM CFLAGS to compiler"),
96 "RPM 3 compatibility mode (default)"),
98 "RPM 2 compatibility mode"),
100 # Add the hooks necessary for specifying custom scripts
101 ('prep-script=', None,
102 "Specify a script for the PREP phase of RPM building"),
103 ('build-script=', None,
104 "Specify a script for the BUILD phase of RPM building"),
106 ('pre-install=', None,
107 "Specify a script for the pre-INSTALL phase of RPM building"),
108 ('install-script=', None,
109 "Specify a script for the INSTALL phase of RPM building"),
110 ('post-install=', None,
111 "Specify a script for the post-INSTALL phase of RPM building"),
113 ('pre-uninstall=', None,
114 "Specify a script for the pre-UNINSTALL phase of RPM building"),
115 ('post-uninstall=', None,
116 "Specify a script for the post-UNINSTALL phase of RPM building"),
118 ('clean-script=', None,
119 "Specify a script for the CLEAN phase of RPM building"),
121 ('verify-script=', None,
122 "Specify a script for the VERIFY phase of the RPM build"),
124 # Allow a packager to explicitly force an architecture
125 ('force-arch=', None,
126 "Force an architecture onto the RPM build process"),
129 "Run the INSTALL phase of RPM building in quiet mode"),
132 boolean_options
= ['keep-temp', 'use-rpm-opt-flags', 'rpm3-mode',
133 'no-autoreq', 'quiet']
135 negative_opt
= {'no-keep-temp': 'keep-temp',
136 'no-rpm-opt-flags': 'use-rpm-opt-flags',
137 'rpm2-mode': 'rpm3-mode'}
140 def initialize_options (self
):
141 self
.bdist_base
= None
145 self
.fix_python
= None
146 self
.spec_only
= None
147 self
.binary_only
= None
148 self
.source_only
= None
149 self
.use_bzip2
= None
151 self
.distribution_name
= None
157 self
.doc_files
= None
158 self
.changelog
= None
161 self
.prep_script
= None
162 self
.build_script
= None
163 self
.install_script
= None
164 self
.clean_script
= None
165 self
.verify_script
= None
166 self
.pre_install
= None
167 self
.post_install
= None
168 self
.pre_uninstall
= None
169 self
.post_uninstall
= None
173 self
.conflicts
= None
174 self
.build_requires
= None
175 self
.obsoletes
= None
178 self
.use_rpm_opt_flags
= 1
182 self
.force_arch
= None
185 # initialize_options()
188 def finalize_options (self
):
189 self
.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
190 if self
.rpm_base
is None:
191 if not self
.rpm3_mode
:
192 raise DistutilsOptionError
, \
193 "you must specify --rpm-base in RPM 2 mode"
194 self
.rpm_base
= os
.path
.join(self
.bdist_base
, "rpm")
196 if self
.python
is None:
198 self
.python
= sys
.executable
200 self
.python
= "python"
201 elif self
.fix_python
:
202 raise DistutilsOptionError
, \
203 "--python and --fix-python are mutually exclusive options"
205 if os
.name
!= 'posix':
206 raise DistutilsPlatformError
, \
207 ("don't know how to create RPM "
208 "distributions on platform %s" % os
.name
)
209 if self
.binary_only
and self
.source_only
:
210 raise DistutilsOptionError
, \
211 "cannot supply both '--source-only' and '--binary-only'"
213 # don't pass CFLAGS to pure python distributions
214 if not self
.distribution
.has_ext_modules():
215 self
.use_rpm_opt_flags
= 0
217 self
.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
218 self
.finalize_package_data()
222 def finalize_package_data (self
):
223 self
.ensure_string('group', "Development/Libraries")
224 self
.ensure_string('vendor',
225 "%s <%s>" % (self
.distribution
.get_contact(),
226 self
.distribution
.get_contact_email()))
227 self
.ensure_string('packager')
228 self
.ensure_string_list('doc_files')
229 if isinstance(self
.doc_files
, list):
230 for readme
in ('README', 'README.txt'):
231 if os
.path
.exists(readme
) and readme
not in self
.doc_files
:
232 self
.doc_files
.append(readme
)
234 self
.ensure_string('release', "1")
235 self
.ensure_string('serial') # should it be an int?
237 self
.ensure_string('distribution_name')
239 self
.ensure_string('changelog')
240 # Format changelog correctly
241 self
.changelog
= self
._format
_changelog
(self
.changelog
)
243 self
.ensure_filename('icon')
245 self
.ensure_filename('prep_script')
246 self
.ensure_filename('build_script')
247 self
.ensure_filename('install_script')
248 self
.ensure_filename('clean_script')
249 self
.ensure_filename('verify_script')
250 self
.ensure_filename('pre_install')
251 self
.ensure_filename('post_install')
252 self
.ensure_filename('pre_uninstall')
253 self
.ensure_filename('post_uninstall')
255 # XXX don't forget we punted on summaries and descriptions -- they
256 # should be handled here eventually!
258 # Now *this* is some meta-data that belongs in the setup script...
259 self
.ensure_string_list('provides')
260 self
.ensure_string_list('requires')
261 self
.ensure_string_list('conflicts')
262 self
.ensure_string_list('build_requires')
263 self
.ensure_string_list('obsoletes')
265 self
.ensure_string('force_arch')
266 # finalize_package_data ()
272 print "before _get_package_data():"
273 print "vendor =", self
.vendor
274 print "packager =", self
.packager
275 print "doc_files =", self
.doc_files
276 print "changelog =", self
.changelog
280 spec_dir
= self
.dist_dir
281 self
.mkpath(spec_dir
)
284 for d
in ('SOURCES', 'SPECS', 'BUILD', 'RPMS', 'SRPMS'):
285 rpm_dir
[d
] = os
.path
.join(self
.rpm_base
, d
)
286 self
.mkpath(rpm_dir
[d
])
287 spec_dir
= rpm_dir
['SPECS']
289 # Spec file goes into 'dist_dir' if '--spec-only specified',
290 # build/rpm.<plat> otherwise.
291 spec_path
= os
.path
.join(spec_dir
,
292 "%s.spec" % self
.distribution
.get_name())
293 self
.execute(write_file
,
295 self
._make
_spec
_file
()),
296 "writing '%s'" % spec_path
)
298 if self
.spec_only
: # stop if requested
301 # Make a source distribution and copy to SOURCES directory with
303 saved_dist_files
= self
.distribution
.dist_files
[:]
304 sdist
= self
.reinitialize_command('sdist')
306 sdist
.formats
= ['bztar']
308 sdist
.formats
= ['gztar']
309 self
.run_command('sdist')
310 self
.distribution
.dist_files
= saved_dist_files
312 source
= sdist
.get_archive_files()[0]
313 source_dir
= rpm_dir
['SOURCES']
314 self
.copy_file(source
, source_dir
)
317 if os
.path
.exists(self
.icon
):
318 self
.copy_file(self
.icon
, source_dir
)
320 raise DistutilsFileError
, \
321 "icon file '%s' does not exist" % self
.icon
325 log
.info("building RPMs")
327 if os
.path
.exists('/usr/bin/rpmbuild') or \
328 os
.path
.exists('/bin/rpmbuild'):
329 rpm_cmd
= ['rpmbuild']
331 if self
.source_only
: # what kind of RPMs?
332 rpm_cmd
.append('-bs')
333 elif self
.binary_only
:
334 rpm_cmd
.append('-bb')
336 rpm_cmd
.append('-ba')
338 rpm_cmd
.extend(['--define',
339 '_topdir %s' % os
.path
.abspath(self
.rpm_base
)])
340 if not self
.keep_temp
:
341 rpm_cmd
.append('--clean')
344 rpm_cmd
.append('--quiet')
346 rpm_cmd
.append(spec_path
)
347 # Determine the binary rpm names that should be built out of this spec
349 # Note that some of these may not be really built (if the file
351 nvr_string
= "%{name}-%{version}-%{release}"
352 src_rpm
= nvr_string
+ ".src.rpm"
353 non_src_rpm
= "%{arch}/" + nvr_string
+ ".%{arch}.rpm"
354 q_cmd
= r
"rpm -q --qf '%s %s\n' --specfile '%s'" % (
355 src_rpm
, non_src_rpm
, spec_path
)
357 out
= os
.popen(q_cmd
)
362 line
= out
.readline()
365 l
= string
.split(string
.strip(line
))
367 binary_rpms
.append(l
[1])
368 # The source rpm is named after the first entry in the spec file
369 if source_rpm
is None:
374 raise DistutilsExecError("Failed to execute: %s" % repr(q_cmd
))
382 if self
.distribution
.has_ext_modules():
383 pyversion
= get_python_version()
387 if not self
.binary_only
:
388 srpm
= os
.path
.join(rpm_dir
['SRPMS'], source_rpm
)
389 assert(os
.path
.exists(srpm
))
390 self
.move_file(srpm
, self
.dist_dir
)
391 filename
= os
.path
.join(self
.dist_dir
, source_rpm
)
392 self
.distribution
.dist_files
.append(
393 ('bdist_rpm', pyversion
, filename
))
395 if not self
.source_only
:
396 for rpm
in binary_rpms
:
397 rpm
= os
.path
.join(rpm_dir
['RPMS'], rpm
)
398 if os
.path
.exists(rpm
):
399 self
.move_file(rpm
, self
.dist_dir
)
400 filename
= os
.path
.join(self
.dist_dir
,
401 os
.path
.basename(rpm
))
402 self
.distribution
.dist_files
.append(
403 ('bdist_rpm', pyversion
, filename
))
406 def _dist_path(self
, path
):
407 return os
.path
.join(self
.dist_dir
, os
.path
.basename(path
))
409 def _make_spec_file(self
):
410 """Generate the text of an RPM spec file and return it as a
411 list of strings (one per line).
413 # definitions and headers
415 '%define name ' + self
.distribution
.get_name(),
416 '%define version ' + self
.distribution
.get_version().replace('-','_'),
417 '%define unmangled_version ' + self
.distribution
.get_version(),
418 '%define release ' + self
.release
.replace('-','_'),
420 'Summary: ' + self
.distribution
.get_description(),
423 # put locale summaries into spec file
424 # XXX not supported for now (hard to put a dictionary
425 # in a config file -- arg!)
426 #for locale in self.summaries.keys():
427 # spec_file.append('Summary(%s): %s' % (locale,
428 # self.summaries[locale]))
432 'Version: %{version}',
433 'Release: %{release}',])
435 # XXX yuck! this filename is available from the "sdist" command,
436 # but only after it has run: and we create the spec file before
437 # running "sdist", in case of --spec-only.
439 spec_file
.append('Source0: %{name}-%{unmangled_version}.tar.bz2')
441 spec_file
.append('Source0: %{name}-%{unmangled_version}.tar.gz')
444 'License: ' + self
.distribution
.get_license(),
445 'Group: ' + self
.group
,
446 'BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot',
447 'Prefix: %{_prefix}', ])
449 if not self
.force_arch
:
450 # noarch if no extension modules
451 if not self
.distribution
.has_ext_modules():
452 spec_file
.append('BuildArch: noarch')
454 spec_file
.append( 'BuildArch: %s' % self
.force_arch
)
456 for field
in ('Vendor',
463 val
= getattr(self
, string
.lower(field
))
464 if isinstance(val
, list):
465 spec_file
.append('%s: %s' % (field
, string
.join(val
)))
466 elif val
is not None:
467 spec_file
.append('%s: %s' % (field
, val
))
470 if self
.distribution
.get_url() != 'UNKNOWN':
471 spec_file
.append('Url: ' + self
.distribution
.get_url())
473 if self
.distribution_name
:
474 spec_file
.append('Distribution: ' + self
.distribution_name
)
476 if self
.build_requires
:
477 spec_file
.append('BuildRequires: ' +
478 string
.join(self
.build_requires
))
481 spec_file
.append('Icon: ' + os
.path
.basename(self
.icon
))
484 spec_file
.append('AutoReq: 0')
489 self
.distribution
.get_long_description()
492 # put locale descriptions into spec file
493 # XXX again, suppressed because config file syntax doesn't
494 # easily support this ;-(
495 #for locale in self.descriptions.keys():
498 # '%description -l ' + locale,
499 # self.descriptions[locale],
503 # figure out default build script
504 def_setup_call
= "%s %s" % (self
.python
,os
.path
.basename(sys
.argv
[0]))
505 def_build
= "%s build" % def_setup_call
506 if self
.use_rpm_opt_flags
:
507 def_build
= 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
509 # insert contents of files
511 # XXX this is kind of misleading: user-supplied options are files
512 # that we open and interpolate into the spec file, but the defaults
513 # are just text that we drop in as-is. Hmmm.
515 install_cmd
= ('%s install -O1 --root=$RPM_BUILD_ROOT '
516 '--record=INSTALLED_FILES') % def_setup_call
519 ('prep', 'prep_script', "%setup -n %{name}-%{unmangled_version}"),
520 ('build', 'build_script', def_build
),
521 ('install', 'install_script', install_cmd
),
522 ('clean', 'clean_script', "rm -rf $RPM_BUILD_ROOT"),
523 ('verifyscript', 'verify_script', None),
524 ('pre', 'pre_install', None),
525 ('post', 'post_install', None),
526 ('preun', 'pre_uninstall', None),
527 ('postun', 'post_uninstall', None),
530 for (rpm_opt
, attr
, default
) in script_options
:
531 # Insert contents of file referred to, if no file is referred to
532 # use 'default' as contents of script
533 val
= getattr(self
, attr
)
539 spec_file
.extend(string
.split(open(val
, 'r').read(), '\n'))
541 spec_file
.append(default
)
547 '%files -f INSTALLED_FILES',
548 '%defattr(-,root,root)',
552 spec_file
.append('%doc ' + string
.join(self
.doc_files
))
558 spec_file
.extend(self
.changelog
)
564 def _format_changelog(self
, changelog
):
565 """Format the changelog correctly and convert it to a list of strings
570 for line
in string
.split(string
.strip(changelog
), '\n'):
571 line
= string
.strip(line
)
573 new_changelog
.extend(['', line
])
575 new_changelog
.append(line
)
577 new_changelog
.append(' ' + line
)
579 # strip trailing newline inserted by first changelog entry
580 if not new_changelog
[0]:
585 # _format_changelog()