[BACK]Return to MakeTorrents.pl CVS log [TXT][DIR] Up to [local] / openbsd / OpenBSDTorrents

Annotation of openbsd/OpenBSDTorrents/MakeTorrents.pl, Revision 1.8

1.1       andrew      1: #!/usr/bin/perl -T
1.8     ! andrew      2: #$Id: MakeTorrents.pl,v 1.7 2005/03/24 00:21:43 andrew Exp andrew $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5: use diagnostics;
                      6:
1.5       andrew      7: use BT::MetaInfo;
                      8:
1.4       andrew      9: use lib 'lib';
                     10: use OpenBSDTorrents;
                     11:
1.1       andrew     12: %ENV = ();
                     13:
                     14: use YAML;
                     15:
1.5       andrew     16: my $Piece_Length = 18;
1.8     ! andrew     17:
1.3       andrew     18: my $MinFiles = 5;
1.8     ! andrew     19: my $MinSize  = 50 * 1024 * 1024; # 50 MiB
1.3       andrew     20:
1.1       andrew     21: my $StartDir = shift || $BaseName;
                     22: $StartDir =~ s#/$##;
                     23:
                     24: chdir($BaseDir) || die "Couldn't change dir to $BaseDir";
                     25:
                     26: Process_Dir($StartDir);
                     27:
                     28: sub Process_Dir
                     29: {
                     30:        my $basedir = shift;
                     31:
                     32:        my ($dirs, $files) = Get_Files_and_Dirs($basedir);
                     33:        if (@$files) {
1.4       andrew     34:                my $torrent = Make_Torrent($basedir, $files);
1.1       andrew     35:        }
1.3       andrew     36:
                     37:        # don't recurse if we were called on a specific directory
                     38:        return 1 if $StartDir ne $BaseName;
                     39:
1.1       andrew     40:        foreach my $subdir (@$dirs) {
1.4       andrew     41:                next if $subdir eq '.';
                     42:                next if $subdir eq '..';
1.1       andrew     43:                Process_Dir("$basedir/$subdir")
                     44:        }
                     45: }
                     46:
                     47: sub Make_Torrent
                     48: {
1.4       andrew     49:         my $basedir = shift;
                     50:         my $files   = shift;
1.1       andrew     51:
1.4       andrew     52:         if ($#{ $files } < $MinFiles) {
                     53:                 print "Too few files in $basedir, skipping . . .\n";
                     54:                 return undef;
                     55:         }
1.3       andrew     56:
1.4       andrew     57:         if ($basedir !~ /\.\./ && $basedir =~ /^([\w\/\.-]+)$/) {
                     58:                 $basedir = $1;
                     59:         } else {
                     60:                 die "Invalid characters in dir '$basedir'";
                     61:         }
1.1       andrew     62:
1.4       andrew     63:         foreach (@$files) {
                     64:                 if (/^([^\/]+)$/) {
                     65:                         $_ = "$basedir/$1";
                     66:                 } else {
                     67:                         die "Invalid characters in file '$_' in '$basedir'";
                     68:                 }
                     69:         }
1.1       andrew     70:
1.4       andrew     71:         my $torrent = Name_Torrent($basedir);
1.3       andrew     72:
1.4       andrew     73:         print "Creating $torrent\n";
1.1       andrew     74:
1.4       andrew     75:         my $comment = "Files from $basedir\n" .
                     76:                       "Created by andrew fresh (andrew\@mad-techies.org)\n" .
                     77:                       "http://OpenBSD.somedomain.net/";
1.1       andrew     78:
1.7       andrew     79:        eval { btmake($torrent, $comment, $files); };
                     80:        if ($@) {
                     81:                print "Error creating $torrent\n";
                     82:        }
1.3       andrew     83:
1.5       andrew     84: #        system($BTMake,
                     85: #               '-C',
                     86: #               '-c', $comment,
                     87: #               '-n', $BaseName,
                     88: #               '-o', "$TorrentDir/$torrent",
                     89: #               '-a', $Tracker,
                     90: #               @$files
                     91: #        );# || die "Couldn't system $BTMake $torrent: $!";
                     92:
1.4       andrew     93:         return $torrent;
1.5       andrew     94: }
                     95:
                     96:
                     97: # Stole and modified from btmake to work for this.
                     98: sub btmake {
                     99:     no locale;
                    100:
                    101:     my $torrent = shift;
                    102:     my $comment = shift;
                    103:     my $files = shift;
                    104:
                    105:     my $name = $BaseName;
                    106:     my $announce = $Tracker;
                    107:     my $piece_len = 2 << ($Piece_Length - 1);
                    108:
                    109:     $torrent = "$TorrentDir/$torrent";
                    110:
                    111:     my $t = BT::MetaInfo->new();
                    112:     $t->name($name);
                    113:     $t->announce($announce);
                    114:     unless ($announce =~ m!^http://[^/]+/!i) {
                    115:         warn "  [ WARNING: announce URL does not look like: http://hostname/ ]\n";
                    116:     }
                    117:     $t->comment($comment);
                    118:     #foreach my $pair (split(/;/, $::opt_f)) {
                    119:     #    if (my($key, $val) = split(/,/, $pair, 2)) {
                    120:     #        $t->set($key, $val);
                    121:     #    }
                    122:     #}
                    123:     $t->piece_length($piece_len);
                    124:     $t->creation_date(time);
                    125:     warn "Checksumming files. This may take a little while...\n";
                    126:     $t->set_files(@$files);
1.8     ! andrew    127:
        !           128:     if ($t->total_size < $MinSize) {
        !           129:         print "Skipping smaller than minimum size\n";
        !           130:         return 0;
        !           131:     }
        !           132:
1.5       andrew    133:     $t->save("$torrent");
                    134:     print "Created: $torrent\n";
                    135:     #system("btinfo $torrent") if ($::opt_I);
1.1       andrew    136: }
1.3       andrew    137:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>