1 """distutils.command.bdist_rpm
3 Implements the Distutils 'bdist_rpm' command (create RPM source and binary
6 # created 2000/04/25, by Harry Henry Gebel
10 import sys
, os
, string
13 from distutils
.core
import Command
, DEBUG
14 from distutils
.util
import get_platform
15 from distutils
.file_util
import write_file
16 from distutils
.errors
import *
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)"),
70 "path to RPM changelog"),
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 # Actions to take when building RPM
86 "don't clean up RPM build directory"),
87 ('no-keep-temp', None,
88 "clean up RPM build directory [default]"),
89 ('use-rpm-opt-flags', None,
90 "compile with RPM_OPT_FLAGS when building from source RPM"),
91 ('no-rpm-opt-flags', None,
92 "do not pass any RPM CFLAGS to compiler"),
94 "RPM 3 compatibility mode (default)"),
96 "RPM 2 compatibility mode"),
99 boolean_options
= ['keep-temp', 'use-rpm-opt-flags', 'rpm3-mode']
101 negative_opt
= {'no-keep-temp': 'keep-temp',
102 'no-rpm-opt-flags': 'use-rpm-opt-flags',
103 'rpm2-mode': 'rpm3-mode'}
106 def initialize_options (self
):
107 self
.bdist_base
= None
111 self
.fix_python
= None
112 self
.spec_only
= None
113 self
.binary_only
= None
114 self
.source_only
= None
115 self
.use_bzip2
= None
117 self
.distribution_name
= None
123 self
.doc_files
= None
124 self
.changelog
= None
127 self
.prep_script
= None
128 self
.build_script
= None
129 self
.install_script
= None
130 self
.clean_script
= None
131 self
.pre_install
= None
132 self
.post_install
= None
133 self
.pre_uninstall
= None
134 self
.post_uninstall
= None
138 self
.conflicts
= None
139 self
.build_requires
= None
140 self
.obsoletes
= None
143 self
.use_rpm_opt_flags
= 1
146 # initialize_options()
149 def finalize_options (self
):
150 self
.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
151 if self
.rpm_base
is None:
152 if not self
.rpm3_mode
:
153 raise DistutilsOptionError
, \
154 "you must specify --rpm-base in RPM 2 mode"
155 self
.rpm_base
= os
.path
.join(self
.bdist_base
, "rpm")
157 if self
.python
is None:
159 self
.python
= sys
.executable
161 self
.python
= "python"
162 elif self
.fix_python
:
163 raise DistutilsOptionError
, \
164 "--python and --fix-python are mutually exclusive options"
166 if os
.name
!= 'posix':
167 raise DistutilsPlatformError
, \
168 ("don't know how to create RPM "
169 "distributions on platform %s" % os
.name
)
170 if self
.binary_only
and self
.source_only
:
171 raise DistutilsOptionError
, \
172 "cannot supply both '--source-only' and '--binary-only'"
174 # don't pass CFLAGS to pure python distributions
175 if not self
.distribution
.has_ext_modules():
176 self
.use_rpm_opt_flags
= 0
178 self
.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
179 self
.finalize_package_data()
183 def finalize_package_data (self
):
184 self
.ensure_string('group', "Development/Libraries")
185 self
.ensure_string('vendor',
186 "%s <%s>" % (self
.distribution
.get_contact(),
187 self
.distribution
.get_contact_email()))
188 self
.ensure_string('packager')
189 self
.ensure_string_list('doc_files')
190 if type(self
.doc_files
) is ListType
:
191 for readme
in ('README', 'README.txt'):
192 if os
.path
.exists(readme
) and readme
not in self
.doc_files
:
193 self
.doc_files
.append(readme
)
195 self
.ensure_string('release', "1")
196 self
.ensure_string('serial') # should it be an int?
198 self
.ensure_string('distribution_name')
200 self
.ensure_string('changelog')
201 # Format changelog correctly
202 self
.changelog
= self
._format
_changelog
(self
.changelog
)
204 self
.ensure_filename('icon')
206 self
.ensure_filename('prep_script')
207 self
.ensure_filename('build_script')
208 self
.ensure_filename('install_script')
209 self
.ensure_filename('clean_script')
210 self
.ensure_filename('pre_install')
211 self
.ensure_filename('post_install')
212 self
.ensure_filename('pre_uninstall')
213 self
.ensure_filename('post_uninstall')
215 # XXX don't forget we punted on summaries and descriptions -- they
216 # should be handled here eventually!
218 # Now *this* is some meta-data that belongs in the setup script...
219 self
.ensure_string_list('provides')
220 self
.ensure_string_list('requires')
221 self
.ensure_string_list('conflicts')
222 self
.ensure_string_list('build_requires')
223 self
.ensure_string_list('obsoletes')
225 # finalize_package_data ()
231 print "before _get_package_data():"
232 print "vendor =", self
.vendor
233 print "packager =", self
.packager
234 print "doc_files =", self
.doc_files
235 print "changelog =", self
.changelog
239 spec_dir
= self
.dist_dir
240 self
.mkpath(spec_dir
)
243 for d
in ('SOURCES', 'SPECS', 'BUILD', 'RPMS', 'SRPMS'):
244 rpm_dir
[d
] = os
.path
.join(self
.rpm_base
, d
)
245 self
.mkpath(rpm_dir
[d
])
246 spec_dir
= rpm_dir
['SPECS']
248 # Spec file goes into 'dist_dir' if '--spec-only specified',
249 # build/rpm.<plat> otherwise.
250 spec_path
= os
.path
.join(spec_dir
,
251 "%s.spec" % self
.distribution
.get_name())
252 self
.execute(write_file
,
254 self
._make
_spec
_file
()),
255 "writing '%s'" % spec_path
)
257 if self
.spec_only
: # stop if requested
260 # Make a source distribution and copy to SOURCES directory with
262 sdist
= self
.reinitialize_command('sdist')
264 sdist
.formats
= ['bztar']
266 sdist
.formats
= ['gztar']
267 self
.run_command('sdist')
269 source
= sdist
.get_archive_files()[0]
270 source_dir
= rpm_dir
['SOURCES']
271 self
.copy_file(source
, source_dir
)
274 if os
.path
.exists(self
.icon
):
275 self
.copy_file(self
.icon
, source_dir
)
277 raise DistutilsFileError
, \
278 "icon file '%s' does not exist" % self
.icon
282 log
.info("building RPMs")
284 if self
.source_only
: # what kind of RPMs?
285 rpm_cmd
.append('-bs')
286 elif self
.binary_only
:
287 rpm_cmd
.append('-bb')
289 rpm_cmd
.append('-ba')
291 rpm_cmd
.extend(['--define',
292 '_topdir %s/%s' % (os
.getcwd(), self
.rpm_base
),])
293 if not self
.keep_temp
:
294 rpm_cmd
.append('--clean')
295 rpm_cmd
.append(spec_path
)
298 # XXX this is a nasty hack -- we really should have a proper way to
299 # find out the names of the RPM files created; also, this assumes
300 # that RPM creates exactly one source and one binary RPM.
302 if not self
.binary_only
:
303 srpms
= glob
.glob(os
.path
.join(rpm_dir
['SRPMS'], "*.rpm"))
304 assert len(srpms
) == 1, \
305 "unexpected number of SRPM files found: %s" % srpms
306 self
.move_file(srpms
[0], self
.dist_dir
)
308 if not self
.source_only
:
309 rpms
= glob
.glob(os
.path
.join(rpm_dir
['RPMS'], "*/*.rpm"))
310 assert len(rpms
) == 1, \
311 "unexpected number of RPM files found: %s" % rpms
312 self
.move_file(rpms
[0], self
.dist_dir
)
317 def _make_spec_file(self
):
318 """Generate the text of an RPM spec file and return it as a
319 list of strings (one per line).
321 # definitions and headers
323 '%define name ' + self
.distribution
.get_name(),
324 '%define version ' + self
.distribution
.get_version(),
325 '%define release ' + self
.release
,
327 'Summary: ' + self
.distribution
.get_description(),
330 # put locale summaries into spec file
331 # XXX not supported for now (hard to put a dictionary
332 # in a config file -- arg!)
333 #for locale in self.summaries.keys():
334 # spec_file.append('Summary(%s): %s' % (locale,
335 # self.summaries[locale]))
339 'Version: %{version}',
340 'Release: %{release}',])
342 # XXX yuck! this filename is available from the "sdist" command,
343 # but only after it has run: and we create the spec file before
344 # running "sdist", in case of --spec-only.
346 spec_file
.append('Source0: %{name}-%{version}.tar.bz2')
348 spec_file
.append('Source0: %{name}-%{version}.tar.gz')
351 'Copyright: ' + self
.distribution
.get_license(),
352 'Group: ' + self
.group
,
353 'BuildRoot: %{_tmppath}/%{name}-buildroot',
354 'Prefix: %{_prefix}', ])
356 # noarch if no extension modules
357 if not self
.distribution
.has_ext_modules():
358 spec_file
.append('BuildArchitectures: noarch')
360 for field
in ('Vendor',
367 val
= getattr(self
, string
.lower(field
))
368 if type(val
) is ListType
:
369 spec_file
.append('%s: %s' % (field
, string
.join(val
)))
370 elif val
is not None:
371 spec_file
.append('%s: %s' % (field
, val
))
374 if self
.distribution
.get_url() != 'UNKNOWN':
375 spec_file
.append('Url: ' + self
.distribution
.get_url())
377 if self
.distribution_name
:
378 spec_file
.append('Distribution: ' + self
.distribution_name
)
380 if self
.build_requires
:
381 spec_file
.append('BuildRequires: ' +
382 string
.join(self
.build_requires
))
385 spec_file
.append('Icon: ' + os
.path
.basename(self
.icon
))
390 self
.distribution
.get_long_description()
393 # put locale descriptions into spec file
394 # XXX again, suppressed because config file syntax doesn't
395 # easily support this ;-(
396 #for locale in self.descriptions.keys():
399 # '%description -l ' + locale,
400 # self.descriptions[locale],
404 # figure out default build script
405 def_build
= "%s setup.py build" % self
.python
406 if self
.use_rpm_opt_flags
:
407 def_build
= 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
409 # insert contents of files
411 # XXX this is kind of misleading: user-supplied options are files
412 # that we open and interpolate into the spec file, but the defaults
413 # are just text that we drop in as-is. Hmmm.
416 ('prep', 'prep_script', "%setup"),
417 ('build', 'build_script', def_build
),
418 ('install', 'install_script',
419 ("%s setup.py install "
420 "--root=$RPM_BUILD_ROOT "
421 "--record=INSTALLED_FILES") % self
.python
),
422 ('clean', 'clean_script', "rm -rf $RPM_BUILD_ROOT"),
423 ('pre', 'pre_install', None),
424 ('post', 'post_install', None),
425 ('preun', 'pre_uninstall', None),
426 ('postun', 'post_uninstall', None),
429 for (rpm_opt
, attr
, default
) in script_options
:
430 # Insert contents of file referred to, if no file is refered to
431 # use 'default' as contents of script
432 val
= getattr(self
, attr
)
438 spec_file
.extend(string
.split(open(val
, 'r').read(), '\n'))
440 spec_file
.append(default
)
446 '%files -f INSTALLED_FILES',
447 '%defattr(-,root,root)',
451 spec_file
.append('%doc ' + string
.join(self
.doc_files
))
457 spec_file
.extend(self
.changelog
)
463 def _format_changelog(self
, changelog
):
464 """Format the changelog correctly and convert it to a list of strings
469 for line
in string
.split(string
.strip(changelog
), '\n'):
470 line
= string
.strip(line
)
472 new_changelog
.extend(['', line
])
474 new_changelog
.append(line
)
476 new_changelog
.append(' ' + line
)
478 # strip trailing newline inserted by first changelog entry
479 if not new_changelog
[0]:
484 # _format_changelog()