'Generate the text of an RPM spec file and return it as a
list of strings (one per line).'

def _make_spec_file(self):

    spec_file = [('%define    name    ' + self.distribution.get_name()), ('%define    version    ' + self.distribution.get_version().replace('-', '_')), ('%define    unmangled_version    ' + self.distribution.get_version()), ('%define    release    ' + self.release.replace('-', '_')), '', ('Summary:    ' + self.distribution.get_description())]
    spec_file.extend(['Name:    %{name}', 'Version:    %{version}', 'Release:    %{release}'])
    if self.use_bzip2:
        spec_file.append('Source0:    %{name}-%{unmangled_version}.tar.bz2')
    else:
        spec_file.append('Source0:    %{name}-%{unmangled_version}.tar.gz')
    spec_file.extend([('License:    ' + self.distribution.get_license()), ('Group:    ' + self.group), 'BuildRoot:    %{_tmppath}/%{name}-%{version}-%{release}-buildroot', 'Prefix:    %{_prefix}'])
    if (not self.force_arch):
        if (not self.distribution.has_ext_modules()):
            spec_file.append('BuildArch:    noarch')
    else:
        spec_file.append(('BuildArch:    %s' % self.force_arch))
    for field in ('Vendor', 'Packager', 'Provides', 'Requires', 'Conflicts', 'Obsoletes'):
        val = getattr(self, string.lower(field))
        if isinstance(val, list):
            spec_file.append(('%s:    %s' % (field, string.join(val))))
        elif (val is not None):
            spec_file.append(('%s:    %s' % (field, val)))
    if (self.distribution.get_url() != 'UNKNOWN'):
        spec_file.append(('Url:    ' + self.distribution.get_url()))
    if self.distribution_name:
        spec_file.append(('Distribution:    ' + self.distribution_name))
    if self.build_requires:
        spec_file.append(('BuildRequires:    ' + string.join(self.build_requires)))
    if self.icon:
        spec_file.append(('Icon:    ' + os.path.basename(self.icon)))
    if self.no_autoreq:
        spec_file.append('AutoReq:    0')
    spec_file.extend(['', '%description', self.distribution.get_long_description()])
    def_setup_call = ('%s    %s' % (self.python, os.path.basename(sys.argv[0])))
    def_build = ('%s    build' % def_setup_call)
    if self.use_rpm_opt_flags:
        def_build = ('env    CFLAGS="$RPM_OPT_FLAGS"    ' + def_build)
    install_cmd = ('%s    install    -O1    --root=$RPM_BUILD_ROOT    --record=INSTALLED_FILES' % def_setup_call)
    script_options = [('prep', 'prep_script', '%setup    -n    %{name}-%{unmangled_version}'), ('build', 'build_script', def_build), ('install', 'install_script', install_cmd), ('clean', 'clean_script', 'rm    -rf    $RPM_BUILD_ROOT'), ('verifyscript', 'verify_script', None), ('pre', 'pre_install', None), ('post', 'post_install', None), ('preun', 'pre_uninstall', None), ('postun', 'post_uninstall', None)]
    for (rpm_opt, attr, default) in script_options:
        val = getattr(self, attr)
        if (val or default):
            spec_file.extend(['', ('%' + rpm_opt)])
            if val:
                spec_file.extend(string.split(open(val, 'r').read(), '\n'))
            else:
                spec_file.append(default)
    spec_file.extend(['', '%files    -f    INSTALLED_FILES', '%defattr(-,root,root)'])
    if self.doc_files:
        spec_file.append(('%doc    ' + string.join(self.doc_files)))
    if self.changelog:
        spec_file.extend(['', '%changelog'])
        spec_file.extend(self.changelog)
    return spec_file
