| version 1.1, 2005/03/22 01:14:11 |
version 1.6, 2005/03/23 00:14:48 |
|
|
| #!/usr/bin/perl -T |
#!/usr/bin/perl -T |
| |
#$Id$ |
| use strict; |
use strict; |
| use warnings; |
use warnings; |
| use diagnostics; |
use diagnostics; |
| |
|
| |
use BT::MetaInfo; |
| |
|
| |
use lib 'lib'; |
| |
use OpenBSDTorrents; |
| |
|
| %ENV = (); |
%ENV = (); |
| |
|
| use YAML; |
use YAML; |
| |
|
| my $BaseDir = '/home/ftp/pub'; |
my $Piece_Length = 18; |
| my $BaseName = 'OpenBSD'; |
my $MinFiles = 5; |
| my $OutDir = '/home/andrew'; |
|
| my $BTMake = '/usr/local/bin/btmake'; |
|
| my $Tracker = 'http://OpenBSD.somedomain.net/announce.php'; |
|
| |
|
| # These are regexes that tell what files to skip; |
|
| my $SkipDirs; |
|
| my $SkipFiles = qr/^\./; |
|
| |
|
| my $StartDir = shift || $BaseName; |
my $StartDir = shift || $BaseName; |
| $StartDir =~ s#/$##; |
$StartDir =~ s#/$##; |
| |
|
|
|
| |
|
| my ($dirs, $files) = Get_Files_and_Dirs($basedir); |
my ($dirs, $files) = Get_Files_and_Dirs($basedir); |
| if (@$files) { |
if (@$files) { |
| Make_Torrent($basedir, $files); |
my $torrent = Make_Torrent($basedir, $files); |
| } |
} |
| |
|
| |
# don't recurse if we were called on a specific directory |
| |
return 1 if $StartDir ne $BaseName; |
| |
|
| foreach my $subdir (@$dirs) { |
foreach my $subdir (@$dirs) { |
| #next if $subdir eq '.'; |
next if $subdir eq '.'; |
| #next if $subdir eq '..'; |
next if $subdir eq '..'; |
| Process_Dir("$basedir/$subdir") |
Process_Dir("$basedir/$subdir") |
| } |
} |
| } |
} |
| |
|
| sub Make_Torrent |
sub Make_Torrent |
| { |
{ |
| my $basedir = shift; |
my $basedir = shift; |
| my $files = shift; |
my $files = shift; |
| |
|
| if ($basedir =~ /^([\w\/\.-]+)$/) { |
if ($#{ $files } < $MinFiles) { |
| $basedir = $1; |
print "Too few files in $basedir, skipping . . .\n"; |
| } else { |
return undef; |
| die "Invalid characters in dir '$basedir'"; |
} |
| } |
|
| |
|
| foreach (@$files) { |
if ($basedir !~ /\.\./ && $basedir =~ /^([\w\/\.-]+)$/) { |
| if (/^([\w\.-]+)$/) { |
$basedir = $1; |
| $_ = "$basedir/$1"; |
} else { |
| } else { |
die "Invalid characters in dir '$basedir'"; |
| die "Invalid characters in file '$_' in '$basedir'"; |
} |
| } |
|
| } |
|
| |
|
| my $torrent = $basedir; |
foreach (@$files) { |
| $torrent =~ s/\W/_/g; |
if (/^([^\/]+)$/) { |
| $torrent .= '-' . Torrent_Date(); |
$_ = "$basedir/$1"; |
| $torrent .= '.torrent'; |
} else { |
| |
die "Invalid characters in file '$_' in '$basedir'"; |
| |
} |
| |
} |
| |
|
| print Dump $torrent, $basedir, $files; |
my $torrent = Name_Torrent($basedir); |
| print "Creating $torrent\n"; |
|
| |
|
| system($BTMake, |
print "Creating $torrent\n"; |
| '-n', $BaseName, |
|
| '-o', "$OutDir/$torrent", |
my $comment = "Files from $basedir\n" . |
| '-a', $Tracker, |
"Created by andrew fresh (andrew\@mad-techies.org)\n" . |
| @$files |
"http://OpenBSD.somedomain.net/"; |
| );# || die "Couldn't system $BTMake $torrent: $!"; |
|
| |
btmake($torrent, $comment, $files); |
| |
|
| |
# system($BTMake, |
| |
# '-C', |
| |
# '-c', $comment, |
| |
# '-n', $BaseName, |
| |
# '-o', "$TorrentDir/$torrent", |
| |
# '-a', $Tracker, |
| |
# @$files |
| |
# );# || die "Couldn't system $BTMake $torrent: $!"; |
| |
|
| |
return $torrent; |
| } |
} |
| |
|
| sub Get_Files_and_Dirs |
|
| { |
|
| my $basedir = 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 |
# Stole and modified from btmake to work for this. |
| my @files;# answer |
sub btmake { |
| |
no locale; |
| |
|
| # build lookup table |
my $torrent = shift; |
| @dirs{@dirs} = (); |
my $comment = shift; |
| |
my $files = shift; |
| |
|
| foreach my $item (@contents) { |
my $name = $BaseName; |
| push(@files, $item) unless exists $dirs{$item}; |
my $announce = $Tracker; |
| } |
my $piece_len = 2 << ($Piece_Length - 1); |
| |
|
| @dirs = grep { ! /$SkipDirs/ } @dirs if $SkipDirs; |
$torrent = "$TorrentDir/$torrent"; |
| @files = grep { ! /$SkipFiles/ } @files if $SkipFiles; |
|
| |
|
| return \@dirs, \@files; |
my $t = BT::MetaInfo->new(); |
| |
$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); |
| |
#foreach my $pair (split(/;/, $::opt_f)) { |
| |
# if (my($key, $val) = split(/,/, $pair, 2)) { |
| |
# $t->set($key, $val); |
| |
# } |
| |
#} |
| |
$t->piece_length($piece_len); |
| |
$t->creation_date(time); |
| |
warn "Checksumming files. This may take a little while...\n"; |
| |
$t->set_files(@$files); |
| |
$t->save("$torrent"); |
| |
print "Created: $torrent\n"; |
| |
#system("btinfo $torrent") if ($::opt_I); |
| } |
} |
| |
|
| sub Torrent_Date |
|
| { |
|
| my ($min, $hour, $mday, $mon, $year) = (gmtime)[1..5]; |
|
| $mon++; |
|
| $year += 1900; |
|
| foreach ($min, $hour, $mday, $mon) { |
|
| if (length $_ == 1) { |
|
| $_ = '0' . $_; |
|
| } |
|
| } |
|
| return join '-', ($year, $mon, $mday, $hour . $min); |
|
| } |
|