Annotation of openbsd/OpenBSDTorrents/MakeTorrents.pl, Revision 1.17
1.1 andrew 1: #!/usr/bin/perl -T
1.17 ! andrew 2: #$Id: MakeTorrents.pl,v 1.16 2005/05/06 20:07:22 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.16 andrew 113: my $t = BT::MetaInfo::Cached->new(
1.15 andrew 114: {
115: cache_root =>
116: $OBT->{DIR_HOME} . '/FileCache'
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.17 ! andrew 132: $t->creation_date(gmtime);
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>