version 1.3, 2005/03/22 23:07:23 |
version 1.26, 2010/03/22 21:16:02 |
|
|
#!/usr/bin/perl -T |
#!/usr/bin/perl |
#$Id$ |
# -T |
|
#$RedRiver: MakeTorrents.pl,v 1.25 2010/03/22 20:15:06 andrew Exp $ |
use strict; |
use strict; |
use warnings; |
use warnings; |
use diagnostics; |
use diagnostics; |
|
|
|
use lib 'lib'; |
|
use BT::MetaInfo::Cached; |
|
use OpenBSDTorrents; |
|
|
%ENV = (); |
%ENV = (); |
|
|
use YAML; |
chdir( $OBT->{DIR_FTP} ) |
|
|| die "Couldn't change dir to " . $OBT->{DIR_FTP} . ": $!"; |
|
|
my $BaseDir = '/home/ftp/pub'; |
my $StartDir = ''; |
my $BaseName = 'OpenBSD'; |
if (@ARGV) { |
my $OutDir = '/home/andrew/torrents'; |
foreach (@ARGV) { |
my $BTMake = '/usr/local/bin/btmake'; |
s#/$##; |
my $Tracker = 'http://OpenBSD.somedomain.net/announce.php'; |
Process_Dir($_); |
|
} |
|
} |
|
else { |
|
$StartDir = $OBT->{BASENAME}; |
|
Process_Dir($StartDir); |
|
} |
|
|
my $MinFiles = 5; |
sub Process_Dir { |
|
my $basedir = shift; |
|
|
# These are regexes that tell what files to skip; |
#return undef if $basedir =~ /packages/; |
my $SkipDirs = qr/\/patches$/; |
|
my $SkipFiles = qr/^\./; |
|
|
|
|
my ( $dirs, $files ) = Get_Files_and_Dirs($basedir); |
|
if (@$files) { |
|
Make_Torrent( $basedir, $files ); |
|
} |
|
|
my $StartDir = shift || $BaseName; |
# don't recurse if we were started with a specific directory |
$StartDir =~ s#/$##; |
return 1 if $StartDir ne $OBT->{BASENAME}; |
|
|
chdir($BaseDir) || die "Couldn't change dir to $BaseDir"; |
foreach my $subdir (@$dirs) { |
|
next if $subdir =~ /^\./; |
|
Process_Dir("$basedir/$subdir"); |
|
} |
|
} |
|
|
Process_Dir($StartDir); |
sub Make_Torrent { |
|
my $basedir = shift; |
|
my $files = shift; |
|
|
sub Process_Dir |
if ( $basedir !~ /\.\./ && $basedir =~ /^([\w\/\.-]+)$/ ) { |
{ |
$basedir = $1; |
my $basedir = shift; |
} |
|
else { |
|
die "Invalid characters in dir '$basedir'"; |
|
} |
|
|
my ($dirs, $files) = Get_Files_and_Dirs($basedir); |
if ( $#{$files} < $OBT->{MIN_FILES} ) { |
if (@$files) { |
print "Too few files in $basedir, skipping . . .\n"; |
Make_Torrent($basedir, $files); |
return undef; |
} |
} |
|
|
# don't recurse if we were called on a specific directory |
my $torrent = Name_Torrent($basedir); |
return 1 if $StartDir ne $BaseName; |
my $comment = "Files from $basedir"; |
|
|
foreach my $subdir (@$dirs) { |
my %torrents; |
#next if $subdir eq '.'; |
foreach my $file (@$files) { |
#next if $subdir eq '..'; |
if ( $file =~ /^([^\/]+)$/ ) { |
Process_Dir("$basedir/$subdir") |
$file = $1; |
} |
|
} |
|
|
|
sub Make_Torrent |
my $t = $torrent; |
{ |
my $c = $comment; |
my $basedir = shift; |
|
my $files = shift; |
|
|
|
if ($#files < $MinFiles) { |
if ( $file =~ /$INSTALL_ISO_REGEX/xms ) { |
print "Too few files in $basedir, skipping . . .\n"; |
$t = Name_Torrent("$basedir/$file"); |
return undef; |
$c = "$basedir/$file"; |
} |
} |
|
elsif ( my ($ext) = $file =~ /$SONG_REGEX/xms ) { |
|
$t = Name_Torrent("$basedir/$ext"); |
|
$c = "$ext files from $basedir"; |
|
} |
|
|
if ($basedir !~ /\.\./ && $basedir =~ /^([\w\/\.-]+)$/) { |
$torrents{$t}{comment} = $c; |
$basedir = $1; |
push @{ $torrents{$t}{files} }, "$basedir/$file"; |
} else { |
} |
die "Invalid characters in dir '$basedir'"; |
else { |
} |
die "Invalid characters in file '$file' in '$basedir'"; |
|
} |
|
} |
|
|
foreach (@$files) { |
foreach my $t ( keys %torrents ) { |
if (/^([^\/]+)$/) { |
|
$_ = "$basedir/$1"; |
|
} else { |
|
die "Invalid characters in file '$_' in '$basedir'"; |
|
} |
|
} |
|
|
|
my $date = Torrent_Date(); |
print "Creating $t (" |
|
. ( scalar @{ $torrents{$t}{files} } ) |
|
. " files)\n"; |
|
|
my $torrent = $basedir; |
my $c = $torrents{$t}{comment}; |
$torrent =~ s/\W/_/g; |
$c .= "\nCreated by andrew fresh (andrew\@afresh1.com)\n" |
$torrent .= '-' . $date; |
. "http://OpenBSD.somedomain.net/"; |
$torrent .= '.torrent'; |
|
|
|
#print Dump $torrent, $basedir, $files; |
eval { btmake( $t, $c, $torrents{$t}{files} ); }; |
print "Creating $torrent\n"; |
if ($@) { |
|
print "Error creating $t\n$@\n"; |
|
} |
|
|
my $comment = "Files from $basedir\n" . |
# system($BTMake, |
"Created by andrew fresh (andrew\@mad-techies.org)\n" . |
# '-C', |
"http://OpenBSD.somedomain.net/", |
# '-c', $comment, |
|
# '-n', $OBT->{BASENAME}, |
|
# '-o', $OBT->{DIR_TORRENT} . "/$t", |
|
# '-a', $Tracker, |
|
# @$files |
|
# );# || die "Couldn't system $BTMake $t: $!"; |
|
} |
|
|
system($BTMake, |
return [ keys %torrents ]; |
'-C', |
|
'-c', $comment, |
|
'-n', $BaseName, |
|
'-o', "$OutDir/$torrent", |
|
'-a', $Tracker, |
|
@$files |
|
);# || die "Couldn't system $BTMake $torrent: $!"; |
|
} |
} |
|
|
|
# Stole and modified from btmake to work for this. |
|
sub btmake { |
|
no locale; |
|
|
sub Get_Files_and_Dirs |
my $torrent = shift; |
{ |
my $comment = shift; |
my $basedir = shift; |
my $files = shift; |
opendir DIR, $basedir or die "Couldn't opendir $basedir: $!"; |
|
my @contents = grep { ! /^\.\.$/ } grep { ! /^\.$/ } readdir DIR; |
|
closedir DIR; |
|
my @dirs = grep { -d "$basedir/$_" } @contents; |
|
|
|
my %dirs; # lookup table |
my $name = $OBT->{BASENAME}; |
my @files;# answer |
my $announce = $OBT->{URL_TRACKER}; |
|
my $piece_len = 2 << ( $OBT->{PIECE_LENGTH} - 1 ); |
|
|
# build lookup table |
my $torrent_with_path = $OBT->{DIR_NEW_TORRENT} . "/$torrent"; |
@dirs{@dirs} = (); |
|
|
|
foreach my $item (@contents) { |
#if (@$files == 1) { |
push(@files, $item) unless exists $dirs{$item}; |
#$name = $files->[0]; |
} |
#} |
|
|
@dirs = grep { ! /$SkipDirs/ } @dirs if $SkipDirs; |
my $t |
@files = grep { ! /$SkipFiles/ } @files if $SkipFiles; |
= BT::MetaInfo::Cached->new( { cache_root => '/tmp/OBTFileCache' } ); |
|
|
return \@dirs, \@files; |
$t->name($name); |
} |
$t->announce($announce); |
|
unless ( $announce =~ m!^http://[^/]+/!i ) { |
|
warn |
|
" [ WARNING: announce URL does not look like: http://hostname/ ]\n"; |
|
} |
|
$t->comment($comment); |
|
|
sub Torrent_Date |
#foreach my $pair (split(/;/, $::opt_f)) { |
{ |
# if (my($key, $val) = split(/,/, $pair, 2)) { |
my ($min, $hour, $mday, $mon, $year) = (gmtime)[1..5]; |
# $t->set($key, $val); |
$mon++; |
# } |
$year += 1900; |
#} |
foreach ($min, $hour, $mday, $mon) { |
$t->piece_length($piece_len); |
if (length $_ == 1) { |
$t->creation_date(time); |
$_ = '0' . $_; |
|
} |
#print "Checksumming files. This may take a little while...\n"; |
} |
|
return join '-', ($year, $mon, $mday, $hour . $min); |
# Can't use this, have to do this manually because |
|
# we need to have the multi-file type of torrent |
|
# even when we have only one file. |
|
#$t->set_files(@$files); |
|
|
|
my @file_list; |
|
foreach my $f (@$files) { |
|
my $l = ( stat("$OBT->{DIR_FTP}/$f") )[7]; |
|
my @p = split /\//, $f; |
|
shift @p; |
|
push @file_list, |
|
{ |
|
length => $l, |
|
path => \@p, |
|
}; |
|
} |
|
$t->files( \@file_list ); |
|
$t->make_pieces(@$files); |
|
|
|
if ( $t->total_size < $OBT->{MIN_SIZE} ) { |
|
print "Skipping smaller than minimum size\n"; |
|
return 0; |
|
} |
|
|
|
my $hash = $t->info_hash; |
|
$hash = unpack( "H*", $hash ); |
|
|
|
$t->save($torrent_with_path); |
|
print "Created: $torrent_with_path\n"; |
} |
} |
|
|