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

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

1.1       andrew      1: #!/usr/bin/perl -T
1.12    ! andrew      2: #$Id: MakeTorrents.pl,v 1.11 2005/04/06 23:10:06 andrew Exp andrew $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5: use diagnostics;
                      6:
1.4       andrew      7: use lib 'lib';
1.12    ! andrew      8: use BT::OBTMetaInfo;
1.4       andrew      9: use OpenBSDTorrents;
                     10:
1.1       andrew     11: %ENV = ();
                     12:
1.11      andrew     13: chdir($OBT->{DIR_FTP}) || die "Couldn't change dir to " . $OBT->{DIR_FTP} . ": $!";
1.1       andrew     14:
1.11      andrew     15: my $StartDir = '';
                     16: if (@ARGV) {
                     17:        foreach (@ARGV) {
                     18:                s#/$##;
                     19:                Process_Dir($_);
                     20:        }
                     21: } else {
                     22:        $StartDir = $OBT->{BASENAME};
                     23:        Process_Dir($StartDir);
                     24: }
1.1       andrew     25:
                     26:
                     27:
                     28: sub Process_Dir
                     29: {
                     30:        my $basedir = shift;
                     31:
1.12    ! andrew     32:        return undef if $basedir =~ /packages/;
        !            33:
1.1       andrew     34:        my ($dirs, $files) = Get_Files_and_Dirs($basedir);
                     35:        if (@$files) {
1.4       andrew     36:                my $torrent = Make_Torrent($basedir, $files);
1.1       andrew     37:        }
1.3       andrew     38:
1.12    ! andrew     39:        # don't recurse if we were started with a specific directory
1.10      andrew     40:        return 1 if $StartDir ne $OBT->{BASENAME};
1.3       andrew     41:
1.1       andrew     42:        foreach my $subdir (@$dirs) {
1.4       andrew     43:                next if $subdir eq '.';
                     44:                next if $subdir eq '..';
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.12    ! andrew    113:     my $t = BT::OBTMetaInfo->new();
1.5       andrew    114:     $t->name($name);
                    115:     $t->announce($announce);
                    116:     unless ($announce =~ m!^http://[^/]+/!i) {
                    117:         warn "  [ WARNING: announce URL does not look like: http://hostname/ ]\n";
                    118:     }
                    119:     $t->comment($comment);
                    120:     #foreach my $pair (split(/;/, $::opt_f)) {
                    121:     #    if (my($key, $val) = split(/,/, $pair, 2)) {
                    122:     #        $t->set($key, $val);
                    123:     #    }
                    124:     #}
                    125:     $t->piece_length($piece_len);
                    126:     $t->creation_date(time);
1.9       andrew    127:     print "Checksumming files. This may take a little while...\n";
1.5       andrew    128:     $t->set_files(@$files);
1.8       andrew    129:
1.10      andrew    130:     if ($t->total_size < $OBT->{MIN_SIZE}) {
1.8       andrew    131:         print "Skipping smaller than minimum size\n";
                    132:         return 0;
                    133:     }
                    134:
1.12    ! andrew    135:     my $hash = $t->info_hash_cached($torrent_with_path);
        !           136:     $hash = unpack("H*", $hash);
        !           137:
        !           138:     $t->save($torrent_with_path);
        !           139:     print "Created: $torrent_with_path\n";
1.1       andrew    140: }
1.3       andrew    141:

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