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

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

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

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