| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
# SPDX-FileCopyrightText: 2010-2011 Jan Engelhardt |
| 4 |
# |
| 5 |
# spec-beautifier - make .spec files shine again |
| 6 |
# |
| 7 |
use Getopt::Long; |
| 8 |
use strict; |
| 9 |
my $opt_diff_mode; |
| 10 |
my $opt_replace_inline; |
| 11 |
|
| 12 |
our $PSTOP = qr<(?=[\s/]|$)>; |
| 13 |
our $IDNAME = qr<[A-Za-z_][A-Za-z_0-9]*>; |
| 14 |
our $VSTOP = qr<(?![A-Za-z_0-9])>; |
| 15 |
our $VRSTOP = qr<(?![A-Za-z_0-9\*])>; |
| 16 |
|
| 17 |
sub help |
| 18 |
{ |
| 19 |
print "Usage: $ARGV[0] [-d|-r]\n"; |
| 20 |
print " -d Generate diff-like output\n"; |
| 21 |
print " -r Replace file in line rather than printing to stdout\n"; |
| 22 |
exit(0); |
| 23 |
} |
| 24 |
|
| 25 |
sub process_generic |
| 26 |
{ |
| 27 |
my($s) = shift @_; |
| 28 |
|
| 29 |
if ($s->{sectype} eq "shell") { |
| 30 |
# Undo crimes on shell vars |
| 31 |
s<\$\{($IDNAME)\}$VSTOP> <\$$1>g; |
| 32 |
|
| 33 |
# Shell variables to RPM macros |
| 34 |
#s<\$RPM_SOURCE_DIR> <\%_sourcedir>g; |
| 35 |
#s<\$RPM_BUILD_DIR> <\%_builddir>g; |
| 36 |
s<\$RPM_OPT_FLAGS$VSTOP> <\%optflags>g; |
| 37 |
s<\$RPM_ARCH$VSTOP> <\%_arch>g; |
| 38 |
s<\$RPM_OS$VSTOP> <\%_os>g; |
| 39 |
s<\$RPM_BUILD_ROOT$VSTOP> <\%buildroot>g; |
| 40 |
s<\$RPM_DOC_DIR$VSTOP> <\%_docdir>g; |
| 41 |
s<\$RPM_PACKAGE_NAME$VSTOP> <\%name>g; |
| 42 |
s<\$RPM_PACKAGE_VERSION$VSTOP> <\%version>g; |
| 43 |
s<\$RPM_PACKAGE_RELEASE$VSTOP> <\%release>g; |
| 44 |
} |
| 45 |
|
| 46 |
# Hardcoded paths to macros |
| 47 |
s</etc/init.d$PSTOP> <\%_initddir>g; |
| 48 |
s</etc$PSTOP> <\%_sysconfdir>g; |
| 49 |
s</usr/bin$PSTOP> <\%_bindir>g; |
| 50 |
s</usr/sbin$PSTOP> <\%_sbindir>g; |
| 51 |
s</usr/libexec$PSTOP> <\%_libexecdir>g; |
| 52 |
s</usr/include$PSTOP> <\%_includedir>g; |
| 53 |
s</usr/share/man$PSTOP> <\%_mandir>g; |
| 54 |
s</usr/share/info$PSTOP> <\%_infodir>g; |
| 55 |
s</usr/share$PSTOP> <\%_datadir>g; |
| 56 |
if (m</usr/lib$PSTOP>) { |
| 57 |
print STDERR "$s->{pos}: Seen \"/usr/lib\". Consider replacing with \%_libdir or \%_libexecdir as appropriate.\n"; |
| 58 |
} |
| 59 |
if (m</usr/lib64$PSTOP>) { |
| 60 |
print STDERR "$s->{pos}: Seen \"/usr/lib64\". Consider fixing it.\n"; |
| 61 |
} |
| 62 |
if (m{(?:^|\s)/lib$PSTOP}) { |
| 63 |
print STDERR "$s->{pos}: Seen \"/lib\". Consider replacing with \%_lib as appropriate.\n"; |
| 64 |
} |
| 65 |
if (m{(?:^|\s)/lib64$PSTOP}) { |
| 66 |
print STDERR "$s->{pos}: Seen \"/lib64\". Consider fixing it.\n"; |
| 67 |
} |
| 68 |
|
| 69 |
# Temporarily add {} to reduce following regexes |
| 70 |
s<\%($IDNAME)> <\%{$1}>g; |
| 71 |
|
| 72 |
# Upgrade macros to newer forms |
| 73 |
if (m<\%\{_initrddir\}>g) { |
| 74 |
print STDERR "$s->{pos}: Try not to use \%_initrddir (this was a typo introduced in rpm) anymore, use \%_initddir. NOTE: SLES11 does not know the new variable yet.\n"; |
| 75 |
} |
| 76 |
s<\%\{_usr\}> <\%{_prefix}>g; |
| 77 |
s<\%\{_var\}> <\%{_localstatedir}>g; |
| 78 |
s<\%\{_prefix\}/+\%\{_lib\}> <\%{_libdir}>g; |
| 79 |
|
| 80 |
# Replace macros by normal forms |
| 81 |
my %util = ("id_u" => "id -u", "ln_s" => "ln -s", |
| 82 |
"lzma" => "xz --format-lzma", "mkdir_p" => "mkdir -p", |
| 83 |
"awk" => "gawk", "cc" => "gcc", "cpp" => "gcc -E", |
| 84 |
"cxx" => "g++", "remsh" => "rsh"); |
| 85 |
foreach my $re (keys %util) { |
| 86 |
s<\%\{__$re\}><$util{$re}>g; |
| 87 |
} |
| 88 |
|
| 89 |
foreach my $re (qw(aclocal ar as autoconf autoheader automake bzip2 |
| 90 |
cat chgrp chmod chown cp cpio file gpg grep gzip id install ld |
| 91 |
libtoolize make mkdir mv nm objcopy objdump patch perl python |
| 92 |
ranlib restorecon rm rsh sed semodule ssh strip tar unzip xz)) |
| 93 |
{ |
| 94 |
s<\%\{__$re\}><$re>g; |
| 95 |
} |
| 96 |
|
| 97 |
# Removal of .la files |
| 98 |
foreach my $re ( |
| 99 |
qr<find \%buildroot -type f -name "\*\.la" -exec rm -f?vf? \{\} \+$>, |
| 100 |
qr<find \%buildroot -type f -name '\*\.la' -exec rm -f?vf? \{\} \+$>, |
| 101 |
qr<find \%buildroot -type f -name "\*\.la" -print0 \| xargs -0 rm -f?vf?$>, |
| 102 |
qr<find \%buildroot -type f -name '\*\.la' -print0 \| xargs -0 rm -f?vf?$>) |
| 103 |
{ |
| 104 |
my $tmp = $_; |
| 105 |
$tmp =~ s<\s+>< >gs; |
| 106 |
s<$re><find \%buildroot -type f -name "*.la" -delete -print>; |
| 107 |
} |
| 108 |
foreach (qr<find \%buildroot -type f -name "\*\.la" -exec rm -f \{\} \+$>, |
| 109 |
qr<find \%buildroot -type f -name '\*\.la' -exec rm -f \{\} \+$>, |
| 110 |
qr<find \%buildroot -type f -name "\*\.la" -print0 \| xargs -0 rm -fv$>, |
| 111 |
qr<find \%buildroot -type f -name '\*\.la' -print0 \| xargs -0 rm -fv$>) |
| 112 |
{ |
| 113 |
s<$_><find \%buildroot -type f -name "*.la" -delete -print>; |
| 114 |
} |
| 115 |
|
| 116 |
# Guard BS variables |
| 117 |
foreach my $re (qw(centos debian fedora mandrive meego rhel |
| 118 |
sles suse ubuntu)) |
| 119 |
{ |
| 120 |
s<\%\{${re}_version\}><0\%\{?${re}_version}>g; |
| 121 |
} |
| 122 |
|
| 123 |
# Undo crimes on RPM macros |
| 124 |
s<\%\{($IDNAME)\}$VRSTOP> <\%$1>g; |
| 125 |
|
| 126 |
# More Build System freedom |
| 127 |
s<\%\{\?jobs\s*:\s*-j\s*\%jobs\}><\%{?_smp_mflags}>g; |
| 128 |
} |
| 129 |
|
| 130 |
sub process_package |
| 131 |
{ |
| 132 |
s<^\s*(\S+)\s*:> <$1:>g; |
| 133 |
s<^([A-Za-z])(\S+):> <uc($1).lc($2).":">eg; |
| 134 |
s<^Prereq:> <PreReq:>g; |
| 135 |
s<^Buildrequires:> <BuildRequires:>g; |
| 136 |
s<^Buildarch:> <BuildArch:>g; |
| 137 |
s<^Url:> <URL:>g; |
| 138 |
|
| 139 |
if (m<^Buildroot:(\s*).*>) { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
if (s<^Packager:.*> <>i) { |
| 143 |
print STDERR "You should not use the Packager tag. These are set by the Build Service.\n"; |
| 144 |
return 0; |
| 145 |
} |
| 146 |
if (s<^Distribution:.*> <>i) { |
| 147 |
print STDERR "You should not use the Distribution tag. These are set by the Build Service/prjconf.\n"; |
| 148 |
return 0; |
| 149 |
} |
| 150 |
if (s<^Vendor:.*> <>i) { |
| 151 |
print STDERR "You should not use the Vendor tag. These are set by the Build Service/prjconf.\n"; |
| 152 |
return 0; |
| 153 |
} |
| 154 |
if (m<^\s*\%define\s*(name|version|release)\b.*>i) { |
| 155 |
print STDERR "Defining \%$1 is pointless. Just use the actual \"$1:\" tag.\n"; |
| 156 |
} |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
sub process_master |
| 161 |
{ |
| 162 |
local *FH = shift @_; |
| 163 |
my $s = shift @_; |
| 164 |
my $repl = []; |
| 165 |
my $old; |
| 166 |
|
| 167 |
$s->{line} = 0; |
| 168 |
$s->{sectype} = "package"; |
| 169 |
while (<FH>) { |
| 170 |
++$s->{line}; |
| 171 |
$s->{pos} = $s->{file}.":".$s->{line}; |
| 172 |
$old = $_; |
| 173 |
if (m<^\%(package|description|files)\b>) { |
| 174 |
$s->{sectype} = $s->{section} = $1; |
| 175 |
} elsif (m<\%(prep|build|install|check|clean|pre|post|preun|postun)>) { |
| 176 |
$s->{sectype} = "shell"; |
| 177 |
$s->{section} = $1; |
| 178 |
} |
| 179 |
&process_generic($s); |
| 180 |
if ($s->{sectype} eq "package") { |
| 181 |
if (!&process_package()) { |
| 182 |
next; |
| 183 |
} |
| 184 |
} |
| 185 |
if ($opt_diff_mode) { |
| 186 |
print (($old ne $_) ? "-$old+$_" : " $_"); |
| 187 |
} elsif ($opt_replace_inline) { |
| 188 |
push(@$repl, $_); |
| 189 |
} else { |
| 190 |
print; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $repl; |
| 195 |
} |
| 196 |
|
| 197 |
sub process_file |
| 198 |
{ |
| 199 |
my $file = shift @_; |
| 200 |
my $state = {"file" => $file}; |
| 201 |
local *FH; |
| 202 |
|
| 203 |
if (!open(FH, "< $file")) { |
| 204 |
print STDERR "Error opening for reading $file: $!\n"; |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
my $repl = &process_master(\*FH, $state); |
| 209 |
close FH; |
| 210 |
|
| 211 |
if (!$opt_diff_mode && $opt_replace_inline) { |
| 212 |
if (!open(FH, "> $file")) { |
| 213 |
print STDERR "Error opening $file for write: $!\n"; |
| 214 |
return; |
| 215 |
} |
| 216 |
print FH @$repl; |
| 217 |
close FH; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
sub main |
| 222 |
{ |
| 223 |
&Getopt::Long::Configure(qw(bundling)); |
| 224 |
&GetOptions( |
| 225 |
"d" => \$opt_diff_mode, |
| 226 |
"r" => \$opt_replace_inline, |
| 227 |
"h" => sub { &help(); }, |
| 228 |
); |
| 229 |
if (scalar(@ARGV) == 0) { |
| 230 |
@ARGV = ("-"); |
| 231 |
} |
| 232 |
foreach (@ARGV) { |
| 233 |
&process_file($_); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
&main(); |