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

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

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

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