rebuildperl: Fix parsing repourls configuration option
[Fedora-Rebuild.git] / bin / rebuildlog
blobc69dc6446bbf8934548e4a2bf14060aca6013848
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Carp;
5 use Config::Tiny;
6 use Getopt::Long;
7 use Fedora::Rebuild::Set::Package;
8 use Fedora::Rebuild::Package;
9 use Fedora::Rebuild::Scheduler;
10 use File::Copy;
11 use File::Spec;
13 =encoding utf8
15 =head1 NAME
17 rebuildlog - Back build logs up
19 =head1 SYNOPIS
21 rebuildlog [--config FILE] DIRECTORY
23 =head1 DESCRIPTION
25 This tool copy build logs from configured Fedora::Rebuild location into given
26 directory.
28 =cut
30 my $cfgfile = File::Spec->catfile($ENV{HOME}, '.rebuildperlrc');
31 my %config = (
32 all => undef,
33 done => undef,
34 failed => undef,
35 failedtemp => undef,
36 workdir => undef,
37 repodir => undef,
38 dist => undef,
39 loadthreads => 1,
40 target => '',
41 message => '',
43 my $changed_only = 0;
45 =head1 OPTIONS
47 =head2 --config I<FILE>
49 Read configuration from I<FILE>, or F<~/.rebuildperlrc> if not specified.
51 =head1 FILES
53 =head2 F<~/.rebuildperlrc>
55 Configuration is in L<Config::Tiny> format. Following options are needed:
57 all = all
58 done = done
59 failed = failed
60 failedtemp = failedtemp
61 workdir = workdir
62 dist = rawhide
63 loadthreads = 4
65 =cut
68 sub copylogs {
69 my ($package, $destination) = @_;
71 my $directory = File::Spec->catfile($destination, 'packages',
72 $package->name);
73 File::Path::Tiny::mk($directory)
74 or die "Could not create directory `$directory': $!\n";
76 for (glob(File::Spec->catfile($package->mockdir, '*.log'))) {
77 copy($_, $directory) or
78 die "Could not copy `$_' into `$directory': $!\n";
81 return 1;
84 GetOptions(
85 'config=s' => \$cfgfile,
86 ) or die "Could not parse program options\n";
87 if ($#ARGV != 0) {
88 die "Missing directory name argument\n";
90 my $destination = $ARGV[0];
92 # Load configuration
93 if (-f $cfgfile) {
94 my $cfg = Config::Tiny->new->read($cfgfile);
95 if (! defined $cfg) {
96 print STDERR "Could not parse `" . $cfgfile .
97 "' configuration file: " . $Config::Tiny::errstr . "\n";
98 exit 1;
100 foreach (keys %{$cfg->{_}}) {
101 $config{$_} = $cfg->{_}->{$_};
102 $config{$_} = eval $config{$_} if $_ eq 'buildrequiresfilter';
106 # Load list of packages
107 my $packages = Fedora::Rebuild::Set::Package->new();
108 print "Loading list of all packages...\n";
109 open(my $file, '<', $config{all})
110 or die "Could not open file `$config{all}' for reading: $!\n";
111 while (local $_ = <$file>) {
112 chomp;
113 if (m/^\s*$/) { next; }
114 if ($packages->contains($_)) { next; }
115 my $package = Fedora::Rebuild::Package->new(name => $_,
116 workdir => $config{workdir}, dist => $config{dist},
117 pkgdist => $config{pkgdist}, target => $config{target},
118 message => $config{message});
119 $packages->insert($package);
121 if ($!) {
122 croak "Could not read list of all packages: $!";
124 close($file);
125 print "Number of packages: " . $packages->size() . "\n";
128 # Create destination directory
129 File::Path::Tiny::mk($destination)
130 or die "Could not create directory `$destination': $!\n";
131 # Copy package listings
132 for ('all', 'done', 'failed') {
133 my $output = File::Spec->catfile($destination, $_);
134 copy($config{$_}, $output) or
135 die "Could not copy `$config{$_}' into `$output': $!\n";
139 # Process packages
140 my $scheduler = Fedora::Rebuild::Scheduler->new(
141 limit => $config{loadthreads},
142 name => 'Copying logs',
143 total => $packages->size
145 my %jobs = ();
146 my $i = 0;
148 for my $package ($packages->packages) {
149 my $job = $scheduler->schedule(\&copylogs, $package, $destination);
150 if (! defined $job) {
151 next;
153 $jobs{$job} = $package;
154 my %finished = $scheduler->finish(++$i < $packages->size);
156 while (my ($job, $status) = each %finished) {
157 my $package = $jobs{$job};
159 my ($retval, $exception) = @$status;
160 if (!$retval) {
161 print "Could not copy logs for package `", $package->name, "'";
162 if (defined $exception) {
163 print ": $exception";
164 } else {
165 print ".\n";
167 print "Waiting for finishing scheduled jobs...\n";
168 $scheduler->finish(1);
169 print "All jobs have finished.\n";
170 croak "Could not copy all logs.\n";
175 exit 0;