Annotation of openbsd/OpenBSDTorrents/MakeTorrents.pl, Revision 1.20
1.20 ! andrew 1: #!/usr/bin/perl
! 2: # -T
! 3: #$RedRiver: MakeTorrents.pl,v 1.19 2006/05/15 18:47:04 andrew Exp $
1.1 andrew 4: use strict;
5: use warnings;
6: use diagnostics;
7:
1.4 andrew 8: use lib 'lib';
1.14 andrew 9: use BT::MetaInfo::Cached;
1.4 andrew 10: use OpenBSDTorrents;
11:
1.1 andrew 12: %ENV = ();
13:
1.11 andrew 14: chdir($OBT->{DIR_FTP}) || die "Couldn't change dir to " . $OBT->{DIR_FTP} . ": $!";
1.1 andrew 15:
1.11 andrew 16: my $StartDir = '';
17: if (@ARGV) {
18: foreach (@ARGV) {
19: s#/$##;
20: Process_Dir($_);
21: }
22: } else {
23: $StartDir = $OBT->{BASENAME};
24: Process_Dir($StartDir);
25: }
1.1 andrew 26:
27:
28:
29: sub Process_Dir
30: {
31: my $basedir = shift;
32:
1.13 andrew 33: #return undef if $basedir =~ /packages/;
1.12 andrew 34:
1.1 andrew 35: my ($dirs, $files) = Get_Files_and_Dirs($basedir);
36: if (@$files) {
1.4 andrew 37: my $torrent = Make_Torrent($basedir, $files);
1.1 andrew 38: }
1.3 andrew 39:
1.12 andrew 40: # don't recurse if we were started with a specific directory
1.10 andrew 41: return 1 if $StartDir ne $OBT->{BASENAME};
1.3 andrew 42:
1.1 andrew 43: foreach my $subdir (@$dirs) {
1.4 andrew 44: next if $subdir eq '.';
45: next if $subdir eq '..';
1.1 andrew 46: Process_Dir("$basedir/$subdir")
47: }
48: }
49:
50: sub Make_Torrent
51: {
1.4 andrew 52: my $basedir = shift;
53: my $files = shift;
1.1 andrew 54:
1.10 andrew 55: if ($#{ $files } < $OBT->{MIN_FILES}) {
1.4 andrew 56: print "Too few files in $basedir, skipping . . .\n";
57: return undef;
58: }
1.3 andrew 59:
1.4 andrew 60: if ($basedir !~ /\.\./ && $basedir =~ /^([\w\/\.-]+)$/) {
61: $basedir = $1;
62: } else {
63: die "Invalid characters in dir '$basedir'";
64: }
1.1 andrew 65:
1.4 andrew 66: foreach (@$files) {
67: if (/^([^\/]+)$/) {
68: $_ = "$basedir/$1";
69: } else {
70: die "Invalid characters in file '$_' in '$basedir'";
71: }
72: }
1.1 andrew 73:
1.4 andrew 74: my $torrent = Name_Torrent($basedir);
1.3 andrew 75:
1.4 andrew 76: print "Creating $torrent\n";
1.1 andrew 77:
1.4 andrew 78: my $comment = "Files from $basedir\n" .
79: "Created by andrew fresh (andrew\@mad-techies.org)\n" .
80: "http://OpenBSD.somedomain.net/";
1.1 andrew 81:
1.7 andrew 82: eval { btmake($torrent, $comment, $files); };
83: if ($@) {
1.12 andrew 84: print "Error creating $torrent\n$@\n";
1.7 andrew 85: }
1.3 andrew 86:
1.5 andrew 87: # system($BTMake,
88: # '-C',
89: # '-c', $comment,
1.10 andrew 90: # '-n', $OBT->{BASENAME},
91: # '-o', $OBT->{DIR_TORRENT} . "/$torrent",
1.5 andrew 92: # '-a', $Tracker,
93: # @$files
94: # );# || die "Couldn't system $BTMake $torrent: $!";
95:
1.4 andrew 96: return $torrent;
1.5 andrew 97: }
98:
99:
100: # Stole and modified from btmake to work for this.
101: sub btmake {
102: no locale;
103:
104: my $torrent = shift;
105: my $comment = shift;
106: my $files = shift;
107:
1.10 andrew 108: my $name = $OBT->{BASENAME};
109: my $announce = $OBT->{URL_TRACKER};
110: my $piece_len = 2 << ($OBT->{PIECE_LENGTH} - 1);
1.5 andrew 111:
1.12 andrew 112: my $torrent_with_path = $OBT->{DIR_NEW_TORRENT} . "/$torrent";
1.5 andrew 113:
1.16 andrew 114: my $t = BT::MetaInfo::Cached->new(
1.15 andrew 115: {
1.20 ! andrew 116: cache_root => '/tmp/OBTFileCache'
1.15 andrew 117: }
118: );
119:
1.5 andrew 120: $t->name($name);
121: $t->announce($announce);
122: unless ($announce =~ m!^http://[^/]+/!i) {
123: warn " [ WARNING: announce URL does not look like: http://hostname/ ]\n";
124: }
125: $t->comment($comment);
126: #foreach my $pair (split(/;/, $::opt_f)) {
127: # if (my($key, $val) = split(/,/, $pair, 2)) {
128: # $t->set($key, $val);
129: # }
130: #}
131: $t->piece_length($piece_len);
1.18 andrew 132: $t->creation_date(time);
1.9 andrew 133: print "Checksumming files. This may take a little while...\n";
1.5 andrew 134: $t->set_files(@$files);
1.8 andrew 135:
1.10 andrew 136: if ($t->total_size < $OBT->{MIN_SIZE}) {
1.8 andrew 137: print "Skipping smaller than minimum size\n";
138: return 0;
139: }
140:
1.14 andrew 141: my $hash = $t->info_hash;
1.12 andrew 142: $hash = unpack("H*", $hash);
143:
144: $t->save($torrent_with_path);
145: print "Created: $torrent_with_path\n";
1.1 andrew 146: }
1.3 andrew 147:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>