Debian Codegraph @

scripts/debaux-publish

raw
1 #! /usr/bin/perl -w
2 #
3 # debaux-publish - Debian package publishing script
4 #
5 # Copyright 2000,2001,2002,2006,2009 Stefan Hornburg (Racke) <[email protected]>
6 #
7 # This program is free software; you can redistribute it
8 # and/or modify it under the terms of the GNU General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public
19 # License along with this program; if not, write to the Free
20 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 # MA 02111-1307 USA.
22
23 # MODULE SETUP
24 # ============
25
26 use strict;
27 use AppConfig qw(:argcount);
28 use Data::Dumper;
29 $Data::Dumper::Terse = 1;
30 use File::Basename;
31 use Getopt::Long;
32 use Net::SSH qw(sshopen2);
33
34 use DebAux::Util;
35
36 # VARIABLES
37 # =========
38
39 my $infref;
40 # package info by file name, binary package name, source package name
41 my (%upmap, %pkgmap, %srcmap);
42 my @secdirs;
43 # remote location information
44 my %rmtloc;
45 # used distributions and architectures
46 my $debarch;
47 my %distmap;
48 my %distarches;
49 # settings specific for a site
50 my %sitedefs;
51
52 # COMMANDLINE PARAMETERS
53 # ======================
54
55 my %opts = ('dry-run' => 0);
56 my $whandler = $SIG{__WARN__};
57 $SIG{__WARN__} = sub {print STDERR "$0: @_";};
58 unless (GetOptions (\%opts, 'distribution|d=s', 'dry-run|n', 'site=s')) {
59 exit 1;
60 }
61 $SIG{__WARN__} = $whandler;
62
63 # PARSE CONFIGURATION FILE
64 # ========================
65
66 my $cfgfile = $ENV{'HOME'} . '/.debpublishrc';
67 my $config = new AppConfig ({GLOBAL => {ARGCOUNT => ARGCOUNT_ONE},
68 ERROR => sub {conferror($cfgfile,@_)}});
69 $config -> define ('host', {ARGCOUNT => ARGCOUNT_HASH});
70 $config -> define ('remote_host');
71 $config -> define ('remote_dir', {ARGCOUNT => ARGCOUNT_HASH});
72 $config -> define ('remote_user');
73 $config -> define ('user', {ARGCOUNT => ARGCOUNT_HASH});
74 $config -> define ('override');
75 $config -> define ('distconf');
76 $config -> define ('dist_remote_dir');
77
78 open (CONF, $cfgfile)
79 || die "$0: Couldn't open configuration file $cfgfile: $!\n";
80 $config -> file (\*CONF);
81 close (CONF);
82
83 # sanity checks
84 if ($opts{'site'}) {
85 # if a site is given, we need its name and the remote directory
86 my $href = $config->get('remote_dir');
87 unless (exists $href->{$opts{'site'}}) {
88 die "$0: remote directory for site \"$opts{'site'}\" not defined\n";
89 }
90 $sitedefs{'remote_dir'} = $href->{$opts{'site'}};
91 $href = $config->get('host');
92 unless (exists $href->{$opts{'site'}}) {
93 die "$0: remote host for site \"$opts{'site'}\" not defined\n";
94 }
95 $sitedefs{'host'} = $href->{$opts{'site'}};
96 $href = $config->get('user');
97 $sitedefs{'user'} = $href->{$opts{'site'}};
98 }
99
100 # determine architecture
101 $debarch = DebAux::Util::architecture();
102
103 my $debfile;
104
105 for (@ARGV) {
106 if (/\.deb$/) {
107 # Debian package file
108 $debfile = $_;
109 $infref = get_debinfo ($_);
110 if (exists $pkgmap{$infref->{'package'}}) {
111 if (compare_versions('>', $infref->{'version'},
112 $pkgmap{$infref->{'package'}}->{'version'})) {
113 # remove entrie for older package
114 delete $upmap{$pkgmap{$infref->{'package'}}->{'deb'}};
115 } else {
116 next;
117 }
118 }
119 # push (@secdirs, $infref->{'section'});
120 $upmap{$debfile} = {arch => $infref->{'arch'},
121 dir => $infref->{'section'},
122 deb => $debfile,
123 dsc => dsc_filename ($infref),
124 name => $infref->{'package'},
125 priority => $infref->{'priority'},
126 source => $infref->{'source'},
127 version => $infref->{'version'},
128 };
129 $pkgmap{$infref->{'package'}} = $upmap{$debfile};
130 } else {
131 die "$0: What is $_ ?\n";
132 }
133 }
134
135 unless (keys %upmap) {
136 die "$0: Nothing found to publish\n";
137 }
138
139 for (keys %upmap) {
140 # complete entries for valid packages
141 push(@{$srcmap{$upmap{$_}->{'source'}}->{'bin'}}, $upmap{$_}->{'name'});
142 $srcmap{$upmap{$_}->{'source'}}->{'localdir'} = dirname($_);
143 $srcmap{$upmap{$_}->{'source'}}->{'version'} = $upmap{$_}->{'version'};
144 $srcmap{$upmap{$_}->{'source'}}->{'dir'} = $upmap{$_}->{'dir'};
145 }
146
147 my $distconfig;
148 my %distrmtdirs;
149 my $rmtdir;
150
151 # read distribution configuration file
152 my $distrcfile;
153 my %vars;
154
155 if ($opts{'distribution'}) {
156 for (keys %upmap) {
157 $vars{$upmap{$_}->{source}} = [$opts{'distribution'}];
158 $vars{$upmap{$_}->{name}} = [$opts{'distribution'}];
159 }
160 } else {
161 $distrcfile = $config->get('distconf');
162
163 unless ($distrcfile) {
164 die "$0: please specify distribution configuration file or use the distribution commandline option\n";
165 }
166
167 $distconfig = new AppConfig ({GLOBAL => {ARGCOUNT => ARGCOUNT_LIST},
168 CREATE => 1,
169 ERROR => sub {conferror($distrcfile,@_)}});
170
171 open (CONF, $distrcfile)
172 || die "$0: Couldn't open configuration file $distrcfile: $!\n";
173 $distconfig -> file (\*CONF);
174 close (CONF);
175 %vars = $distconfig -> varlist ('.');
176 }
177
178 my $dscfile;
179
180 if ($opts{site}) {
181 $rmtdir = $sitedefs{'remote_dir'};
182 } else {
183 $rmtdir = $config -> get ('dist_remote_dir');
184 }
185
186 foreach my $src (keys %vars) {
187 if (exists $srcmap{$src}) {
188 # read corresponding .dsc file
189 $dscfile = $srcmap{$src}->{localdir} . '/' . $src . '_' . cut_epoch($srcmap{$src}->{version}) . '.dsc';
190 open (DSC, $dscfile)
191 || die ("$0: Couldn't open $dscfile\n");
192 my ($afterfiles, @frags, @srcfiles);
193 while (<DSC>) {
194 # ignore comments
195 next if /^Comment:/;
196
197 if ($afterfiles) {
198 last unless /\S/;
199 s/^\s+//;
200 @frags = split (/\s+/);
201 push (@srcfiles, $frags[2]);
202 } elsif (index ($_, 'Files:') == 0) {
203 $afterfiles = 1;
204 }
205 }
206 close (DSC);
207 push (@srcfiles, $src . '_'
208 . cut_epoch ($srcmap{$src}->{version}) . '.dsc');
209 for my $dist (@{$vars{$src}}) {
210 next if $dist eq '1';
211 my (@distfrags, $cmpop, $cmpversion, $sysret);
212 @distfrags = split (/\s+/, $dist);
213 if (@distfrags == 3) {
214 ($dist, $cmpop, $cmpversion) = @distfrags;
215 my $cmpret = compare_versions($cmpop, $srcmap{$src}->{version},$cmpversion);
216 next if $cmpret == 0; # skip this entry
217 }
218
219 $distmap{$dist} = 1;
220
221 # add source files
222 for my $srcfile (@srcfiles) {
223 push (@{$rmtloc{$srcmap{$src}->{localdir} . '/' . $srcfile}},
224 join ('/',
225 "dists/$dist/main/source",
226 $srcmap{$src}->{dir}, $srcfile));
227 }
228
229 # loop over all corresponding binary packages
230 for my $pkg (@{$srcmap{$src}->{'bin'}}) {
231 push (@{$pkgmap{$pkg}->{dists}}, $dist);
232 my $arch = $pkgmap{$pkg}->{arch};
233 if ($arch eq 'all') {
234 $arch = $debarch;
235 }
236 unless (grep {$_ eq $pkgmap{$pkg}->{arch}} @{$distarches{$dist}}) {
237 push (@{$distarches{$dist}}, $arch);
238 }
239 push (@{$rmtloc{$pkgmap{$pkg}->{deb}}},
240 join ('/',
241 "dists/$dist/main/binary-" . $pkgmap{$pkg}->{arch},
242 $pkgmap{$pkg}->{dir},
243 basename($pkgmap{$pkg}->{deb})));
244 if ($pkgmap{$pkg}->{arch} eq 'all') {
245 push (@{$rmtloc{$pkgmap{$pkg}->{deb}}},
246 join ('/',
247 "dists/$dist/main/binary-$debarch",
248 $pkgmap{$pkg}->{dir},
249 basename($pkgmap{$pkg}->{deb})));
250 }
251 }
252 }
253 }
254 }
255
256 for (keys %rmtloc) {
257 for my $loc (@{$rmtloc{$_}}) {
258 $distrmtdirs{$rmtdir . '/' . dirname($loc)} = 1;
259 }
260 }
261
262 sub conferror {
263 my $file = shift;
264 die "$0: $file: @_\n";
265 }
266
267 # CREATE REMOTE DIRECTORIES
268 # =========================
269
270 $rmtdir ||= '';
271
272 if ($rmtdir !~ m%^/\S+%) {
273 die "$0: suspicious remote directory \"$rmtdir\"\n";
274 }
275
276 my $seclist = join (',', map{"'$_'"} (@secdirs, "$rmtdir/indices",
277 sort(keys(%distrmtdirs))));
278 my $script = <<'EOF';
279 use strict;
280 use File::Path;
281
282 my $dryrun = '@dryrun@';
283
284 if ($dryrun) {
285 print "Creating directories @secdirs@\n";
286 } else {
287 mkpath([@secdirs@], 1, 0755);
288 }
289
290 EOF
291
292 my $host = $config->get('remote_host');
293 my $user = $config->get('remote_user');
294
295 if ($opts{'site'}) {
296 $host = $sitedefs{'host'};
297 $user = $sitedefs{'user'};
298 }
299
300 remote_perl ($script, {secdirs => $seclist,
301 dryrun => $opts{'dry-run'}},
302 $host, $user);
303
304 # UPLOAD FILES
305 # ============
306
307 $script = << 'EOF';
308 use strict;
309 my $locs = @locs@;
310 my $dryrun = '@dryrun@';
311
312 chdir('@remotedir@') || die "Couldn't enter remote directory @remotedir@: $!\n";
313 for (@$locs) {
314 if (-f $_ || -l $_) {
315 if ($dryrun) {
316 print "Unlinking $_\n";
317 } else {
318 unlink($_) || die "Couldn't delete $_: $!\n";
319 }
320 }
321
322 if ($dryrun) {
323 print "Creating symbolic link from $_ to ../../../../../@original@\n";
324 } else {
325 symlink('../../../../../@original@', $_) || die "Couldn't create symbolic link @original@ => $_: $!\n";
326 }
327 }
328 EOF
329
330 my $sysret;
331
332 for (sort (keys %rmtloc)) {
333 my @locs = @{$rmtloc{$_}};
334 my $firstloc = shift(@locs);
335
336 # copy first location
337 if ($opts{'dry-run'}) {
338 print "Running rsync $_ $user\@$host:$rmtdir/$firstloc\n";
339 } else {
340 $sysret = system ("rsync $_ $user\@$host:$rmtdir/$firstloc");
341 if ($sysret) {
342 die "Rsync failed with exit state ", $sysret >> 8, "\n";
343 }
344 }
345
346 # symlink other locations
347 remote_perl ($script,
348 {original => $firstloc,
349 locs => Dumper(\@locs),
350 remotedir => $rmtdir,
351 dryrun => $opts{'dry-run'}},
352 $host,
353 $user
354 );
355 }
356
357 # DELETE OLD PACKAGES, MANGLE OVERRIDE FILE, UPDATE PACKAGES FILE
358 # ===============================================================
359
360 my $ovfile = $config->get('override');
361
362 $script = << 'EOF';
363 use strict;
364 use File::Basename;
365 use File::Find;
366
367 # FUNCTION: cut_epoch
368 sub cut_epoch {
369 my ($version) = @_;
370 $version =~ s/^\d+://;
371 $version;
372 }
373
374 my (@ovlines, $package, $state, $section);
375 my $pkgmap = @pkgdata@;
376 my $srcmap = @srcdata@;
377 my $dist = '@dist@';
378 my $archs = @archs@;
379 my $dryrun = '@dryrun@';
380
381 sub debcleaner {
382 my ($package, $file, $var, $value);
383
384 # remove broken symlinks
385 if (-l $_ && ! -f readlink($_)) {
386 if ($dryrun) {
387 print "Deleting broken symlink $_\n";
388 } else {
389 print "Deleting broken symlink $_\n";
390 unlink ($_) || die "$0: Couldn't delete symlink $_: $!\n";
391 }
392 return;
393 }
394
395 # consider only valid files
396 return unless (-f $_ || -l $_);
397 # return unless /\.deb$/;
398
399 $file = $_;
400
401 if (/\.deb$/) {
402 open (DPKGINF, "dpkg-deb --info $_ |");
403 while (<DPKGINF>) {
404 print "dpkg-deb: $_";
405 s/\s+$//;
406 ($var, $value) = split (/:\s*/);
407 if ($var =~ /^ Package/) {
408 $package = $value;
409 last;
410 }
411 }
412 close (DPKGINF) || die "$0: Bad exit state from dpkg-deb\n";
413
414 # see if package corresponds to one of the published
415 if (exists $$pkgmap{$package}) {
416 if ($file ne basename($$pkgmap{$package}->{deb})) {
417 # inappropriate package => need to delete it
418 print "Delete $file\n";
419 unless ($dryrun) {
420 unlink ($file) || die "$0: Couldn't delete file $file: $!\n";
421 }
422 }
423 } else {
424 warn "Package $package not found\n";
425 }
426 } elsif (/^(.*?)_(.*?)\.diff\.gz$/) {
427 if (exists $$srcmap{$1}) {
428 if ($file ne $1 . '_' . cut_epoch($$srcmap{$1}->{version}) . '.diff.gz') {
429 print "Delete $file\n";
430 unless ($dryrun) {
431 unlink ($file) || die "$0: Couldn't delete file $file: $!\n";
432 }
433 }
434 }
435 } elsif (/^(.*?)_(.*?)\.dsc$/) {
436 if (exists $$srcmap{$1}) {
437 if ($file ne $1 . '_' . cut_epoch($$srcmap{$1}->{version}) . '.dsc') {
438 print "Delete $file\n";
439 unless ($dryrun) {
440 unlink ($file) || die "$0: Couldn't delete file $file: $!\n";
441 }
442 }
443 }
444 } elsif (/^(.*?)_(.*?)\.orig\.tar\.gz$/) {
445 if (exists $$srcmap{$1}) {
446 my ($origversion) = split (/-/, cut_epoch($$srcmap{$1}->{version}));
447
448 if ($file ne $1 . '_' . $origversion . '.orig.tar.gz') {
449 print "Delete $file\n";
450 unless ($dryrun) {
451 unlink ($file) || die "$0: Couldn't delete file $file: $!\n";
452 }
453 }
454 }
455 }
456 }
457
458 #find (\&debcleaner, '@rootdir@/main');
459
460 unless ($dryrun) {
461 unless (-f '@override@') {
462 open (OVERRIDE, '>@override@');
463 close (OVERRIDE);
464 }
465
466 open (OVERRIDE, '+<@override@')
467 || die ("$0: Couldn't open override file @override@: $!\n");
468 while (<OVERRIDE>) {
469 chomp;
470 next unless /\S/;
471 my $distflag = 1;
472 ($package, $state, $section) = split;
473 # is this a package just uploaded ?
474 if (exists $$pkgmap{$package}) {
475 if ($dist) {
476 $distflag = grep {$_ eq $dist} @{$$pkgmap{$package}->{dists}};
477 next unless $distflag;
478 }
479 push (@ovlines, join (' ', $package, $$pkgmap{$package}->{priority},
480 $$pkgmap{$package}->{dir}));
481 delete $$pkgmap{$package};
482 } else {
483 push (@ovlines, $_);
484 }
485 }
486 for (sort (keys %$pkgmap)) {
487 push (@ovlines, join (' ', $_, $$pkgmap{$_}->{priority},
488 $$pkgmap{$_}->{dir}));
489 }
490 unless (truncate (OVERRIDE, 0)) {
491 die ("$0: Couldn't truncate @override@: $!\n");
492 }
493 seek (OVERRIDE, 0, 0);
494 print OVERRIDE join ("\n", @ovlines, '');
495 close(OVERRIDE);
496 warn "Running dpkg-scanpackages on @$archs\n";
497 for (@$archs) {
498 chdir ("@rootdir@") || die "$0: Couldn't enter directory @rootdir@: $!\n";
499 system ("dpkg-scanpackages main/binary-$_ '@override@' dists/$dist/ | gzip -c > main/binary-$_/Packages.gz");
500 }
501 chdir ("@rootdir@") || die "$0: Couldn't enter directory @rootdir@: $!\n";
502 system ("dpkg-scansources main/source '@override@' dists/$dist/ | gzip -c > main/source/Sources.gz");
503 }
504 EOF
505
506 #remote_perl ($script,
507 # {override => $config->get('override'),
508 # pkgdata => Dumper(\%pkgmap),
509 # rootdir => $config->get('remote_dir')},
510 # $config->get('remote_host'),
511 # $config->get('remote_user')
512 # );
513
514 for (sort (keys %distmap)) {
515 print "Preparing distribution $_\n";
516 remote_perl ($script,
517 {archs => Dumper($distarches{$_}),
518 dist => $_,
519 override => "$rmtdir/indices/override.$_",
520 pkgdata => Dumper(\%pkgmap),
521 rootdir => "$rmtdir/dists/$_",
522 srcdata => Dumper(\%srcmap),
523 dryrun => $opts{'dry-run'}},
524 $host,
525 $user
526 );
527 }
528
529 # -----------------------------------------------
530 # FUNCTION: compare_versions OP VERSION1 VERSION2
531 #
532 # Compares versions VERSION1 and VERSION2 with
533 # operator OP. Returns the result.
534 # -----------------------------------------------
535
536 sub compare_versions {
537 my ($op, $version1, $version2) = @_;
538 my $sysret;
539
540 $sysret = system ("dpkg --compare-versions '$version1' '$op' '$version2'");
541 $sysret /= 256;
542
543 if ($sysret != 0 && $sysret != 1) {
544 die "$0: dpkg --compare-versions '$version1' '$op' '$version2' failed\n";
545 }
546 return ! $sysret;
547 }
548
549 # ----------------------------------------------------------
550 # FUNCTION: get_debinfo FILE
551 #
552 # Retrieves information about the Debian package and returns
553 # a hash reference with the following pieces of information:
554 #
555 # arch architecture where the package belongs to
556 # package package name
557 # priority package priority
558 # section category where the package belongs to
559 # source source package name
560 # ----------------------------------------------------------
561
562 sub get_debinfo {
563 my ($file) = @_;
564 my (%infmap, $var, $value, $param);
565
566 open (DPKGINF, "dpkg-deb --info $file |");
567 while (<DPKGINF>) {
568 s/\s+$//;
569 ($var, $value) = split (/:\s*/, $_, 2);
570 if ($var =~ /^ Section/) {
571 if ($value eq 'non-US/main') {
572 $infmap{'section'} = 'non-us';
573 } else {
574 $infmap{'section'} = $value;
575 }
576 } elsif ($var =~ /^ Architecture/) {
577 $infmap{'arch'} = $value;
578 } elsif ($var =~ /^ Package/) {
579 $infmap{'package'} = $value;
580 } elsif ($var =~ /^ Priority/) {
581 $infmap{'priority'} = $value;
582 } elsif ($var =~ /^ Source/) {
583 ($value, $param) = split (/\s/, $value);
584 $infmap{'source'} = $value;
585 if (defined $param && $param =~ /\((.*?)\)/) {
586 $infmap{'version'} = $1;
587 }
588 } elsif ($var =~ /^ Version/) {
589 $infmap{'version'} = $value;
590 }
591 }
592 close (DPKGINF) || die "$0: Bad exit state from dpkg-deb\n";
593
594 # source package has the same name as the base package
595 # if source not explicitly mentioned by dpkg-deb
596 unless (exists $infmap{'source'}) {
597 $infmap{'source'} = $infmap{'package'};
598 }
599
600 \%infmap;
601 }
602
603 # -------------------------------------------------
604 # FUNCTION: dsc_filename DEBINFREF
605 #
606 # Returns the name of the description file
607 # derived from the information stored in DEBINFREF.
608 # -------------------------------------------------
609
610 sub dsc_filename {
611 my ($debinfref) = @_;
612
613 $debinfref->{'package'} . '_' . $debinfref->{'version'} . '.dsc';
614 }
615
616 # --------------------------------------------------------
617 # FUNCTION: pooldir NAME
618 #
619 # Returns the name of the pool directory for package NAME.
620 # --------------------------------------------------------
621
622 sub pooldir {
623 my ($name) = @_;
624
625 join('/', substr($name,0,1), $name);
626 }
627
628 # FUNCTION: cut_epoch
629 sub cut_epoch {
630 my ($version) = @_;
631 $version =~ s/^\d+://;
632 $version;
633 }
634
635 # FUNCTION: remote_perl SCRIPT REPLACEMENTS HOST USER
636
637 sub remote_perl {
638 my ($script, $repref, $host, $user) = @_;
639 my $reg;
640
641 for (keys %$repref) {
642 $reg = qr/\@$_\@/;
643 $script =~ s/$reg/$$repref{$_}/eg;
644 }
645
646 sshopen2("$user\@$host", *READER, *WRITER, "/usr/bin/perl -");
647 print WRITER $script;
648 close WRITER;
649 while (<READER>) {
650 print;
651 }
652 close READER;
653 }
654
655 # DOCUMENTATION
656 # =============
657
658 =head1 NAME
659
660 debaux-publish - Debian package publishing script
661
662 =head1 SYNOPSIS
663
664 debaux-publish [OPTIONS] DEBFILES
665
666 =head1 VERSION
667
668 0.1.4
669
670 =head1 DESCRIPTION
671
672 debaux-publish is a helper script for publishing Debian packages.
673
674 =head1 OPTIONS
675
676 =over 4
677
678 =item C<-d NAME, --distribution NAME>
679
680 Forces debaux-publish to publish the package(s) to this distribution.
681
682 =item C<-n, --dry-run>
683
684 Don't actually publish anything.
685
686 =item C<--site=NAME>
687
688 Publish Debian packages on site NAME.
689
690 =back
691
692 =head1 SEE ALSO
693
694 dpkg(8), dpkg-scanpackages(8)
695
696 =head1 AUTHOR
697
698 Stefan Hornburg (Racke) <[email protected]>.
699
700 =head1 LICENSE
701
702 debaux-publish comes with ABSOLUTELY NO WARRANTY. This is free software, and
703 you are welcome to redistribute and modify it under the terms of the
704 GNU General Public License.
705
706 =head1 COPYRIGHT
707
708 Copyright 2000,2001,2002 Stefan Hornburg (Racke) <[email protected]>.
709
710 =cut