]> git.pld-linux.org Git - projects/cleanbuild.git/blob - findunusedbr
cleanbuild-docker: refactor to use functions
[projects/cleanbuild.git] / findunusedbr
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 die "Arguments missing: $0 [-c] <chroot> <spec>\n" unless @ARGV;
7 my $clear;
8 if ( $ARGV[0] eq "-c" ) {
9         $clear = 1;
10         shift @ARGV;
11 }
12 my $chroot = shift @ARGV or die "chroot directory not specified\n";
13 my $spec = shift @ARGV or die "Spec file not specified\n";
14
15 -d $chroot or die "chroot '$chroot' is not a directory\n";
16 -r $spec or die "spec file '$spec' is not readable\n";
17
18 my @rpms;
19 open F_IN, "<", $spec or die "Cannot open '$spec'\n";
20 while ( <F_IN> ) {
21         if ( /^\s*(?:%\{.*)?BuildRequires:\s*(\S+)/i ) {
22                 local $_ = $1;
23                 s/}$//;
24                 push @rpms, $_;
25         } elsif ( /^%(prep|build|changelog)/ ) {
26                 last;
27         }
28 }
29 close F_IN;
30
31 sub clear_files
32 {
33         my $rpm = shift;
34         my $files = shift;
35         foreach my $file ( @$files ) {
36                 chop $file;
37                 my $f = $chroot.$file;
38
39                 local $_;
40                 while ( $_ = readlink $f and m#^/# ) {
41                          $f = $chroot.$_;
42                 }
43
44                 # skip missing files, like "%_excludedocs 0" being active
45                 next unless stat $f;
46
47                 # XXX: this truncates mtime to its low resolution value
48                 my $mtime = (stat $f)[9];
49                 warn "Mtime failed on $f" unless $mtime;
50                 utime 0, $mtime, $f;
51         }
52 }
53
54 sub check_files
55 {
56         my $rpm = shift;
57         my $files = shift;
58         #my $used = 0;
59         foreach my $file ( @$files ) {
60                 chop $file;
61                 my $f = $chroot.$file;
62
63                 local $_;
64                 while ( $_ = readlink $f and m#^/# ) {
65                          $f = $chroot.$_;
66                 }
67
68                 my $atime = (stat $f)[8];
69                 if ( not defined $atime ) {
70                         print "$rpm: $f no atime\n";
71                 } elsif ( $atime > 0 ) {
72                         #print "$rpm: $file accessed\n";
73                         #$used = 1;
74                         return;
75                 } else {
76                         #print "$rpm: $file NOT accessed !\n";
77                 }
78         }
79         print "$rpm may be superfluous !\n";# unless $used;
80 }
81
82 sub rpm {
83         my @cmd = ("rpm", "--root=$chroot", @_);
84         open my $fh, '-|', @cmd or die "$!: @cmd";
85         my @data = <$fh>;
86         close $fh;
87         warn $! if $!;
88         return @data;
89 }
90
91 foreach my $rpm ( @rpms ) {
92         my @files = rpm("-ql", "--what-provides", "$rpm");
93         next if $files[0] =~ /^no package provides/;
94         #print "*** $rpm ***\n";
95         if ( $clear ) {
96                 clear_files( $rpm, \@files );
97         } else {
98                 check_files( $rpm, \@files );
99         }
100         #print "\n\n";
101 }
This page took 0.067313 seconds and 3 git commands to generate.