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

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

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

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