File: btshowmetainfo.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 (98 lines) | stat: -rwxr-xr-x 3,075 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
#
# Written by Henry 'Pi' James and Loring Holden
# modified for multitracker display by John Hoffman
# Modified by Cameron Dale
# see LICENSE.txt for license information
#
# $Id: btshowmetainfo.py 335 2008-01-19 22:05:46Z camrdale-guest $

"""Display a summary of the information in a metainfo or Packages file."""

from sys import *
from os.path import *
from sha import *
from binascii import b2a_hex
from DebTorrent.BT1.btformats import check_message
from DebTorrent.bencode import *
from DebTorrent.download_bt1 import get_packages
import logging

assert version_info >= (2,3), 'Requires Python 2.3 or better'

logging.basicConfig()
logger = logging.getLogger()

NAME, EXT = splitext(basename(argv[0]))
VERSION = '20030621'

print '%s %s - decode DebTorrent metainfo files' % (NAME, VERSION)
print

if len(argv) == 1:
    print '%s file1.dtorrent file2.dtorrent file3.dtorrent ...' % argv[0]
    print
    exit(2) # common exit code for syntax error

for metainfo_name in argv[1:]:
    if len(metainfo_name) >= 8 and metainfo_name[-8:] == 'Packages':
        (metainfo, temp) = get_packages(metainfo_name, '', 0)
    else:
        metainfo_file = open(metainfo_name, 'rb')
        metainfo = bdecode(metainfo_file.read())
    
    try:
        check_message(metainfo)
    except:
        logger.exception('Error found in metainfo of: %s', metainfo_name)
        continue
    
#    print metainfo
    info = metainfo['info']
    info_hash = sha(bencode(info)).hexdigest()
    identifier = b2a_hex(metainfo.get('identifier', ''))
    if not identifier:
        identifier = info_hash

    print 'metainfo file.: %s' % basename(metainfo_name)
    print 'identifier....: %s' % identifier
    print 'info hash.....: %s' % info_hash
    piece_lengths = info['piece lengths']
    print 'directory name: %s' % metainfo['name']
    print 'files.........: '
    file_length = 0;
    for file in info['files']:
        path = ''
        for item in file['path']:
            if (path != ''):
                path = path + "/"
            path = path + item
        print '   %s (%d)' % (path, file['length'])
        file_length += file['length']
        name ='archive size..:'
    print '%s %i bytes (%i pieces)' \
          % (name,file_length, len(piece_lengths))
    print 'announce url..: %s' % metainfo['announce']
    if metainfo.has_key('announce-list'):
        list = []
        for tier in metainfo['announce-list']:
            for tracker in tier:
                list+=[tracker,',']
            del list[-1]
            list+=['|']
        del list[-1]
        liststring = ''
        for i in list:
            liststring+=i
        print 'announce-list.: %s' % liststring
    if metainfo.has_key('deb_mirrors'):
        list = []
        for seed in metainfo['deb_mirrors']:
            list += [seed,'|']
        del list[-1]
        liststring = ''
        for i in list:
            liststring+=i
        print 'mirror URLs....: %s' % liststring
    if metainfo.has_key('comment'):
        print 'comment.......: %s' % metainfo['comment']