bin/import
changeset 0 0d30ea853889
equal deleted inserted replaced
-1:000000000000 0:0d30ea853889
       
     1 #! /usr/bin/perl
       
     2 # © 2006 Heiko Schlittermann
       
     3 
       
     4 use strict;
       
     5 use warnings;
       
     6 use File::Find;
       
     7 use Cwd;
       
     8 use FindBin;
       
     9 use File::Basename;
       
    10 use File::Path;
       
    11 use AppConfig;
       
    12 
       
    13 use lib "$FindBin::RealBin/../lib";
       
    14 use Debian::ChangesFile;
       
    15 
       
    16 use constant CONF => (
       
    17     { CASE => 1 },
       
    18     "force" =>	{ ARGS => "!" },
       
    19 );
       
    20 
       
    21 sub processChanges;
       
    22 
       
    23 my $TIMESTAMP = "var/.import";
       
    24 my $POOLDIR = "pool";
       
    25 my $Cf = new AppConfig CONF or die;
       
    26    $Cf->getopt or die;
       
    27 
       
    28 MAIN: {
       
    29     my $cwd = cwd();
       
    30 
       
    31     $POOLDIR = "$cwd/$POOLDIR" if substr($POOLDIR, 0, 1) ne "/";
       
    32     $TIMESTAMP = "$cwd/$TIMESTAMP" if substr($TIMESTAMP, 0, 1) ne "/";
       
    33 
       
    34     $Cf->force and unlink $TIMESTAMP;
       
    35 
       
    36 
       
    37     my $last = (stat $TIMESTAMP)[9] || 0;
       
    38     my $changed = 0;
       
    39     my $wanted = sub { processChanges($last, \$changed) };
       
    40 
       
    41 	# Two! slashes
       
    42 	find({ wanted => $wanted}, "incoming//");
       
    43 
       
    44     if ($changed) {
       
    45 	open(X, ">$TIMESTAMP");
       
    46 	print X $$;
       
    47 	close(X);
       
    48     }
       
    49 
       
    50 }
       
    51 
       
    52 sub processChanges($$) {
       
    53     my $last = shift;
       
    54     my $touched = shift;
       
    55 
       
    56     -f or return;
       
    57     /\.changes$/ or return;
       
    58     (stat $_)[9] >= $last or return;
       
    59 
       
    60     my $current = $_;
       
    61     my ($base) = $File::Find::dir =~ m!//(.*)!;
       
    62 
       
    63     print "checking $File::Find::name\n" if -t STDIN;
       
    64     my @import;
       
    65 
       
    66     my $changes = new Debian::ChangesFile($_);
       
    67 
       
    68     push @import, [$_, "$POOLDIR/$base/" . $_];
       
    69 
       
    70     foreach ($changes->binaryFiles) {
       
    71 	print "\t", $_->file, "\n" if -t STDIN;
       
    72 	$_->check;
       
    73 	push @import, [$_->file, "$POOLDIR/$base/" . $_->file];
       
    74     }
       
    75 
       
    76     foreach ($changes->sourceFiles) {
       
    77 	print "\t", $_->file, "\n" if -t STDIN;
       
    78 	$_->check;
       
    79 	push @import, [$_->file, "$POOLDIR/$base/" . $_->file];
       
    80 	my $sources = new Debian::ControlFile $_;
       
    81 	foreach ($sources->files) {
       
    82 	    print "\t\t", $_->file, "\n" if -t STDIN;
       
    83 	    $_->check();
       
    84 	push @import, [$_->file, "$POOLDIR/$base/" . $_->file];
       
    85 	}
       
    86     }
       
    87 
       
    88     print "ok ($current)\n" if -t STDIN;
       
    89 
       
    90     foreach (@import) {
       
    91 	my ($from, $to) = @$_;
       
    92 	my $dstdir = dirname($to);
       
    93 	-d $dstdir or mkpath($dstdir, 0, 0755) or die "Can't mkpath $dstdir: $!\n";
       
    94 	my $in = new IO::File $from or die "Can't open <$from: $!\n";
       
    95 	my $out = new IO::File ">$to.tmp" or die "Can't open >$to.tmp: $!\n";
       
    96 
       
    97 	local $/ = \4096;
       
    98 	$out->print($_) while <$in>;
       
    99 	$out->close;
       
   100 	$in->close;
       
   101 
       
   102 	utime((stat $from)[8, 9], "$to.tmp") or die "Can't change timestamps on $to: $!\n";
       
   103 	rename("$to.tmp", $to) or die "Can't rename $to.tmp -> $to: $!\n";
       
   104 	$$touched = 1;
       
   105     }
       
   106 
       
   107 }
       
   108 
       
   109 # vim:sts=4 sw=4 aw ai sm: