1 """distutils.command.bdist_rpm
3 Implements the Distutils 'bdist_rpm' command (create RPM source and binary
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"
24 "base directory for creating built distributions"),
26 "base directory for creating RPMs (defaults to \"rpm\" under "
27 "--bdist-base; must be specified for RPM 2)"),
29 "directory to put final RPM files in "
30 "(and .spec files if --spec-only)"),
32 "path to Python interpreter to hard-code in the .spec file "
33 "(default: \"python\")"),
35 "hard-code the exact path to the current Python interpreter in "
38 "only regenerate spec file"),
40 "only generate source RPM"),
42 "only generate binary RPM"),
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!)"),
55 "package classification [default: \"Development/Libraries\"]"),
57 "RPM release number"),
61 "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
62 "[default: maintainer or author from setup script]"),
64 "RPM packager (eg. \"Jane Doe <jane@example.net>\")"
67 "list of documentation files (space or comma-separated)"),
73 "capabilities provided by this package"),
75 "capabilities required by this package"),
77 "capabilities which conflict with this package"),
78 ('build-requires=', None,
79 "capabilities required to build this package"),
81 "capabilities made obsolete by this package"),
83 "do not automatically calculate dependencies"),
85 # Actions to take when building RPM
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"),
95 "RPM 3 compatibility mode (default)"),
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"),
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
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
156 self
.doc_files
= None
157 self
.changelog
= 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
172 self
.conflicts
= None
173 self
.build_requires
= None
174 self
.obsoletes
= None
177 self
.use_rpm_opt_flags
= 1
181 self
.force_arch
= None
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:
197 self
.python
= sys
.executable
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()
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 ()
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
279 spec_dir
= self
.dist_dir
280 self
.mkpath(spec_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
,
294 self
._make
_spec
_file
()),
295 "writing '%s'" % spec_path
)
297 if self
.spec_only
: # stop if requested
300 # Make a source distribution and copy to SOURCES directory with
302 saved_dist_files
= self
.distribution
.dist_files
[:]
303 sdist
= self
.reinitialize_command('sdist')
305 sdist
.formats
= ['bztar']
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
)
316 if os
.path
.exists(self
.icon
):
317 self
.copy_file(self
.icon
, source_dir
)
319 raise DistutilsFileError
, \
320 "icon file '%s' does not exist" % self
.icon
324 log
.info("building RPMs")
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')
335 rpm_cmd
.append('-ba')
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')
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
348 # Note that some of these may not be really built (if the file
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
)
360 line
= out
.readline()
363 l
= string
.split(string
.strip(line
))
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:
372 raise DistutilsExecError("Failed to execute: %s" % repr(q_cmd
))
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
)
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
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]))
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.
422 spec_file
.append('Source0: %{name}-%{unmangled_version}.tar.bz2')
424 spec_file
.append('Source0: %{name}-%{unmangled_version}.tar.gz')
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')
437 spec_file
.append( 'BuildArch: %s' % self
.force_arch
)
439 for field
in ('Vendor',
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
))
464 spec_file
.append('Icon: ' + os
.path
.basename(self
.icon
))
467 spec_file
.append('AutoReq: 0')
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():
481 # '%description -l ' + locale,
482 # self.descriptions[locale],
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
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
)
522 spec_file
.extend(string
.split(open(val
, 'r').read(), '\n'))
524 spec_file
.append(default
)
530 '%files -f INSTALLED_FILES',
531 '%defattr(-,root,root)',
535 spec_file
.append('%doc ' + string
.join(self
.doc_files
))
541 spec_file
.extend(self
.changelog
)
547 def _format_changelog(self
, changelog
):
548 """Format the changelog correctly and convert it to a list of strings
553 for line
in string
.split(string
.strip(changelog
), '\n'):
554 line
= string
.strip(line
)
556 new_changelog
.extend(['', line
])
558 new_changelog
.append(line
)
560 new_changelog
.append(' ' + line
)
562 # strip trailing newline inserted by first changelog entry
563 if not new_changelog
[0]:
568 # _format_changelog()