Annotation of openbsd/OpenBSDTorrents/NewTorrents.pl, Revision 1.10
1.1 andrew 1: #!/usr/bin/perl -T
1.10 ! andrew 2: #$RedRiver: NewTorrents.pl,v 1.9 2006/05/15 18:47:04 andrew Exp $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
7: use lib 'lib';
8: use OpenBSDTorrents;
9:
1.7 andrew 10: use POSIX qw / setsid :sys_wait_h /;
11: $SIG{CHLD} = \&REAPER;
12: my %Kids;
13: my %Kid_Status;
14: my %Need_Update;
1.3 andrew 15:
1.1 andrew 16: %ENV = ();
17:
18: use YAML;
19:
1.3 andrew 20:
1.1 andrew 21: my $last_dir = '';
22: while (<>) {
23: chomp;
1.10 ! andrew 24: print $_, "\n";
! 25: if (my ($message, $file) = m#(.*)\s+\`([^']+)'#) {
! 26: next if $message eq 'Making directory';
! 27:
! 28: my $dir = '';
! 29: if ($file =~ m#^(.*)/([^/]+)#) {
! 30: ($dir, $file) = ($1, $2);
! 31: }
! 32: #print "$message - $dir - $file\n";
1.1 andrew 33: if ($last_dir && $last_dir ne $dir) {
1.2 andrew 34: StartTorrent($last_dir);
1.1 andrew 35: }
36: $last_dir = $dir;
37: }
1.2 andrew 38: }
1.7 andrew 39:
40: # Regen just the new ones now
41: sleep(1) while (keys %Kids > 0);
1.2 andrew 42: StartTorrent($last_dir);
43:
1.7 andrew 44: # after the new ones are done, regen all, just to make sure
45: sleep(1) while (keys %Kids > 0);
1.3 andrew 46: StartTorrent('skip');
47:
1.7 andrew 48: sub REAPER {
49: my $child;
50: while (($child = waitpid(-1,WNOHANG)) > 0) {
51: $Kid_Status{$child} = $?;
52: delete $Kids{$child};
53: }
54: $SIG{CHLD} = \&REAPER; # still loathe sysV
55: }
1.3 andrew 56:
1.2 andrew 57: sub StartTorrent
58: {
59: my $dir = shift;
1.3 andrew 60: return undef unless $dir;
1.2 andrew 61:
1.7 andrew 62: my $should_fork = 1;
63:
64: if ($dir eq 'skip') {
1.8 andrew 65: #$dir = '';
1.7 andrew 66: %Need_Update = ();
67: $should_fork = 0;
68: } else {
1.6 andrew 69: $dir = $OBT->{BASENAME} . "/$dir";
1.7 andrew 70: $Need_Update{$dir} = 1;
1.3 andrew 71: }
72:
1.7 andrew 73: if (keys %Kids > 0) {
74: print "Not making torrents for $dir now, already running\n";
75: return undef;
76: }
1.3 andrew 77:
1.7 andrew 78: my @now_update = keys %Need_Update;
79: %Need_Update = ();
1.3 andrew 80:
1.7 andrew 81: if ($should_fork) {
82: defined(my $pid = fork) or die "Can't fork: $!";
1.3 andrew 83:
1.7 andrew 84: if ($pid) {
85: $Kids{$pid} = 1;
86: return undef;
87: }
1.3 andrew 88:
1.7 andrew 89: }
90:
91: if (@now_update) {
92: print "Making torrents for ", join(" ", @now_update), "\n";
93: } else {
94: print "Remaking all torrents\n";
1.8 andrew 95: push @now_update, $dir;
1.7 andrew 96: }
97: exec($OBT->{DIR_HOME} . '/regen.sh', @now_update);
1.1 andrew 98: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>