forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_source.pl
301 lines (263 loc) · 8.67 KB
/
check_source.pl
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
#! /usr/bin/perl
use File::Basename;
use File::Temp qw/ tempdir /;
use XML::Simple;
use Data::Dumper;
use Cwd;
use Text::Diff;
BEGIN {
unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
}
use Build;
my $ret = 0;
my $old = $ARGV[0];
my $dir = $ARGV[1];
my $bname = basename($dir);
if (-f "$dir/_service") {
my $service = XMLin("$dir/_service", ForceArray => ['service']);
while( my ($name, $s) = each %{$service->{service}} ) {
my $mode = $s->{mode} || '';
next if ($mode eq "localonly" || $mode eq "disabled" || $mode eq "buildtime" || $mode eq "manual" );
print "Services are only allowed if they are mode='localonly', 'disabled', 'manual' or 'buildtime'. Please change the mode of $name and use `osc service localrun/disabledrun`.\n";
$ret = 1;
}
# move it away to have full service from source validator
rename("$dir/_service", "$dir/_service.bak") || die "rename failed";
}
for my $file (glob("$dir/_service:*")) {
$file=basename($file);
print "Found _service generated file $file in checkout. Please clean this up first.";
$ret = 1;
}
my @specs = map basename($_), glob("$dir/*.spec");
if (@specs) {
if (!-f "$dir/$bname.changes") {
print "$bname.changes is missing. A package submitted as FooBar needs to have a FooBar.changes file with a format created by `osc vc`.\n";
$ret = 1;
}
if (!-f "$dir/$bname.spec") {
print "$bname.spec is missing. A package submitted as FooBar needs to have a FooBar.spec file.\n";
$ret = 1;
}
exit($ret) if ($ret);
} else {
# package without spec files, eg kiwi only
exit($ret);
}
open(SPEC, "$dir/$bname.spec");
my $spec = join("", <SPEC>);
close(SPEC);
if ($spec !~ m/#[*\s]+Copyright\s/) {
print "$bname.spec does not appear to contain a Copyright comment. Please stick to the format\n\n";
print "# Copyright (c) 2011 Stephan Kulow\n\n";
print "or use osc service runall format_spec_file\n";
$ret = 1;
}
if ($spec =~ m/\nVendor:/) {
print "$bname.spec contains a Vendor line, this is forbidden.\n";
$ret = 1;
}
# Check that we have for each spec file a changes file - and that at least one
# contains changes
my $changes_updated = 0;
for my $spec (@specs) {
$changes = $spec;
$changes =~ s/\.spec$/.changes/;
# new or deleted .changes files also count
if ((-f "$old/$changes") != (-f "$dir/$changes")) {
$changes_updated = 1;
last;
}
elsif ((-f "$old/$changes") && (-f "$dir/$changes")) {
if (system(("cmp", "-s", "$old/$changes", "$dir/$changes"))) {
$changes_updated = 1;
last;
}
}
}
if (!$changes_updated) {
print "No changelog. Please use 'osc vc' to update the changes file(s).\n";
$ret = 1;
}
if ($spec !~ m/\n%changelog\s/ && $spec != m/\n%changelog$/) {
print "$bname.spec does not contain a %changelog line. We don't want a changelog in the spec file, but the %changelog section needs to be present\n";
$ret = 1;
}
if ($spec !~ m/(#[^\n]*license)/i) {
print "$bname.spec does not appear to have a license. The file needs to contain a free software license\n";
print "Suggestion: use \"osc service runall format_spec_file\" to get our default license or\n";
print "the minimal license:\n\n";
print "# This file is under MIT license\n";
$ret = 1;
}
my %patches = ();
for my $test (glob("/usr/lib/obs/service/source_validators/*")) {
next if (!-f "$test");
my $checkivsd = `/bin/bash $test --batchmode $dir $old < /dev/null 2>&1`;
if ($?) {
print "Source validator failed. Try \"osc service runall source_validator\"\n";
print $checkivsd;
print "\n";
$ret = 1;
}
else {
for my $line (split(/\n/, $checkivsd)) {
# pimp up some warnings
if ($line =~ m/Attention.*not mentioned/) {
$line =~ s,\(W\) ,,;
print "$line\n";
$ret = 1;
}
}
}
}
my $odir = getcwd();
chdir($dir) || "chdir $dir failed";
for my $patch (glob("*.diff *.patch *.dif")) {
$patches{$patch} = 'current';
}
chdir($odir) || die "chdir $odir failed";
if (-d "$old") {
chdir($old) || die "chdir $old failed";
my $cf = Build::read_config("x86_64", "/usr/lib/build/configs/default.conf");
my %thash = ();
my %rhash = ();
for my $spec (glob("*.spec")) {
my $ps = Build::Rpm::parse($cf, $spec);
while (my ($k, $v) = each %$ps) {
if ($k =~ m/^source/) {
$thash{$v} = 1;
}
}
}
for my $patch (glob("*.diff *.patch *.dif")) {
if ($patches{$patch}) {
delete $patches{$patch};
}
else {
$patches{$patch} = 'old';
}
}
chdir($odir) || die "chdir $odir failed";
chdir($dir) || die "chdir $dir failed";
for my $spec (glob("*.spec")) {
my $ps = Build::Rpm::parse($cf, $spec);
open(OSPEC, "$spec");
open(NSPEC, ">$spec.new");
while (<OSPEC>) {
chomp;
if (m/^Source/) {
my $line = $_;
$line =~ s/^(Source[0-9]*)\s*:\s*//;
if ($patches{$line}) {
delete $patches{$line};
}
my $prefix = $1;
my $parsedline = $ps->{lc $prefix};
if (defined $thash{$parsedline}) {
my $file = $line;
my $bname = basename($file);
print NSPEC "$prefix: $bname\n";
}
else {
print NSPEC "$_\n";
}
}
else {
print NSPEC "$_\n";
}
}
close(OSPEC);
close(NSPEC);
system(("cp", "$spec", "$spec.beforeurlstrip"));
rename("$spec.new", "$spec") || die "rename failed";
}
chdir($dir);
my @changes = glob("*.changes");
chdir($odir);
if (%patches) {
# parse changes
for my $changes (@changes) {
my $diff = "";
if (! -e "$old/$changes") {
$diff = diff "/dev/null", "$dir/$changes";
}
else {
$diff = diff "$old/$changes", "$dir/$changes";
}
for my $line (split(/\n/, $diff)) {
next unless $line =~ m/^+/;
$line =~ s/^\+//;
for my $patch (keys %patches) {
if (index($line, $patch) != -1) {
delete $patches{$patch};
}
}
}
}
}
# still some left?
if (%patches) {
$ret = 1;
for my $patch (keys %patches) {
# wording stolen from Raymond's declines :)
if ($patches{$patch} eq 'current') {
print "A patch ($patch) is being added without this addition being mentioned in the changelog.\n";
}
else {
print "A patch ($patch) is being deleted without this removal being mentioned in the changelog.\n";
}
}
}
}
my $odir = getcwd;
my $tmpdir = tempdir("obs-XXXXXXX", TMPDIR => 1, CLEANUP => 1);
chdir($dir) || die 'tempdir failed';
if (system("/usr/lib/obs/service/download_files","--enforceupstream", "yes", "--enforcelocal", "yes", "--outdir", $tmpdir)) {
print "Source URLs are not valid. Try \"osc service runall download_files\".\n";
$ret = 2;
}
chdir($odir);
for my $rpmlint (glob("$dir/*rpmlintrc")) {
open(RPMLINTRC, $rpmlint);
while (<RPMLINTRC>) {
if (m/^\s*setBadness/) {
print "For Factory submissions, you cannot use setBadness. Use filters in $rpmlint.\n";
$ret = 1;
}
}
}
exit($ret) if $ret;
# now check if the change is small enough to warrent a review-by-mail
exit(0) unless -d $old;
sub prepare_package($) {
my $files = shift;
unlink glob "*.changes"; # ignore changes
unlink glob "*.tar.*"; # we can't diff them anyway
unlink glob "*.zip";
# restore original spec file
for my $spec (glob("*.beforeurlstrip")) {
my $oldname = $spec;
$oldname =~ s/.beforeurlstrip//;
rename($spec, $oldname);
}
for my $spec (glob("*.spec")) {
open(SPEC, "/usr/lib/obs/service/format_spec_file.files/prepare_spec $spec | grep -v '^#' |");
my @lines = <SPEC>;
close(SPEC);
open(SPEC, ">", $spec);
print SPEC join('', @lines);
close(SPEC);
}
}
# move it back so we also diff the service file
if (-f "$dir/_service.bak") {
rename("$dir/_service.bak", "$dir/_service") || die "rename failed";
}
my %files;
chdir($old);
prepare_package(\%files);
chdir($odir);
chdir($dir);
prepare_package(\%files);
exit(0);