| 1 |
#!/usr/bin/perl |
| 2 |
# vim: set ts=3 sw=3 ai et nu: |
| 3 |
# |
| 4 |
# Synchronizes repository configuration files with what's on the |
| 5 |
# openSUSE Build Service (using osc). |
| 6 |
# Needs an operational osc client. |
| 7 |
# |
| 8 |
# by Pascal Bleser <pascal.bleser@opensuse.org> |
| 9 |
# |
| 10 |
# This library is free software; you can redistribute it and/or modify it |
| 11 |
# under the terms of the GNU Lesser General Public License as published by |
| 12 |
# the Free Software Foundation; either version 2.1 of the License, or (at |
| 13 |
# your option) any later version. |
| 14 |
# |
| 15 |
# This library is distributed in the hope that it will be useful, but |
| 16 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
# Lesser General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU Lesser General Public |
| 21 |
# License along with this library; if not, write to the Free Software |
| 22 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, |
| 23 |
# USA. |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Term::ProgressBar; |
| 28 |
use File::Spec; |
| 29 |
use File::Basename; |
| 30 |
use Getopt::Long; |
| 31 |
|
| 32 |
my $repos = "./repos.d"; |
| 33 |
my $cache_dir = "./cache.d"; |
| 34 |
my $verbose = undef; |
| 35 |
|
| 36 |
GetOptions( |
| 37 |
'v|verbose' => \$verbose, |
| 38 |
); |
| 39 |
|
| 40 |
my @projects = (); |
| 41 |
{ |
| 42 |
print "Retrieving top-level project list\n" if $verbose; |
| 43 |
open(my $osc, 'osc ls|') or die "failed to run osc ls: $!"; |
| 44 |
while (<$osc>) { |
| 45 |
chomp(); |
| 46 |
push(@projects, $_) unless /^home:/; |
| 47 |
} |
| 48 |
close($osc); |
| 49 |
print scalar(@projects), " top-level projects\n" if $verbose; |
| 50 |
} |
| 51 |
|
| 52 |
my $progress = undef; |
| 53 |
my $count = 0; |
| 54 |
if ($verbose) { |
| 55 |
$progress = Term::ProgressBar->new({ |
| 56 |
count => scalar(@projects), |
| 57 |
name => 'projects', |
| 58 |
ETA => 'linear', |
| 59 |
}); |
| 60 |
#$progress->minor(0); |
| 61 |
} |
| 62 |
|
| 63 |
foreach my $p (@projects) { |
| 64 |
if (defined $progress) { |
| 65 |
$count++; |
| 66 |
$progress->update($count); |
| 67 |
} |
| 68 |
|
| 69 |
my $conf_file = File::Spec->catfile($repos, $p.".conf"); |
| 70 |
{ |
| 71 |
my $dir = dirname($conf_file); |
| 72 |
mkdir($dir, 0750) unless -d $dir; |
| 73 |
} |
| 74 |
my @conf = (); |
| 75 |
if (-e $conf_file) { |
| 76 |
open(my $fh, '<', $conf_file) or die "failed to open $conf_file: $!"; |
| 77 |
while (<$fh>) { |
| 78 |
chomp; |
| 79 |
push(@conf, $_); |
| 80 |
} |
| 81 |
close($fh); |
| 82 |
} |
| 83 |
my @enabled_conf = (); |
| 84 |
foreach (grep { not /^$/ } map { s/#.*$//; s/^\s*//; s/\s*$//; $_ } @conf) { |
| 85 |
my ($id, $dist, $baseurl, $flags) = split(/\s+/); |
| 86 |
my $r = { |
| 87 |
repoid => $id, |
| 88 |
distribution=> $dist, |
| 89 |
baseurl => $baseurl, |
| 90 |
}; |
| 91 |
push(@enabled_conf, $r); |
| 92 |
} |
| 93 |
my %h = map { $_->{repoid} => $_ } @enabled_conf; |
| 94 |
|
| 95 |
my @dists = (); |
| 96 |
open(my $osc, 'osc repos "'.$p.'"|') or die "failed to run osc repos $p: $!"; |
| 97 |
while (<$osc>) { |
| 98 |
chomp; |
| 99 |
s/^\s*//; |
| 100 |
s/\s*$//; |
| 101 |
my ($dist, $arch) = split(/\s+/); |
| 102 |
push(@dists, $dist) if $dist =~ /^openSUSE_11\.[123]$/; |
| 103 |
} |
| 104 |
close($osc); |
| 105 |
|
| 106 |
my $changed = undef; |
| 107 |
my %unique_dists = map { $_ => 1 } @dists; |
| 108 |
foreach my $d (keys %unique_dists) { |
| 109 |
my $subdirs = $p; |
| 110 |
$subdirs =~ s|:|:/|g; |
| 111 |
my $repoid = $p.'-'.$distversion; |
| 112 |
my $baseurl = "http://download.opensuse.org/repositories/".$subdirs."/".$d; |
| 113 |
if (not exists $h{$repoid}) { |
| 114 |
push(@conf, join("\t", ($repoid, $d, $baseurl))); |
| 115 |
$changed = 1; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if ($changed) { |
| 120 |
open(my $fh, '>', $conf_file) or die "failed to open $conf_file for writing: $!"; |
| 121 |
foreach (@conf) { |
| 122 |
print $fh $_, "\n"; |
| 123 |
} |
| 124 |
close($fh); |
| 125 |
} |
| 126 |
} |