-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbless_run.pl
206 lines (126 loc) · 4.39 KB
/
bless_run.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use lib qw(/usr/mports/Tools/lib);
use Magus;
use Getopt::Std;
use File::Path;
use File::Copy qw(move);
use YAML qw(LoadFile);
main();
=head1 NAME
bless_run.pl -- Promote A Run
=head1 SYNOPSIS
bless_run.pl -r RUN -m MIRRORLIST -f FTPROOT -a ALIASLIST
=head1 DESCRIPTION
A run in magus can be blessed into a index that is used by the mport package
system. This index contains a list of availible packages, a list of
aliases, and a list of mirrors. The index is an L<sqlite3> database file that
is compressed with L<bzip2>.
This script will create the index file and copy the bundle files into the
given ftp root. The ftp root is organized as such:
$FTPROOT/$run->arch/$run->osversion/
index.db.bz2
bundle1.mport
bundle2.mport
...
=cut
sub main {
my %opts;
getopts('a:m:f:r:', \%opts) || usage();
unless ($opts{a} && -r $opts{a} && $opts{'m'} && -r $opts{'m'} && $opts{f} && -d $opts{f} && $opts{r}) {
usage();
}
my $run = Magus::Run->retrieve($opts{r}) || die "No such run: $opts{r}\n";
$run->status eq 'complete' || die "Run is not complete!\n";
$opts{f} = join('/', $opts{f}, $run->arch, $run->osversion . ".new");
mkpath($opts{f});
my $index = make_db_file(\%opts, 'index.db');
build_packages_table(\%opts, $index, $run);
build_aliases_table(\%opts, $index, $run);
build_mirror_list(\%opts, $index, $run);
copy_bundle_files(\%opts, $index, $run);
finish_index(\%opts, $index, 'index.db');
move_dirs(\%opts);
}
sub usage {
(my $self = $0) =~ s:.*/::;
die "Usage: $self -r <runid> -m <mirrorlist file> -a <alias file> -f <ftp root>\n";
}
sub make_db_file {
my ($opts, $file) = @_;
$file = "$opts->{f}/$file";
my $dbh = DBI->connect("dbi:SQLite:dbname=$file","","", { RaiseError => 1 });
$dbh->do("CREATE TABLE packages (pkg text NOT NULL, version text NOT NULL, comment text NOT NULL, www text NOT NULL, bundlefile text NOT NULL)");
$dbh->do("CREATE UNIQUE INDEX packages_pkg ON packages (pkg)");
$dbh->do("CREATE TABLE categories (pkg text NOT NULL, category text NOT NULL)");
$dbh->do("CREATE INDEX categories_pkg ON categories (pkg, category)");
$dbh->do("CREATE TABLE aliases (alias text NOT NULL, pkg text NOT NULL)");
$dbh->do("CREATE UNIQUE INDEX aliases_als ON aliases (alias)");
$dbh->do("CREATE TABLE mirrors (mirror text NOT NULL, country text NOT NULL)");
return $dbh;
}
sub build_packages_table {
my ($opts, $index, $run) = @_;
my $ports = $run->ports;
$index->begin_work;
my $sth = $index->prepare("INSERT INTO packages (pkg, version, comment, www, bundlefile) VALUES (?,?,?,?,?)");
while (my $port = $ports->next) {
next unless $port->status eq 'pass' || $port->status eq 'warn';
$sth->execute($port->name, $port->version, $port->description, $port->www, $port->bundle_file);
}
$sth->finish;
$index->commit;
}
sub build_aliases_table {
my ($opts, $index, $run) = @_;
my $aliases = LoadFile($opts->{a});
$index->begin_work;
my $sth = $index->prepare("INSERT INTO aliases (alias, pkg) VALUES (?, ?)");
while (my ($alias, $pkg) = each %$aliases) {
$sth->execute($alias, $pkg);
}
$sth->finish;
$index->commit;
}
sub build_mirror_list {
my ($opts, $index, $run) = @_;
my $mirrors = LoadFile($opts->{'m'});
$index->begin_work;
my $sth = $index->prepare("INSERT INTO mirrors (mirror, country) VALUES (?,?)");
while (my ($country, $list) = each %$mirrors) {
foreach my $mirror (@$list) {
$sth->execute($mirror, $country);
}
}
$sth->finish;
$index->commit;
}
sub finish_index {
my ($opts, $index, $file) = @_;
$index->disconnect;
my $cmnd = "bzip2 $opts->{f}/$file";
if (system($cmnd) != 0) {
die "$cmnd returned non-zero: $?\n";
}
unlink("$opts->{f}/$file");
}
sub move_dirs {
my ($opts) = @_;
my $finaldir = $opts->{f};
$finaldir =~ s/.new$//;
if (-d $finaldir) {
move($finaldir, "$finaldir.old") || die "Couldn't mv $finaldir $finaldir.old\n";
}
move($opts->{f}, $finaldir) || die "Couldn't mv $opts->{f} $finaldir: $!\n";
}
=head1 BUGS
Many. Contact [email protected].
=head1 AUTHOR
Chris Reinhardt <[email protected]>
=head1 COPYRIGHT
Copyright (c) 2009 Chris Reinhardt
All rights reserved.
=head1 LICENSE
This software is licensed under the 2-clause BSD license.
=cut