-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspec-cleaner
executable file
·328 lines (290 loc) · 5.99 KB
/
spec-cleaner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/perl
#
# A script to clean up spec files and bring them closer to ROSA packaging policies
#
# Author: Denis Silakov, ROSA, [email protected]
#
$spec = $ARGV[0];
$newspec = $ARGV[1];
use Env qw(SPEC_LEAVE_CHANGELOG);
$pkgname = $spec;
$pkgname =~ s/\.spec$//;
if (!$newspec) {
$newspec = $spec.".tmp";
}
if (@ARGV < 1) {
die "Usage: $0 OLD_SPEC [NEW_SPEC]";
}
$changelog = 0;
# List of macros known to be variables, so they should be embraced with '{}'
my @vars = (
'name',
'version',
'release',
'eposh',
'EVRD',
'py_ver',
'buildroot',
'optflags',
'ldflags',
'build_ldflags',
'_bindir',
'_libdir',
'rlibdir',
'_sbindir',
'_mandir',
'_datadir',
'_sysconfdir',
'_webappconfdir',
'_webconfdir',
'_jvmcommonsysconfdir',
'_jvmsysconfdir',
'_mavendepmapfragdir',
'_mavendepmapdir',
'_initddir',
'_sys_macros_dir',
'_systemdconfdir',
'_systemdrootdir',
'_systemgeneratordir',
'_systemshutdowndir',
'_systemunitdir',
'_unitdir',
'_userunitdir',
'_sharedstatedir',
'_desktopdir',
'_gamesbindir',
'_includedir',
'firefox_pluginsdir',
'_jnidir',
'_jvmcommonlibdir',
'_jvmjardir',
'_jvmdir',
'_jvmprivdir',
'_kde_plugindir',
'_menudir',
'_qt_bindir',
'_qt_demodir',
'_qt_exampledir',
'_qt_importdir',
'qt4include',
'_qt_includedir',
'qt4plugins',
'_qt_plugindir',
'qt4dir',
'_qt_translationdir',
'_systemdlibexecdir',
'tcl_sitearch',
'_usergeneratordir',
'sampler_libdir',
'_kde_applicationsdir',
'_kde_appsdir',
'_kde_configdir',
'_qt_docdir',
'_lispdir',
'_xfontdir',
'_gamesdatadir',
'_icons16dir',
'_icons192dir',
'_icons22dir',
'_icons48dir',
'_icons64dir',
'_icons96dir',
'_iconsbasedir',
'_iconsscaldir',
'_iconsdir',
'_liconsdir',
'_miconsdir',
'_infodir',
'_javadocdir',
'_javadir',
'_jvmcommondatadir',
'_jvmdatadir',
'_localedir',
'_mavenpomdir',
'php_pear_dir',
'_rpm_helper_dir',
'_spec_helper_dir',
'_systemddatadir',
'_userunitdir',
'_prefix',
'tcl_sitelib',
'_filetriggers_dir',
'_localstatedir',
'_logdir',
'_repackage_dir',
'ruby_gemdir',
'ruby_ridir',
'ruby_libdir',
'ruby_sitelibdir',
'ruby_vendorlibdir',
'ruby_sitedir',
'ruby_vendordir',
'py3_platsitedir',
'py3_platlibdir',
'py3_incdir',
'py_incdir',
'py_platsitedir',
'py_platlibdir',
'py_dyndir',
'py_puresitedir',
'py_purelibdir',
'py3_puresitedir',
'py3_purelibdir',
'perl_archlib',
'perl_privlib',
'perl_sitearch',
'perl_sitelib',
'perl_vendorarch',
'perl_vendorlib',
'perl_version',
);
my @macros = (
'make',
'make_build',
'make_install',
'makeinstall_std',
'configure',
'configure2_5x',
);
my @macros_to_unroll = (
'__mkdir_p',
'__mkdir',
'__ln_s',
'__ln',
'__mv',
'__perl',
'__python',
'__python3',
'__rm',
'__rmdir',
'__ruby',
'__cp',
'__grep',
'__cat',
'__chmod',
'__chown',
'__install',
);
# Indicates that we are inside post- or pre- script
$in_script=0;
open(F, $spec);
open(G, "> $newspec");
while(<F>) {
if( /^BuildRoot:/i or /^Packager:/i ) {
next;
}
# Obsolete
if( /^(Build)?Requires(\(.*\))?:\s*info-install\s*$/i ) {
next;
}
$line = $_;
# Captitalize summary and drop dot from its end, if any
if( $line =~ /^(%define\s*)?Summary:\s*(\S+)/i ) {
$word = $2;
$word_old = $word;
$word =~ s/(^[a-z])/\u$1/;
if( $word_old !~ /^$pkgname/ ) {
$line =~ s/Summary:(\s*)$word_old/Summary:$1$word/;
} else {
print STDERR "Won't capitalize summary - it starts with project name\n";
}
chomp $line;
if( $line =~ /\.\s*$/ ) {
$line =~ s/\.\s*$//;
}
print G $line."\n";
next;
}
if( /^%changelog/i ) {
$changelog = 1;
# By default, we drop changelogs from specs
# nowadays changelogs are generated from Git
# and most spec files contains only ancient changelog entries
if( !$SPEC_LEAVE_CHANGELOG ) {
last;
}
}
if( $changelog or /\s*#/) {
print G $line;
next;
}
if( $line =~ /^\%defattr\(-,root,root(,-)?\)\s*$/ ) {
next;
}
$line =~ s/\$\{?RPM_BUILD_ROOT\}?/\%\{buildroot\}/g;
$line =~ s/\$\{?RPM_OPT_FLAGS\}?/\%\{optflags\}/g;
if( $line =~/^\s*(\%\{?__rm\}?|rm) -rf \%\{?buildroot\}?\s*$/ ) {
next;
}
$line =~ s/\%mkrel\s+//;
$line =~ s/\%\{\?dist\}//;
$line =~ s/make \%\{\?_smp_mflags\}/\%make_build/;
$line =~ s/\%make test/\make test/;
# Cleanup old grep usage
$line =~ s/fgrep/grep -F/g;
$line =~ s/egrep/grep -E/g;
# Clean up obsolete macro usage
$line =~ s/({|%)webappconfdir/$1_webappconfdir/g;
$line =~ s/({|%)py_libdir/$1py_purelibdir/g;
$line =~ s/({|%)py_sitedir/$1py_puresitedir/g;
$line =~ s/({|%)python_sitearch/$1py_platsitedir/g;
$line =~ s/({|%)python_sitelib/$1py_puresitedir/g;
$line =~ s/({|%)python3_sitearch/$1py3_platsitedir/g;
$line =~ s/({|%)python3_sitelib/$1py3_puresitedir/g;
$line =~ s/({|%)python_version/$1py_ver/g;
$line =~ s/({|%)pyver/$1py_ver/g;
$line =~ s/\%py_requires -d/BuildRequires: pkgconfig\(python\)/;
foreach $var (@vars) {
$line =~ s/\%$var/\%\{$var\}/g;
}
foreach $macro (@macros) {
$line =~ s/\%\{$macro\}/\%$macro/g;
$line =~ s/\%\{__$macro\}/\%$macro/g;
}
foreach $macro (@macros_to_unroll) {
my $repl = $macro;
$repl =~ s/^__//;
$repl =~ s/_/ -/;
$line =~ s/\%\{?$macro\}?/$repl/g;
}
foreach my $cmd ('mv', 'cp', 'python', 'rm') {
$line =~ s/\%\{_?_?$cmd\}/$cmd/g;
}
# Drop definitions of %name, %version and %release
if( /\%define\s+name\s+(.+)$/ ) {
$name = $1;
next;
}
if( /\%define\s+version\s+(.+)$/ ) {
$version = $1;
next;
}
if( /\%define\s+release\s+(.+)$/ ) {
$release = $1;
next;
}
# If Name, Version or Release are defined using %name, %version and %relese macros
# replace macros with their values
# (manual definition of these macros is obsolete and not needed, they are get defined
# automatically on the basis of Name, Version and Release tags)
if( /^Name:(\s+)\%\{?name\}?$/ ) {
print G "Name:$1$name\n";
next;
}
if( /^Version:(\s+)\%\{?version\}?$/ ) {
print G "Version:$1$version\n";
next;
}
if( /^Release:(\s+)\%\{?release\}?$/ ) {
print G "Release:$1$release\n";
next;
}
print G $line;
}
# newspec was not specified in command line,
# so we are using temoporary file; now let's move it
# to original one
if (!$ARGV[1]) {
system "cat $newspec > $spec";
system "rm -f $newspec";
}