Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.36
1.1 andrew 1: #!/usr/bin/perl -T
1.36 ! andrew 2: #$RedRiver: CurrentTorrents.pl,v 1.35 2010/03/08 20:19:35 andrew Exp $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
1.2 andrew 7: use Time::Local;
1.16 andrew 8: use Fcntl ':flock';
1.23 andrew 9: use File::Basename;
1.27 andrew 10:
1.29 andrew 11: use Transmission::Client;
12: use Transmission::Utils;
13:
1.23 andrew 14: #use YAML;
1.1 andrew 15:
1.9 andrew 16: use lib 'lib';
1.1 andrew 17: use OpenBSDTorrents;
1.17 andrew 18: use BT::MetaInfo::Cached;
1.1 andrew 19:
20: %ENV = ();
21:
1.15 andrew 22: #justme();
1.1 andrew 23:
1.12 andrew 24: my $Name_Filter = shift || '';
1.27 andrew 25: if ( $Name_Filter =~ /^(\w*)$/ ) {
26: $Name_Filter = $1;
27: }
28: else {
29: die "Invalid filter: $Name_Filter";
1.12 andrew 30: }
31:
1.15 andrew 32: my %Possible_Torrents;
1.27 andrew 33: Process_Dir( $OBT->{DIR_FTP} );
1.15 andrew 34:
1.2 andrew 35: my %files;
1.15 andrew 36: my @delete;
1.27 andrew 37: foreach my $DIR ( $OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT} ) {
38: opendir DIR, $DIR
39: or die "Couldn't opendir $DIR: $!";
40: foreach ( readdir DIR ) {
41: next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/;
42:
43: if (/^([^\/]+)$/) {
44: $_ = $1;
45: }
46: else {
47: die "Invalid character in $_: $!";
48: }
49: my $epoch = 0;
50: my $name = basename( $_, '.torrent' );
51:
52: if ( my ( $base, $year, $mon, $mday, $hour, $min )
53: = /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/ )
54: {
55:
56: $mon--;
57: $epoch = timegm( 0, $min, $hour, $mday, $mon, $year );
58: $name = $base;
59: }
60:
1.36 ! andrew 61: #print "Adding $DIR/$_\n";
! 62:
! 63: if (exists $files{$ext}{$name}{$epoch}) {
! 64: warn "Multiple torrents with $name and epoch $epoch\n";
! 65: }
1.27 andrew 66:
67: $files{$ext}{$name}{$epoch} = {
68: file => $_,
69: dir => $DIR,
1.35 andrew 70: #path => "$DIR/$_",
1.27 andrew 71: ext => $ext,
72:
73: #year => $year,
74: #mon => $mon,
75: #mday => $mday,
76: #hour => $hour,
77: #min => $min,
78: name => $name,
79: epoch => $epoch,
80: };
81:
82: if ( $name =~ m/\A $OBT->{BASENAME} /xms
83: && !exists $Possible_Torrents{$name} )
84: {
1.31 andrew 85: #print "Would remove $_\n";
1.27 andrew 86: push @delete, $files{$ext}{$name}{$epoch};
87: }
88: }
89: closedir DIR;
1.2 andrew 90: }
91:
1.25 andrew 92: #print Dump \%files;
1.28 andrew 93:
94: my %keep;
1.29 andrew 95: my %seen;
1.27 andrew 96: foreach my $name ( sort keys %{ $files{torrent} } ) {
97: next unless $name =~ /^$Name_Filter/;
98:
1.31 andrew 99: #next if $name !~ /songs/xms;
1.29 andrew 100: #next if $name =~ /_packages_/xms;
1.27 andrew 101: #print "Checking $name\n";
102:
1.29 andrew 103: my $cn = $files{torrent}{$name};
1.27 andrew 104:
1.31 andrew 105: EPOCH: foreach my $epoch ( sort { $b <=> $a } keys %{$cn} ) {
1.29 andrew 106: my $ct = $cn->{$epoch};
1.35 andrew 107: my $cf = $ct->{dir} . '/' . $ct->{file};
1.27 andrew 108:
1.29 andrew 109: #print "\t$epoch - $cf\n";
1.27 andrew 110:
111: my $t;
112: eval {
1.29 andrew 113: $t
114: = BT::MetaInfo::Cached->new( $cf,
115: { cache_root => '/tmp/OBTFileCache' } );
1.27 andrew 116: };
117:
118: if ($@) {
1.29 andrew 119: warn "Error reading torrent $cf\n";
120: push @delete, $ct;
121: next EPOCH;
1.27 andrew 122: }
123:
1.29 andrew 124: $ct->{comment} = $t->{comment};
1.27 andrew 125: my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s;
126:
1.29 andrew 127: if ( !-e $OBT->{DIR_FTP} . "/$path" ) {
1.27 andrew 128: print
1.29 andrew 129: 'Deleting ',
130: $cn->{$epoch}{file}, ' the path (', $path,
131: ") doesn't exist.\n";
132: push @delete, $ct;
133: next EPOCH;
1.27 andrew 134: }
135:
1.29 andrew 136: my $hash = unpack( "H*", $t->info_hash );
137: $ct->{info_hash} = $hash;
1.27 andrew 138:
139: undef $t;
140:
1.29 andrew 141: if ( $seen{$name} && $seen{$name} ne $hash ) {
1.31 andrew 142: print "Removing older [$name] [$hash]\n\t",
1.35 andrew 143: $cf,
1.31 andrew 144: "\n";
145: $ct->{reason} = 'older';
1.29 andrew 146: push @delete, $ct;
147: next EPOCH;
148: }
1.31 andrew 149: elsif ( keys %{$cn} == 1 && $ct->{dir} eq $OBT->{DIR_TORRENT} ) {
150: $ct->{reason} = 'only';
1.29 andrew 151: }
152: elsif ( $keep{$hash} ) {
1.28 andrew 153: if ( $keep{$hash}{epoch} == $epoch ) {
1.29 andrew 154: next EPOCH;
1.27 andrew 155: }
1.29 andrew 156:
157: print "Removing duplicate [$name] [$hash]\n\t",
1.35 andrew 158: $keep{$hash}{file}, "\n";
1.31 andrew 159:
160: $keep{$hash}{reason} = 'duplicate';
161: $ct->{reason} = 'duplicate';
162:
1.28 andrew 163: push @delete, $keep{$hash};
164: }
165: else {
1.31 andrew 166: $ct->{reason} = 'first';
167: }
1.2 andrew 168:
1.31 andrew 169: $keep{$hash} = $ct;
170: $seen{$name} = $hash;
1.27 andrew 171: }
1.2 andrew 172: }
173:
174: #print Dump \%files, \%keep, \@delete;
1.31 andrew 175: #print Dump \%keep, \@delete;
1.23 andrew 176: #exit;
1.2 andrew 177:
1.29 andrew 178: my $client = Transmission::Client->new;
179: my %seeding;
180: foreach my $torrent ( @{ $client->torrents } ) {
181:
182: #my $status = Transmission::Utils::from_numeric_status($torrent->status);
183: my $hash = $torrent->hash_string;
184: if ( exists $keep{$hash} ) {
185: $seeding{$hash} = $torrent;
186: }
187: else {
188: print "No longer seeding [$hash]\n";
189: $torrent->stop or warn $torrent->error_string;
190: $client->remove( $torrent->id ) or warn $client->error;
1.27 andrew 191: }
1.13 andrew 192: }
193:
1.27 andrew 194: #print Dump \%keep;
1.28 andrew 195: foreach my $hash ( keys %keep ) {
196: my $file = $keep{$hash}{file} || q{};
197: my $dir = $keep{$hash}{dir} || q{};
1.31 andrew 198:
199: my $name = $keep{$hash}{name};
200: my $epoch = $keep{$hash}{epoch};
1.33 andrew 201: my $reason = $keep{$hash}{reason} ? $keep{$hash}{reason} . q{ } : q{};
1.31 andrew 202:
1.32 andrew 203: #if ($reason && $reason ne 'only') {
204: # print "Keeping $reason instance of [$file] [$hash]\n",
205: # "\t", $file, "\n";
206: #}
1.31 andrew 207:
1.28 andrew 208: if ( $dir eq $OBT->{DIR_NEW_TORRENT} ) {
209: print "Moving $file to current torrents\n";
210: rename( "$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file )
211: or die "Couldn't rename '$file': $!";
212:
1.29 andrew 213: $dir = $OBT->{DIR_TORRENT};
1.35 andrew 214: $keep{$hash}{dir} = $dir;
1.28 andrew 215:
216: if ( exists $files{txt}{$name}{$epoch} ) {
217: my $m_file = $files{txt}{$name}{$epoch}{file};
218: my $m_dir = $files{txt}{$name}{$epoch}{dir};
219: rename( "$m_dir/$m_file", $OBT->{DIR_TORRENT} . "/" . $m_file )
220: or die "Couldn't rename '$m_file': $!";
1.35 andrew 221: $files{txt}{$name}{$epoch}{dir} = $OBT->{DIR_TORRENT};
1.27 andrew 222: }
223: }
1.29 andrew 224:
225: if ( !$seeding{$hash} ) {
1.34 andrew 226: print 'Starting seed of ' . $reason . "[$file] [$hash]\n";
1.29 andrew 227: if (!$client->add(
228: filename => "$dir/$file",
229: download_dir => $OBT->{DIR_FTP},
230: )
231: )
232: {
233:
234: #warn $client->error, ": $dir/$file\n";
1.35 andrew 235: print "Removing invalid torrent\n\t", $keep{$hash}{file}, "\n";
1.29 andrew 236: push @delete, $keep{$hash};
237: }
238: }
239: }
240:
241: foreach (@delete) {
1.35 andrew 242: my $path = $_->{dir} . '/' . $_->{file};
243: if ( -e $path ) {
244: print "Deleting '$path'\n";
245: unlink $path or die "Couldn't delete $path";
1.31 andrew 246: delete $files{torrent}{ $_->{name} }{ $_->{epoch} };
1.29 andrew 247: }
248: else {
249: use Data::Dumper;
250: print Dumper $_;
251: }
252: }
253:
254: foreach my $name ( keys %{ $files{ $OBT->{META_EXT} } } ) {
255: foreach my $epoch ( keys %{ $files{ $OBT->{META_EXT} }{$name} } ) {
256: unless ( exists $files{torrent}{$name}{$epoch} ) {
1.35 andrew 257: my $path = $files{ $OBT->{META_EXT} }{$name}{$epoch}{dir}
258: . '/'
259: . $files{ $OBT->{META_EXT} }{$name}{$epoch}{file};
260:
1.29 andrew 261: print "Unlinking '$path'\n";
262: unlink $path or die "couldn't unlink '$path': $!";
263: }
264: }
1.13 andrew 265: }
1.29 andrew 266:
267: $client->start;
1.13 andrew 268:
1.27 andrew 269: sub Process_Dir {
270: my $basedir = shift;
1.13 andrew 271:
1.27 andrew 272: my ( $dirs, $files ) = Get_Files_and_Dirs($basedir);
273: if (@$files) {
274: my $dir = $basedir;
275: $dir =~ s/^$OBT->{DIR_FTP}\///;
1.31 andrew 276: Make_Possible($dir);
1.27 andrew 277: foreach my $file (@$files) {
278: if ( $file =~ /$INSTALL_ISO_REGEX/ ) {
1.31 andrew 279: Make_Possible("$dir/$file");
1.27 andrew 280: }
1.15 andrew 281: }
1.27 andrew 282: }
283:
284: foreach my $subdir (@$dirs) {
285: next if $subdir eq '.';
286: next if $subdir eq '..';
287: Process_Dir("$basedir/$subdir");
288: }
1.15 andrew 289: }
290:
1.31 andrew 291: sub Make_Possible {
292: my ($path) = @_;
293:
294: my $torrent = Name_Torrent($path);
295: $torrent =~ s/-.*$//;
296: $Possible_Torrents{$torrent} = 1;
297:
298: return $torrent;
299: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>