summaryrefslogtreecommitdiff
path: root/spectool
blob: bdb897ca2f41baacfb20a06ef711e4ff81ce937f (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/perl -w

################################################################
#
# Copyright (c) 1995-2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################

=head1 spectool

spectool - tool to work with rpm spec files

=head1 SYNOPSIS

spectool [options] specfiles...

=head1 OPTIONS

=over 4

=item B<--help>

display help as manpage

=item B<--dist>=I<STRING>

set distribution, e.g. "11.1-i586" or path to config

=item B<--archpath>=I<STRING>

compatible architecture separated by colon, e.g. C<i586:i486:i386>.
Autotected if missing.

=item B<--configdir>=I<STRING>

path to config files if B<--dist> didn't specify a full path

=item B<--define>=I<STRING>

=item B<--with>=I<STRING>

=item B<--without>=I<STRING>

same meaning as in rpmbuild

=item B<--tag>=I<STRING>

print tag from spec file, e.g. C<version>. Regexp is also possible,
e.g. C</source[01]/>

=item B<--sources>

print package source files. If a file C<sources> or
C<I<packagename>.sources> is present verify the checksums against
that.

=over 4

=item B<--update>

update the checksums

=item B<--download>

download missing sources

=back

=back

=head1 DESCRIPTION

The B<--sources> option allows to manage a sources file in a way
similar to Fedora. The sources file lists the check sums and file
names of the binary files specified in the spec file.

B<--sources> without further options compares the check sums of all
files and prints a report consisting of a character that describes
the status of the file and the file name. Meaning of the characters
is as follows:

=over 4

=item B<.> check sum matches

=item B<!> check sum broken

=item B<d> file is missing, checksum known. Can be verified after download

=item B<-> file is missing and checksum unknown

=item B<_> file is present but checksum unknown

=item B<t> text file, will be skipped for check sums

=item B<?> check sum known but not referenced from spec file

=back

Additionally specifying B<--update> recomputes all check sums and
updates the sources file.

With B<--download> all missing files are downloaded if the spec file
has an http or ftp url.

=head2 FORMAT OF THE SOURCES FILE

Lines of the form
<checksum> <whitespace> <filename>

=head2 NAME OF THE SOURCES FILE

A file named C<sources> is preferred if present for compatibility
with Fedora. It only contains md5 sums. If that file is not present
the C<.spec> suffix of the spec file is replaced with C<.sources>
and the this name used as sources file (e.g. C<foo.spec> ->
C<foo.sources>). In this file sha1 is preferred. Also, the name of
the algorithm is prepended with colon to the check sum.

=cut

my $builddir;

BEGIN {
  $builddir = ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
  unshift @INC, $builddir;
}

use strict;

use Build;
use Pod::Usage;
use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");

my (@opt_showtag, $opt_sources, $opt_update, $opt_download);

sub parse_depfile;

my ($dist, $rpmdeps, $archs, $configdir, $useusedforbuild);
my %options;

GetOptions (
  \%options,
  "help" => sub { pod2usage(-exitstatus => 0, -verbose => 2) },

  "dist=s" => \$dist,
  "archpath=s" => \$archs,
  "configdir=s" => \$configdir,
  "define=s" => sub { Build::define($_[1]) },
  "with=s" => sub { Build::define("_with_".$_[1]." --with-".$_[1]) },
  "without=s" => sub { Build::define("_without_".$_[1]." --without-".$_[1]) },

  "tag=s" => \@opt_showtag,
  "sources" => \$opt_sources,
  "update" => \$opt_update,
  "download" => \$opt_download,
  "download-force",
  "download-recompress=s",
  "download-outdir=s",
  "download-compare=s",
  "download-delete-identical",
) or pod2usage(1);

pod2usage(1) unless @ARGV;

my $ua;

my @specs = @ARGV;

die "--download must be used together with --sources\n" if ($opt_download && !$opt_sources);
die "--update must be used together with --sources\n" if ($opt_update && !$opt_sources);

$options{'download-recompress'} ||= 'auto';
$options{'download-outdir'}.='/' if ($options{'download-outdir'} && $options{'download-outdir'} !~ /\/$/);
$options{'download-outdir'} ||= '';
$options{'download-compare'}.='/' if ($options{'download-compare'} && $options{'download-compare'} !~ /\/$/);
$options{'download-compare'} ||= '';

my @archs;
if (!defined $archs) {
  use POSIX qw/uname/;
  my %archmap = qw/x86_64 i686 i686 i586 i586 i486 i486 i386/;
  my @a = uname();
  push @archs, $a[4];
  while(exists $archmap{$archs[-1]}) {
    push @archs, $archmap{$archs[-1]};
  }
} else {
  @archs = split(':', $archs);
}
push @archs, 'noarch' unless grep {$_ eq 'noarch'} @archs;

unless ($dist) {
    $dist = 'spectool';
#    $dist = `rpm -q --qf '%{DISTRIBUTION}' rpm 2>/dev/null`;
#    $dist = Build::dist_canon($dist||'', $archs[0]);
}

if($dist !~ /\// && !defined $configdir) {
  if($0 =~ /^\//) {
    use File::Basename qw/dirname/;
    $configdir = dirname($0).'/configs';
    undef $configdir unless -e $configdir.'/sl11.3.conf';
  } else {
    $configdir = $builddir.'/configs';
    undef $configdir unless -e $configdir.'/sl11.3.conf';
  }
  if(!defined $configdir) {
    print STDERR "please specify config dir\n";
  }
}

#######################################################################

# param: array to fill, spec file
# return: file name
sub read_sources_digests($$)
{
  my $files = shift;
  my $spec = shift;
  my $srcfile = 'sources';
  if (! -r $srcfile) {
    $srcfile = $spec;
    $srcfile =~ s/spec$/sources/;
  }
  if (open (F, '<', $srcfile)) {
    while(<F>) {
      chomp;
      my ($sum, $file) = split(/ +/, $_, 2);
      $files->{$file} = $sum;
    }
    close F;
  }
  return $srcfile;
}

# param: file, oldsum
# return: newsum or undef if match
sub check_sum($$)
{
  my $file = shift;
  my $oldsum = shift || 'sha1:';
  my $sum;
  my $type = 'md5:';
  if($oldsum =~ /^(\S+:)/) {
    $type = $1;
  } else {
    $oldsum = $type.$oldsum;
  }
  if ($type eq 'md5:') {
    $sum = $type.`md5sum $file` || die "md5sum failed\n";
  } elsif ($type eq 'sha1:') {
    $sum = $type.`sha1sum $file` || die "sha1sum failed\n";
  } else {
    die "unsupported digest type '$type'\n";
  }
  $sum =~ s/ .*//s;
  if($sum ne $oldsum) {
    return $sum;
  }
  return undef;
}

sub download($$)
{
  my ($url, $dest) = @_;
  my $retry = 3;
  while ($retry--) {
    my $res = $ua->mirror($url, $dest);
    last if $res->is_success;
    # if it's a redirect we probably got a bad mirror and should just retry
    return 0 unless $retry && $res->previous;
    warn "retrying $url\n";
  }
  return 1;
}

#######################################################################

my $ret = 0;
for my $spec (@specs) {
  my $cf = Build::read_config_dist($dist, $archs[0], $configdir);
  my $parsed = Build::parse($cf, $spec);

  if (!defined $parsed) {
    die "can't parse $spec\n";
  }

  for my $tag (@opt_showtag) {
    if($tag =~ /^\/(.+)\/$/) {
      my $expr = $1;
      for my $t (keys %$parsed) {
        if ($t =~ $expr) {
          push @opt_showtag, $t;
        }
      }
    } else {
      if(exists $parsed->{lc $tag}) {
        print $tag, ": ";
        my $v = $parsed->{lc $tag};
        $v = join(' ', @$v) if (ref $v eq 'ARRAY');
        print $v, "\n";
      } else {
        print STDERR "$tag does not exist\n";
      }
    }
  }

  if ($opt_sources) {
    my $files = {};
    my $srcfile = read_sources_digests($files, $spec);
    if ($opt_download) {
      unless ($ua) {
	use LWP::UserAgent;
	$ua = LWP::UserAgent->new(
	  agent => "openSUSE build service",
	  env_proxy => 1,
	  timeout => 42);
      }

      for my $t (keys %$parsed) {
        next unless ($t =~ /^(?:source|patch)\d*/);
        my $url = $parsed->{$t};
        next unless $url =~ /^(?:https?|ftp):\/\//;
        my $file = $url;
        $file =~ s/.*\///;
	my $src = $options{'download-compare'}.$file;
        next if -e $src && !($options{'download-force'} || $options{'download-delete-identical'});
        print "Downloading $file...\n";
	my $dest = $options{'download-outdir'}.$file;
	print "$url -> $dest\n";

        if(!download($url, $dest) && $options{'download-recompress'} ne 'no') {
	  # TODO
          # let's see if the file was recompressed
          if($url =~ s/\.bz2$/.gz/ && $file =~ s/\.bz2$/.gz/
            && !download($url, $dest)) {
            if(system('bznew', $dest) == 0) {
              print STDERR "Used $file and recompressed to bz2 instead\n";
            } else {
              unlink $dest;
            }
          } else {
            print STDERR "Downloading $file failed\n";
          }
        }
	if ($options{'download-delete-identical'} && $options{'download-outdir'}
	&& system('cmp', '-s', $dest, $src) == 0) {
	  unlink($dest);
	}
      }
    }
    if ($opt_update) {
      my $changed;
      for my $t (keys %$parsed) {
        next unless ($t =~ /^(?:source|patch)\d*/);
        my $file = $parsed->{$t};
        $file =~ s/.*\///;
        next unless -B $file;
        my $sum = check_sum($file, ($files->{$file} || ($srcfile eq 'sources'?'md5:':'sha1:')));
        if($sum) {
          print STDERR "update $file\n";
          $files->{$file} = $sum;
          $changed = 1;
        }
      }
      if($changed) {
        if(open(F, '>', $srcfile)) {
          for my $file (keys %$files) {
            $files->{$file} =~ s/^md5:// if $srcfile eq 'sources';
            print F $files->{$file}, ' ', $file, "\n";
          }
          close F;
        }
      }
    } else {
      for my $t (keys %$parsed) {
        next unless ($t =~ /^(?:source|patch)\d*/);
        my $file = $parsed->{$t};
        $file =~ s/.*\///;
        if (!exists $files->{$file}) {
          if (! -e $file) {
            print '- ';
          } elsif (-B $file) {
            print '_ ';
          } else {
            print 't ';
          }
        } elsif (! -e $file) {
            print 'd ';
            delete $files->{$file};
        } else {
          my $sum = check_sum($file, $files->{$file});
          if($sum) {
            print '! ';
	    $ret = 1;
          } else {
            print '. ';
          }
          delete $files->{$file};
        }
        print $parsed->{$t}, "\n";
      }
      for my $file (keys %$files) {
        print "? $file\n";
      }
    }
  }
}

exit $ret;