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

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

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

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