[Distutils] Latest bdist_rpm

Harry Henry Gebel hgebel@inet.net
Fri, 5 May 2000 02:21:55 -0400


--JYK4vJDZwFMowpUq
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I have added type checking to bdist_rpm, eliminated the ability to
override meta-data supplied in './setup.py', and changed the function
names to the standard Distutils style. Tomorrow I hope to be building with
`rpm -ba` instead of `rpm -ta` and I have some options I will add. The
patch to existing files and the 'package_data' file have not changed.

-- 
Harry Henry Gebel, Senior Developer, Landon House SBS
West Dover Hundred, Delaware
PyNcurses ncurses binding for Python http://pyncurses.sourceforge.net
--JYK4vJDZwFMowpUq
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, remove_tree
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"),
        ('tar-only', None,
         "Only generate spec file and tarball"),
        ('no-remove', None,
         "Do not remove root owned files"),
        ('arch=', None,
         "Build for a specific architecture"),
        ]


    def initialize_options (self):
        self.spec_only = None
        self.tar_only = None
        self.no_remove = None
        self.arch = 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.spec_only and self.tar_only:
            raise DistutilsOptionsError, \
                  'Cannot specify both spec-only and tar-only'
        if os.getuid(): # if not root
            if self.no_remove:
                self.warn('no-remove has no effect when not root')
            self.remove = None
        else: # if root
            if self.no_remove:
                self.remove = None
            else:
                self.remove = 1

    # finalize_options()


    def run (self):
        self._get_package_data() # get packaging info
        

        # place spec file in ./redhat directory
        self.execute(mkpath, ('./redhat',), "Created ./redhat directory")
        spec_path = './redhat/%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 .gz distribution
        sdist = self.find_peer ('sdist')
        sdist.set_option ('formats', ['gztar'])
        self.run_peer('sdist')

        if self.tar_only: # stop if requested
            return
        # build package
        self.announce('Building RPMs')
        rpm_args = ['rpm', '-ta', '--clean']
        if self.arch and self.distribution.has_ext_modules():
            rpm_args.append['--target=' + arch]
        rpm_args.append(self.distribution.get_fullname() + '.tar.gz')
        self.spawn(rpm_args)

        if self.remove: # remove generated files
            self.execute(self._remove_root, (), 'Removing generated files')

    # run()


    def _remove_root(self):
        ''' Remove files generated to support rpm '''
        os.unlink('MANIFEST')
        os.unlink(self.distribution.get_fullname() + '.tar.gz')
        remove_tree('redhat')


    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}',
            'Source0: %{name}-%{version}.tar.gz',
            '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

--JYK4vJDZwFMowpUq--