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

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

1.1       andrew      1: #!/usr/bin/perl -T
1.6     ! andrew      2: #$Id: MakeTorrents.pl,v 1.5 2005/03/23 00:08:18 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.5       andrew     77:        btmake($torrent, $comment, $files);
1.3       andrew     78:
1.5       andrew     79: #        system($BTMake,
                     80: #               '-C',
                     81: #               '-c', $comment,
                     82: #               '-n', $BaseName,
                     83: #               '-o', "$TorrentDir/$torrent",
                     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:
                    100:     my $name = $BaseName;
                    101:     my $announce = $Tracker;
                    102:     my $piece_len = 2 << ($Piece_Length - 1);
                    103:
                    104:     $torrent = "$TorrentDir/$torrent";
                    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);
                    120:     warn "Checksumming files. This may take a little while...\n";
                    121:     $t->set_files(@$files);
                    122:     $t->save("$torrent");
                    123:     print "Created: $torrent\n";
                    124:     #system("btinfo $torrent") if ($::opt_I);
1.1       andrew    125: }
1.3       andrew    126:

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