3 ################################################################
5 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 or 3 as
9 # published by the Free Software Foundation.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program (see the file COPYING); if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 ################################################################
25 spectool - tool to work with rpm spec files
29 spectool [options] specfiles...
37 display help as manpage
39 =item B<--dist>=I<STRING>
41 set distribution, e.g. "11.1-i586" or path to config
43 =item B<--archpath>=I<STRING>
45 compatible architecture separated by colon, e.g. C<i586:i486:i386>.
46 Autotected if missing.
48 =item B<--configdir>=I<STRING>
50 path to config files if B<--dist> didn't specify a full path
52 =item B<--define>=I<STRING>
54 =item B<--with>=I<STRING>
56 =item B<--without>=I<STRING>
58 same meaning as in rpmbuild
60 =item B<--tag>=I<STRING>
62 print tag from spec file, e.g. C<version>. Regexp is also possible,
67 print package source files. If a file C<sources> or
68 C<I<packagename>.sources> is present verify the checksums against
79 download missing sources
87 The B<--sources> option allows to manage a sources file in a way
88 similar to Fedora. The sources file lists the check sums and file
89 names of the binary files specified in the spec file.
91 B<--sources> without further options compares the check sums of all
92 files and prints a report consisting of a character that describes
93 the status of the file and the file name. Meaning of the characters
98 =item B<.> check sum matches
100 =item B<!> check sum broken
102 =item B<d> file is missing, checksum known. Can be verified after download
104 =item B<-> file is missing and checksum unknown
106 =item B<_> file is present but checksum unknown
108 =item B<t> text file, will be skipped for check sums
110 =item B<?> check sum known but not referenced from spec file
114 Additionally specifying B<--update> recomputes all check sums and
115 updates the sources file.
117 With B<--download> all missing files are downloaded if the spec file
118 has an http or ftp url.
120 =head2 FORMAT OF THE SOURCES FILE
123 <checksum> <whitespace> <filename>
125 =head2 NAME OF THE SOURCES FILE
127 A file named C<sources> is preferred if present for compatibility
128 with Fedora. It only contains md5 sums. If that file is not present
129 the C<.spec> suffix of the spec file is replaced with C<.sources>
130 and the this name used as sources file (e.g. C<foo.spec> ->
131 C<foo.sources>). In this file sha1 is preferred. Also, the name of
132 the algorithm is prepended with colon to the check sum.
139 $builddir = ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
140 unshift @INC, $builddir;
148 Getopt::Long::Configure("no_ignore_case");
150 my (@opt_showtag, $opt_sources, $opt_update, $opt_download);
154 my ($dist, $rpmdeps, $archs, $configdir, $useusedforbuild);
159 "help" => sub { pod2usage(-exitstatus => 0, -verbose => 2) },
162 "archpath=s" => \$archs,
163 "configdir=s" => \$configdir,
164 "define=s" => sub { Build::define($_[1]) },
165 "with=s" => sub { Build::define("_with_".$_[1]." --with-".$_[1]) },
166 "without=s" => sub { Build::define("_without_".$_[1]." --without-".$_[1]) },
168 "tag=s" => \@opt_showtag,
169 "sources" => \$opt_sources,
170 "update" => \$opt_update,
171 "download" => \$opt_download,
173 "download-recompress=s",
175 "download-compare=s",
176 "download-delete-identical",
179 pod2usage(1) unless @ARGV;
185 die "--download must be used together with --sources\n" if ($opt_download && !$opt_sources);
186 die "--update must be used together with --sources\n" if ($opt_update && !$opt_sources);
188 $options{'download-recompress'} ||= 'auto';
189 $options{'download-outdir'}.='/' if ($options{'download-outdir'} && $options{'download-outdir'} !~ /\/$/);
190 $options{'download-outdir'} ||= '';
191 $options{'download-compare'}.='/' if ($options{'download-compare'} && $options{'download-compare'} !~ /\/$/);
192 $options{'download-compare'} ||= '';
195 if (!defined $archs) {
197 my %archmap = qw/x86_64 i686 i686 i586 i586 i486 i486 i386/;
200 while(exists $archmap{$archs[-1]}) {
201 push @archs, $archmap{$archs[-1]};
204 @archs = split(':', $archs);
206 push @archs, 'noarch' unless grep {$_ eq 'noarch'} @archs;
210 # $dist = `rpm -q --qf '%{DISTRIBUTION}' rpm 2>/dev/null`;
211 # $dist = Build::dist_canon($dist||'', $archs[0]);
214 if($dist !~ /\// && !defined $configdir) {
216 use File::Basename qw/dirname/;
217 $configdir = dirname($0).'/configs';
218 undef $configdir unless -e $configdir.'/sl11.3.conf';
220 $configdir = $builddir.'/configs';
221 undef $configdir unless -e $configdir.'/sl11.3.conf';
223 if(!defined $configdir) {
224 print STDERR "please specify config dir\n";
228 #######################################################################
230 # param: array to fill, spec file
232 sub read_sources_digests($$)
236 my $srcfile = 'sources';
239 $srcfile =~ s/spec$/sources/;
241 if (open (F, '<', $srcfile)) {
244 my ($sum, $file) = split(/ +/, $_, 2);
245 $files->{$file} = $sum;
252 # param: file, oldsum
253 # return: newsum or undef if match
257 my $oldsum = shift || 'sha1:';
260 if($oldsum =~ /^(\S+:)/) {
263 $oldsum = $type.$oldsum;
265 if ($type eq 'md5:') {
266 $sum = $type.`md5sum $file` || die "md5sum failed\n";
267 } elsif ($type eq 'sha1:') {
268 $sum = $type.`sha1sum $file` || die "sha1sum failed\n";
270 die "unsupported digest type '$type'\n";
273 if($sum ne $oldsum) {
281 my ($url, $dest) = @_;
284 my $res = $ua->mirror($url, $dest);
285 last if $res->is_success;
286 # if it's a redirect we probably got a bad mirror and should just retry
287 return 0 unless $retry && $res->previous;
288 warn "retrying $url\n";
293 #######################################################################
296 for my $spec (@specs) {
297 my $cf = Build::read_config_dist($dist, $archs[0], $configdir);
298 my $parsed = Build::parse($cf, $spec);
300 if (!defined $parsed) {
301 die "can't parse $spec\n";
304 for my $tag (@opt_showtag) {
305 if($tag =~ /^\/(.+)\/$/) {
307 for my $t (keys %$parsed) {
309 push @opt_showtag, $t;
313 if(exists $parsed->{lc $tag}) {
315 my $v = $parsed->{lc $tag};
316 $v = join(' ', @$v) if (ref $v eq 'ARRAY');
319 print STDERR "$tag does not exist\n";
326 my $srcfile = read_sources_digests($files, $spec);
330 $ua = LWP::UserAgent->new(
331 agent => "openSUSE build service",
336 for my $t (keys %$parsed) {
337 next unless ($t =~ /^(?:source|patch)\d*/);
338 my $url = $parsed->{$t};
339 next unless $url =~ /^(?:https?|ftp):\/\//;
342 my $src = $options{'download-compare'}.$file;
343 next if -e $src && !($options{'download-force'} || $options{'download-delete-identical'});
344 print "Downloading $file...\n";
345 my $dest = $options{'download-outdir'}.$file;
346 print "$url -> $dest\n";
348 if(!download($url, $dest) && $options{'download-recompress'} ne 'no') {
350 # let's see if the file was recompressed
351 if($url =~ s/\.bz2$/.gz/ && $file =~ s/\.bz2$/.gz/
352 && !download($url, $dest)) {
353 if(system('bznew', $dest) == 0) {
354 print STDERR "Used $file and recompressed to bz2 instead\n";
359 print STDERR "Downloading $file failed\n";
362 if ($options{'download-delete-identical'} && $options{'download-outdir'}
363 && system('cmp', '-s', $dest, $src) == 0) {
370 for my $t (keys %$parsed) {
371 next unless ($t =~ /^(?:source|patch)\d*/);
372 my $file = $parsed->{$t};
374 next unless -B $file;
375 my $sum = check_sum($file, ($files->{$file} || ($srcfile eq 'sources'?'md5:':'sha1:')));
377 print STDERR "update $file\n";
378 $files->{$file} = $sum;
383 if(open(F, '>', $srcfile)) {
384 for my $file (keys %$files) {
385 $files->{$file} =~ s/^md5:// if $srcfile eq 'sources';
386 print F $files->{$file}, ' ', $file, "\n";
392 for my $t (keys %$parsed) {
393 next unless ($t =~ /^(?:source|patch)\d*/);
394 my $file = $parsed->{$t};
396 if (!exists $files->{$file}) {
404 } elsif (! -e $file) {
406 delete $files->{$file};
408 my $sum = check_sum($file, $files->{$file});
415 delete $files->{$file};
417 print $parsed->{$t}, "\n";
419 for my $file (keys %$files) {