Annotation of openbsd/OpenBSDTorrents/MakeTorrents.pl, Revision 1.15
1.1 andrew 1: #!/usr/bin/perl -T
1.15 ! andrew 2: #$Id: MakeTorrents.pl,v 1.14 2005/05/05 21:45:03 andrew Exp andrew $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
1.4 andrew 7: use lib 'lib';
1.14 andrew 8: use BT::MetaInfo::Cached;
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.13 andrew 32: #return undef if $basedir =~ /packages/;
1.12 andrew 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.15 ! andrew 113: $t = BT::MetaInfo::Cached->new(
! 114: $torrent,
! 115: {
! 116: cache_root =>
! 117: $OBT->{DIR_HOME} . '/FileCache'
! 118: }
! 119: );
! 120:
1.5 andrew 121: $t->name($name);
122: $t->announce($announce);
123: unless ($announce =~ m!^http://[^/]+/!i) {
124: warn " [ WARNING: announce URL does not look like: http://hostname/ ]\n";
125: }
126: $t->comment($comment);
127: #foreach my $pair (split(/;/, $::opt_f)) {
128: # if (my($key, $val) = split(/,/, $pair, 2)) {
129: # $t->set($key, $val);
130: # }
131: #}
132: $t->piece_length($piece_len);
133: $t->creation_date(time);
1.9 andrew 134: print "Checksumming files. This may take a little while...\n";
1.5 andrew 135: $t->set_files(@$files);
1.8 andrew 136:
1.10 andrew 137: if ($t->total_size < $OBT->{MIN_SIZE}) {
1.8 andrew 138: print "Skipping smaller than minimum size\n";
139: return 0;
140: }
141:
1.14 andrew 142: my $hash = $t->info_hash;
1.12 andrew 143: $hash = unpack("H*", $hash);
144:
145: $t->save($torrent_with_path);
146: print "Created: $torrent_with_path\n";
1.1 andrew 147: }
1.3 andrew 148:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>