File: btcopyannounce.py

package info (click to toggle)
debtorrent 0.1.9
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,452 kB
  • ctags: 1,183
  • sloc: python: 13,526; sh: 274; makefile: 51
file content (78 lines) | stat: -rwxr-xr-x 2,218 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
#
# btreannounce.py written by Henry 'Pi' James and Bram Cohen
# multitracker extensions by John Hoffman
# Modified by Cameron Dale
# see LICENSE.txt for license information
#
# $Id: btcopyannounce.py 71 2007-05-29 05:58:07Z camrdale-guest $

"""Copy the announce information from one metainfo file to others.

Copies all the announce information from an input metainfo file, and 
makes all the other metainfo files match it.

"""

from sys import argv,exit
from os.path import split
from DebTorrent.bencode import bencode, bdecode


def give_announce_list(l):
    """Converts an announce list into human-readable output.
    
    @type l: C{list}
    @param l: the announce list to convert
    @rtype: C{string}
    @return: the human-readbale announce list
    
    """
    
    list = []
    for tier in l:
        for tracker in tier:
            list+=[tracker,',']
        del list[-1]
        list+=['|']
    del list[-1]
    liststring = ''
    for i in list:
        liststring+=i
    return liststring


if len(argv) < 3:
    a,b = split(argv[0])
    print 'Usage: ' + b + ' <source.torrent> <file1.torrent> [file2.torrent...]'
    print 'copies announce information from source to all specified torrents'
    exit(2) # common exit code for syntax error

h = open(argv[1], 'rb')
source_metainfo = bdecode(h.read())
h.close()

print 'new announce: ' + source_metainfo['announce']
if source_metainfo.has_key('announce-list'):
    print 'new announce-list: ' + give_announce_list(source_metainfo['announce-list'])


for f in argv[2:]:
    h = open(f, 'rb')
    metainfo = bdecode(h.read())
    h.close()
    print 'old announce for %s: %s' % (f, metainfo['announce'])
    metainfo['announce'] = source_metainfo['announce']
    if metainfo.has_key('announce-list'):
        print 'old announce-list for %s: %s' % (f, give_announce_list(metainfo['announce-list']))
    if source_metainfo.has_key('announce-list'):
        metainfo['announce-list'] = source_metainfo['announce-list']
    elif metainfo.has_key('announce-list'):
        try:
            del metainfo['announce-list']
        except:
            pass
        
    h = open(f, 'wb')
    h.write(bencode(metainfo))
    h.close()