Accepting request 54864 from home:prusnak:branches:openSUSE:Tools

OBS-URL: https://build.opensuse.org/request/show/54864
OBS-URL: https://build.opensuse.org/package/show/openSUSE:Tools/spec-cleaner?expand=0&rev=2
This commit is contained in:
Adrian Schröter 2010-12-07 03:50:54 +00:00 committed by Git OBS Bridge
parent 1403f7b963
commit f187597dc1
3 changed files with 135 additions and 36 deletions

View File

@ -35,6 +35,7 @@
# Authors:
# Vincent Untz <vuntz@novell.com>
# Pavol Rusnak <prusnak@opensuse.org>
# Petr Uzel <petr.uzel@suse.cz>
#
import os
@ -44,6 +45,9 @@ import cStringIO
import optparse
import re
import time
import tempfile
import subprocess
import shlex
#######################################################################
@ -66,6 +70,8 @@ def strip_useless_spaces(s):
def replace_known_dirs(s):
s = s.replace('%_prefix', '%{_prefix}')
s = s.replace('%_usr', '%{_prefix}')
s = s.replace('%{_usr}', '%{_prefix}')
s = s.replace('%_bindir', '%{_bindir}')
s = s.replace('%_sbindir', '%{_sbindir}')
s = s.replace('%_includedir', '%{_includedir}')
@ -78,6 +84,8 @@ def replace_known_dirs(s):
s = s.replace('%{_prefix}/%{_lib}', '%{_libdir}')
s = s.replace('%_sysconfdir', '%{_sysconfdir}')
s = s.replace('%_localstatedir', '%{_localstatedir}')
s = s.replace('%_var', '%{_localstatedir}')
s = s.replace('%{_var}', '%{_localstatedir}')
s = s.replace('%_initddir', '%{_initddir}')
# old typo in rpm macro
s = s.replace('%_initrddir', '%{_initddir}')
@ -97,12 +105,10 @@ def replace_buildroot(s):
s = s.replace('${RPM_BUILD_ROOT}', '%{buildroot}')
s = s.replace('$RPM_BUILD_ROOT', '%{buildroot}')
s = s.replace('%buildroot', '%{buildroot}')
s = s.replace('%{buildroot}/usr', '%{buildroot}%{_prefix}')
s = s.replace('%{buildroot}/', '%{buildroot}')
s = s.replace('%{buildroot}etc/init.d/', '%{buildroot}%{_initddir}/')
s = s.replace('%{buildroot}etc/', '%{buildroot}%{_sysconfdir}/')
s = s.replace('%{buildroot}usr/', '%{buildroot}%{_prefix}/')
s = s.replace('%{buildroot}var/', '%{buildroot}%{_localstatedir}/')
s = s.replace('%{buildroot}/etc/init.d/', '%{buildroot}%{_initddir}/')
s = s.replace('%{buildroot}/etc/', '%{buildroot}%{_sysconfdir}/')
s = s.replace('%{buildroot}/usr/', '%{buildroot}%{_prefix}/')
s = s.replace('%{buildroot}/var/', '%{buildroot}%{_localstatedir}/')
s = s.replace('"%{buildroot}"', '%{buildroot}')
return s
@ -121,11 +127,39 @@ def replace_remove_la(s):
return s
def replace_utils(s):
# take care of all utilities macros that bloat spec file
r = {'id_u': 'id -u', 'ln_s': 'ln -s', 'lzma': 'xz --format-lzma', 'mkdir_p': 'mkdir -p', 'awk':'gawk', 'cc':'gcc', 'cpp':'gcc -E', 'cxx':'g++', 'remsh':'rsh', }
for i in r:
s = s.replace('%__' + i, r[i])
s = s.replace('%{__' + i + '}', r[i])
for i in [ 'aclocal', 'ar', 'as', 'autoconf', 'autoheader', 'automake', 'bzip2', 'cat', 'chgrp', 'chmod', 'chown', 'cp', 'cpio', 'file', 'gpg', 'grep', 'gzip', 'id', 'install', 'ld', 'libtoolize', 'make', 'mkdir', 'mv', 'nm', 'objcopy', 'objdump', 'patch', 'perl', 'python', 'ranlib', 'restorecon', 'rm', 'rsh', 'sed', 'semodule', 'ssh', 'strip', 'tar', 'unzip', 'xz', ]:
s = s.replace('%__' + i, i)
s = s.replace('%{__' + i + '}', i)
return s
def replace_buildservice(s):
for i in ['centos', 'debian', 'fedora', 'mandriva', 'meego', 'rhel', 'sles', 'suse', 'ubuntu']:
s = s.replace('%' + i + '_version', '0%{?' + i + '_version}')
s = s.replace('%{' + i + '_version}', '0%{?' + i + '_version}')
return s
def replace_macros(s):
for i in ['name', 'version', 'release']:
s = s.replace('%' + i, '%{' + i + '}')
return s
def replace_all(s):
s = replace_buildroot(s)
s = replace_optflags(s)
s = replace_known_dirs(s)
s = replace_remove_la(s)
s = replace_utils(s)
s = replace_buildservice(s)
s = replace_macros(s)
return s
@ -172,22 +206,8 @@ class RpmCopyright(RpmSection):
def _add_default_copyright(self):
self.lines.append(time.strftime('''#
# spec file for package
#
# Copyright (c) %Y SUSE LINUX Products GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.
# Please submit bugfixes or comments via http://bugs.opensuse.org/
#
'''))
@ -195,7 +215,8 @@ class RpmCopyright(RpmSection):
if not self.lines and not line:
return
if line == '# norootforbuild':
if line.startswith('# norootforbuild') or \
line.startswith('# usedforbuild'):
return
RpmSection.add(self, line)
@ -386,11 +407,44 @@ class RpmPreamble(RpmSection):
category_to_fixer['license'] = _fix_license
def _pkgname_to_pkgconfig(self, value):
r = {
'cairo-devel': 'cairo',
'dbus-1-devel': 'dbus-1',
'dbus-1-glib-devel': 'dbus-glib-1',
'gconf2-devel': 'gconf-2.0',
'exo-devel': 'exo-1',
'glib2-devel': 'glib-2.0',
'gtk2-devel': 'gtk+-2.0',
'hal-devel': 'hal',
'libexif-devel': 'libexif',
'libgarcon-devel': 'garcon-1',
'libglade2-devel': 'libglade-2.0',
'libgladeui-1_0-devel': 'gladeui-1.0',
'libgudev-1_0-devel': 'gudev-1.0',
'libnotify-devel': 'libnotify',
'libwnck-devel': 'libwnck-1.0',
'libxfce4ui-devel': 'libxfce4ui-1',
'libxfce4util-devel': 'libxfce4util-1.0',
'libxfcegui4-devel': 'libxfcegui4-1.0',
'libxfconf-devel': 'libxfconf-0',
'libxklavier-devel': 'libxklavier',
'libxml2-devel': 'libxml-2.0',
'startup-notification-devel': 'libstartup-notification-1.0',
'xfce4-panel-devel': 'libxfce4panel-1.0',
}
for i in r:
value = value.replace(i, 'pkgconfig('+r[i]+')')
return value
def _fix_list_of_packages(self, value):
if self.re_requires_token.match(value):
tokens = [ item[1] for item in self.re_requires_token.findall(value) ]
for (index, token) in enumerate(tokens):
token = token.replace('%{version}-%{release}', '%{version}')
token = token.replace(' ','')
token = re.sub(r'([<>]=?|=)', r' \1 ', token)
token = self._pkgname_to_pkgconfig(token)
tokens[index] = token
tokens.sort()
@ -485,7 +539,12 @@ class RpmPreamble(RpmSection):
self.current_group.append('# PATCH-MISSING-TAG -- See http://en.opensuse.org/Packaging/Patches')
match = self.re_patch.match(line)
self._add_line_value_to('source', match.group(3), key = '%sPatch%s' % (match.group(1), match.group(2)))
# convert Patch: to Patch0:
if match.group(2) == '':
zero = '0'
else:
zero = ''
self._add_line_value_to('source', match.group(3), key = '%sPatch%s%s' % (match.group(1), zero, match.group(2)))
return
elif self.re_provides.match(line):
@ -578,6 +637,7 @@ class RpmDescription(RpmSection):
class RpmPrep(RpmSection):
'''
Try to simplify to %setup -q when possible.
Replace %patch with %patch0
'''
def add(self, line):
@ -587,6 +647,8 @@ class RpmPrep(RpmSection):
cmp_line = strip_useless_spaces(cmp_line)
if cmp_line == '%setup':
line = '%setup -q'
if line.startswith('%patch '):
line = line.replace('%patch','%patch0')
RpmSection.add(self, line)
@ -601,8 +663,11 @@ class RpmBuild(RpmSection):
def add(self, line):
if not re_comment.match(line):
line = line.replace('%_smp_mflags' , '%{?_smp_mflags}')
line = line.replace('%{_smp_mflags}' , '%{?_smp_mflags}')
line = line.replace('%{?jobs:-j%jobs}' , '%{?_smp_mflags}')
line = line.replace('%{?jobs: -j%jobs}', '%{?_smp_mflags}')
line = line.replace('%{?jobs:-j %jobs}', '%{?_smp_mflags}')
RpmSection.add(self, line)
@ -613,11 +678,10 @@ class RpmBuild(RpmSection):
class RpmInstall(RpmSection):
'''
Remove commands that wipe out the build root.
Use %makeinstall macro.
Use %make_install macro.
Replace %makeinstall (suse-ism).
'''
re_autoreqprov = re.compile('^\s*AutoReqProv:\s*on\s*$', re.IGNORECASE)
def add(self, line):
# remove double spaces when comparing the line
cmp_line = strip_useless_spaces(line)
@ -626,14 +690,13 @@ class RpmInstall(RpmSection):
if cmp_line.find('DESTDIR=%{buildroot}') != -1:
buf = cmp_line.replace('DESTDIR=%{buildroot}', '')
buf = strip_useless_spaces(buf)
if buf == 'make install':
line = '%makeinstall'
if buf == 'make install' or buf == 'make install':
line = '%make_install'
elif cmp_line == '%makeinstall':
line = '%make_install'
elif cmp_line == 'rm -rf %{buildroot}':
return
if self.re_autoreqprov.match(line):
return
RpmSection.add(self, line)
@ -641,6 +704,7 @@ class RpmInstall(RpmSection):
class RpmClean(RpmSection):
# if the section contains just rm -rf %{buildroot} then remove the whole section (including %clean)
pass
@ -752,7 +816,6 @@ class RpmSpecCleaner:
fout = None
current_section = None
re_spec_package = re.compile('^%package\s*', re.IGNORECASE)
re_spec_description = re.compile('^%description\s*', re.IGNORECASE)
re_spec_prep = re.compile('^%prep\s*$', re.IGNORECASE)
@ -777,7 +840,7 @@ class RpmSpecCleaner:
]
def __init__(self, specfile, output, inline, force):
def __init__(self, specfile, output, inline, force, diff, diff_prog):
if not specfile.endswith('.spec'):
raise RpmException('%s does not appear to be a spec file.' % specfile)
@ -787,6 +850,8 @@ class RpmSpecCleaner:
self.specfile = specfile
self.output = output
self.inline = inline
self.diff = diff
self.diff_prog = diff_prog
self.fin = open(self.specfile)
@ -806,6 +871,8 @@ class RpmSpecCleaner:
io.seek(0)
self.fin = io
self.fout = open(self.specfile, 'w')
elif self.diff:
self.fout = tempfile.NamedTemporaryFile(prefix=self.specfile)
else:
self.fout = sys.stdout
@ -843,7 +910,14 @@ class RpmSpecCleaner:
self.current_section.add(line)
self.current_section.output(self.fout)
self.fout.flush()
if self.diff:
cmd = shlex.split(self.diff_prog + " " + self.specfile.replace(" ","\\ ") + " " + self.fout.name.replace(" ","\\ "))
try:
subprocess.call(cmd, shell=False)
except OSError as e:
raise RpmException('Could not execute %s (%s)' % (self.diff_prog.split()[0], e.strerror))
def __del__(self):
if self.fin:
@ -866,6 +940,10 @@ def main(args):
help="output file")
parser.add_option("-f", "--force", action="store_true", dest="force",
default=False, help="overwrite output file if already existing")
parser.add_option("-d", "--diff", action="store_true", dest="diff",
default=False, help="call external program to compare new and original specfile")
parser.add_option("--diff-prog", dest="diff_prog",
help="program to generate diff (implies --diff)")
parser.add_option("-v", "--version", action="store_true", dest="version",
default=False, help="display version (" + VERSION + ")")
@ -876,7 +954,7 @@ def main(args):
return 0
if len(args) != 1:
print >> sys.stderr, '\nUsage:\n\tspec-cleaner file.spec\n'
parser.print_help()
return 1
spec = os.path.expanduser(args[0])
@ -887,12 +965,27 @@ def main(args):
options.output = ''
options.inline = True
if options.diff_prog:
# --diff-prog implies -d
options.diff = True
else:
# if diff-prog is not specified, set default here
options.diff_prog = "vimdiff"
if options.output and options.inline:
print >> sys.stderr, 'Conflicting options: --inline and --output.'
return 1
if options.diff and options.output:
print >> sys.stderr, 'Conflicting options: --diff and --output.'
return 1
if options.diff and options.inline:
print >> sys.stderr, 'Conflicting options: --diff and --inline.'
return 1
try:
cleaner = RpmSpecCleaner(spec, options.output, options.inline, options.force)
cleaner = RpmSpecCleaner(spec, options.output, options.inline, options.force, options.diff, options.diff_prog)
cleaner.run()
except RpmException, e:
print >> sys.stderr, '%s' % e

View File

@ -1,3 +1,9 @@
-------------------------------------------------------------------
Mon Dec 6 11:41:07 UTC 2010 - prusnak@opensuse.org
- update to newer snapshot (1fb8c6db)
- --diff and --diff-prog options by Petr Uzel
-------------------------------------------------------------------
Thu Sep 30 16:04:03 CEST 2010 - vuntz@opensuse.org

View File

@ -37,10 +37,10 @@ will never be perfect.
%build
%install
install -D -m0755 %{S:0} %{buildroot}%{_bindir}/spec-cleaner
install -D -m 0755 %{SOURCE0} %{buildroot}%{_bindir}/spec-cleaner
%clean
%{__rm} -rf %{buildroot}
rm -rf %{buildroot}
%files
%defattr(-, root, root)