[Distutils] Nem bdist_rpm
Harry Henry Gebel
hgebel@inet.net
Fri, 5 May 2000 11:38:41 -0400
--n8g4imXOkfNTN/H1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Here is a new version of bdist_rpm. bdist_rpm now builds in build/rpm
using `rpm -ba` so it no longer requires the spec file to be in the source
distribution and can be completely built by non-root users. Here are the
current options:
--spec-only - only generates a spec file
--source-only - only build a source RPM
--binary-only - only build a binary RPW
--use-bzip2 - makes source distribution using bzip2
--no-clean - does not clean build/rpm/BUILD after building RPM
--arch has been temporarily disabled since 'rpm -ba --target=' does not
seem to work with 'rpm -ba --define'. I will be trying to find out why on
the RPM list.
`./setup.py clean` does not yet take care of 'build/rpm'.
When used with the --spec-only option the spec file is placed in redhat/,
otherwise it is placed in /build/rpm/SPECS.
--
Harry Henry Gebel, Senior Developer, Landon House SBS
West Dover Hundred, Delaware
PyNcurses ncurses binding for Python http://pyncurses.sourceforge.net
--n8g4imXOkfNTN/H1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="bdist_rpm.py"
"""distutils.command.bdist_rpm
Implements the Distutils 'bdist_rpm' command (create RPM source and binary
distributions."""
# created 2000/04/25, by Harry Henry Gebel
__revision__ = "$Id: bdist_dumb.py,v 1.3 2000/04/22 02:51:25 gward Exp $"
from os.path import exists
import os
from distutils.core import Command
from distutils.util import mkpath, write_file, copy_file
from distutils.errors import *
from string import join
from types import StringType, DictType, LongType, FloatType, IntType
class bdist_rpm (Command):
description = "create an RPM distribution"
user_options = [
('spec-only', None,
"Only regenerate spec file"),
# ('arch=', None,
# "Build for a specific architecture"),
('source-only', None,
"Only generate source RPM"),
('binary-only', None,
"Only generate binary RPM"),
('use-bzip2', None,
"Use bzip2 instead of gzip to create source distribution"),
('no-clean', None,
"Do not clean RPM build directory")
]
def initialize_options (self):
self.spec_only = None
# self.arch = None
self.binary_only = None
self.source_only = None
self.use_bzip2 = None
self.no_clean = None
# initialize_options()
def finalize_options (self):
if os.name != 'posix':
raise DistutilsPlatformError, \
("don't know how to create RPM "
"distributions on platform %s" % os.name)
if self.binary_only and self.source_only:
raise DistutilsOptionsError, \
"Cannot supply both '--source-only' and '--binary-only'"
# finalize_options()
def run (self):
self._get_package_data() # get packaging info
# make directories
if self.spec_only:
self.execute(mkpath, ('redhat',), "Created './redhat' directory")
else:
self.execute(mkpath, ('build/rpm/SOURCES',),
"Created RPM source directory")
self.execute(mkpath, ('build/rpm/SPECS',),
"Created RPM source directory")
self.execute(mkpath, ('build/rpm/BUILD',),
"Created RPM source directory")
self.execute(mkpath, ('build/rpm/RPMS',),
"Created RPM source directory")
self.execute(mkpath, ('build/rpm/SRPMS',),
"Created RPM source directory")
# spec file goes into .redhat directory if '--spec-only specified',
# into build/rpm/spec otherwisu
if self.spec_only:
spec_path = './redhat/%s.spec' % self.distribution.get_name()
else:
spec_path = ('build/rpm/SPECS/%s.spec' %
self.distribution.get_name())
self.execute(write_file,
(spec_path,
self._make_spec_file()),
'Writing .spec file')
if self.spec_only: # stop if requested
return
# make a source distribution and copy to SOURCES directory
sdist = self.find_peer ('sdist')
if self.use_bzip2:
sdist.set_option ('formats', ['bztar'])
else:
sdist.set_option ('formats', ['gztar'])
self.run_peer('sdist')
if self.use_bzip2:
source = self.distribution.get_fullname() + '.tar.bz2'
else:
source = self.distribution.get_fullname() + '.tar.gz'
self.execute(copy_file, (source, 'build/rpm/SOURCES'),
'Copying source distribution to SOURCES')
# build package
self.announce('Building RPMs')
rpm_args = ['rpm',]
if self.source_only: # what kind of RPMs?
rpm_args.append('-bs')
elif self.binary_only:
rpm_args.append('-bb')
else:
rpm_args.append('-ba')
topdir = os.getcwd() + 'build/rpm'
rpm_args.extend(['--define',
'_topdir ' + os.getcwd() + '/build/rpm',])
if not self.no_clean:
rpm_args.append('--clean')
# if self.arch and self.distribution.has_ext_modules():
# rpm_args.extend('define', '_target_cpu ' + self.arch)
rpm_args.append(spec_path)
self.spawn(rpm_args)
# run()
def _get_package_data(self):
''' Get data needed to generate spec file, first from the
DistributionMetadata class, then from the package_data file, which is
Python code read with execfile() '''
package_type = 'rpm'
# read in package data, if any
if exists('package_data'):
try:
exec(open('package_data'))
except:
raise DistutilsOptionError, 'Unable to parse package data file'
# set instance variables, supplying default value if not provided in
# package data file
vars = locals().keys()
# the following variables must be {string (len() = 2): string}
if 'summaries' in vars:
self.summaries = self._check_locale(summaries, 'summaries')
else:
self.summaries = {}
if 'descriptions' in vars:
self.descriptions = self._check_locale(descriptions,
'descriptions')
else:
self.descriptions = {}
# The following variable must be an ordinary number or a string
if 'release' in vars:
if type(release) in (StringType, LongType, IntType, FloatType):
self.release = str(release)
else:
raise DistutilsOptionError, \
("Error in package_data: 'release' must be a number or "
'a string')
else:
self.release = '1'
# The following variables must be strings
if 'group' in vars:
self.group = self._check_string(group, 'group')
else:
self.group = 'Applications'
if 'vendor' in vars:
self.vendor = self._check_string(vendor, 'vendor')
else:
self.vendor = None
if 'packager' in vars:
self.packager = self._check_string(packager, 'vendor')
else:
self.packager = None
if 'changelog' in vars:
self.changelog = self._check_string(changelog, 'vendor')
else:
self.changelog = None
# doc must be a list (or tuple) of strings, or a string
if 'doc' not in vars:
doc = []
for readme in ('README', 'README.txt'):
if exists(readme):
doc.append(readme)
try:
self.doc = join(doc)
except:
raise DistutilsOptionError, \
"Error in package_data: 'doc' must be a sequence of strings"
if 'changelog' in vars:
self.changelog = changelog
else:
self.changelog = None
def _make_spec_file(self):
''' Generate an RPM spec file '''
spec_file = [
'%define name ' + self.distribution.get_name(),
'%define version ' + self.distribution.get_version(),
'%define release ' + self.release,
'',
'Summary: ' + self.distribution.get_description(), ]
# put locale summaries into spec file
for locale in self.summaries.keys():
spec_file.append('Summary(%s): %s' % (locale,
self.summaries[locale]))
spec_file.extend([
'Name: %{name}',
'Version: %{version}',
'Release: %{release}',])
if self.use_bzip2:
spec_file.append('Source0: %{name}-%{version}.tar.bz2')
else:
spec_file.append('Source0: %{name}-%{version}.tar.gz')
spec_file.extend([
'Copyright: ' + self.distribution.get_licence(),
'Group: ' + self.group,
'BuildRoot: %{_tmppath}/%{name}-buildroot',
'Prefix: %{_prefix}', ])
if not self.distribution.has_ext_modules():
spec_file.append('BuildArchitectures: noarch')
if self.vendor:
spec_file.append('Vendor: ' + self.vendor)
if self.packager:
spec_file.append('Packager: ' + self.packager)
if self.distribution.get_url() != 'UNKNOWN':
spec_file.append('Url: ' + self.distribution.get_url())
spec_file.extend([
'',
'%description',
self.distribution.get_long_description()])
# put locale descriptions into spec file
for locale in self.descriptions.keys():
spec_file.extend([
'',
'%description -l ' + locale,
self.descriptions[locale],
])
spec_file.extend([
'',
'%prep',
'%setup',
'',
'%build',
'python setup.py build',
'',
'%install',
'python setup.py install --root=$RPM_BUILD_ROOT --record',
'',
'%clean',
'rm -rf $RPM_BUILD_ROOT',
'rm -f INSTALLED_FILES',
'',
'%files -f INSTALLED_FILES',
'%defattr(-,root,root)',
])
if self.doc:
spec_file.append('%doc ' + self.doc)
if self.changelog:
spec_file.extend([
'',
'%changelog',
self.changelog
])
return spec_file
def _check_locale(self, test_me, name):
''' Tests a wariable to determine if it is {string: string} '''
pass_test = 1 # set to 0 if fails test
if type(test_me) == DictType:
for locale in test_me.keys():
if (type(locale) != StringType) or (type(test_me[locale]) !=
StringType):
pass_test = 0
break
if pass_test:
return test_me
raise DistutilsOptionError, \
("Error in package_data: '%s' must be dictionary: "
'{string: string}' % name)
def _check_string(self, test_me, name):
''' Tests a variable to determino if it is a string '''
if type(test_me) == StringType:
return test_me
else:
raise DistutilsOptionError, \
"Error in package_data: '%s' must be a string" % name
--n8g4imXOkfNTN/H1--