~ubuntu-branches/ubuntu/precise/bittornado/precise

« back to all changes in this revision

Viewing changes to .pc/32_use_hashlib_for_sha.patch/BitTornado/parsedir.py

  • Committer: Barry Warsaw
  • Date: 2011-08-10 23:17:46 UTC
  • mfrom: (7.1.1 bittornado)
  • Revision ID: barry@python.org-20110810231746-5buiob6p54m266s8
Tags: 0.3.18-10ubuntu2
* switch to dh_python2 (LP: #788514)
  - install btmakemetafile.py and btcompletedir.py via pyinstall
  - add build depend on python-all
  - bump debhelper depend to 7 for dh_auto_install

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Written by John Hoffman and Uoti Urpala
 
2
# see LICENSE.txt for license information
 
3
from bencode import bencode, bdecode
 
4
from BT1.btformats import check_info
 
5
from os.path import exists, isfile
 
6
from sha import sha
 
7
import sys, os
 
8
 
 
9
try:
 
10
    True
 
11
except:
 
12
    True = 1
 
13
    False = 0
 
14
 
 
15
NOISY = False
 
16
 
 
17
def _errfunc(x):
 
18
    print ":: "+x
 
19
 
 
20
def parsedir(directory, parsed, files, blocked,
 
21
             exts = ['.torrent'], return_metainfo = False, errfunc = _errfunc):
 
22
    if NOISY:
 
23
        errfunc('checking dir')
 
24
    dirs_to_check = [directory]
 
25
    new_files = {}
 
26
    new_blocked = {}
 
27
    torrent_type = {}
 
28
    while dirs_to_check:    # first, recurse directories and gather torrents
 
29
        directory = dirs_to_check.pop()
 
30
        newtorrents = False
 
31
        for f in os.listdir(directory):
 
32
            newtorrent = None
 
33
            for ext in exts:
 
34
                if f.endswith(ext):
 
35
                    newtorrent = ext[1:]
 
36
                    break
 
37
            if newtorrent:
 
38
                newtorrents = True
 
39
                p = os.path.join(directory, f)
 
40
                new_files[p] = [(int(os.path.getmtime(p)),
 
41
                                 os.path.getsize(p)), 0]
 
42
                torrent_type[p] = newtorrent
 
43
        if not newtorrents:
 
44
            for f in os.listdir(directory):
 
45
                p = os.path.join(directory, f)
 
46
                if os.path.isdir(p):
 
47
                    dirs_to_check.append(p)
 
48
 
 
49
    new_parsed = {}
 
50
    to_add = []
 
51
    added = {}
 
52
    removed = {}
 
53
    # files[path] = [(modification_time, size), hash], hash is 0 if the file
 
54
    # has not been successfully parsed
 
55
    for p,v in new_files.items():   # re-add old items and check for changes
 
56
        oldval = files.get(p)
 
57
        if not oldval:          # new file
 
58
            to_add.append(p)
 
59
            continue
 
60
        h = oldval[1]
 
61
        if oldval[0] == v[0]:   # file is unchanged from last parse
 
62
            if h:
 
63
                if blocked.has_key(p):  # parseable + blocked means duplicate
 
64
                    to_add.append(p)    # other duplicate may have gone away
 
65
                else:
 
66
                    new_parsed[h] = parsed[h]
 
67
                new_files[p] = oldval
 
68
            else:
 
69
                new_blocked[p] = 1  # same broken unparseable file
 
70
            continue
 
71
        if parsed.has_key(h) and not blocked.has_key(p):
 
72
            if NOISY:
 
73
                errfunc('removing '+p+' (will re-add)')
 
74
            removed[h] = parsed[h]
 
75
        to_add.append(p)
 
76
 
 
77
    to_add.sort()
 
78
    for p in to_add:                # then, parse new and changed torrents
 
79
        new_file = new_files[p]
 
80
        v,h = new_file
 
81
        if new_parsed.has_key(h): # duplicate
 
82
            if not blocked.has_key(p) or files[p][0] != v:
 
83
                errfunc('**warning** '+
 
84
                    p +' is a duplicate torrent for '+new_parsed[h]['path'])
 
85
            new_blocked[p] = 1
 
86
            continue
 
87
                
 
88
        if NOISY:
 
89
            errfunc('adding '+p)
 
90
        try:
 
91
            ff = open(p, 'rb')
 
92
            d = bdecode(ff.read())
 
93
            check_info(d['info'])
 
94
            h = sha(bencode(d['info'])).digest()
 
95
            new_file[1] = h
 
96
            if new_parsed.has_key(h):
 
97
                errfunc('**warning** '+
 
98
                    p +' is a duplicate torrent for '+new_parsed[h]['path'])
 
99
                new_blocked[p] = 1
 
100
                continue
 
101
 
 
102
            a = {}
 
103
            a['path'] = p
 
104
            f = os.path.basename(p)
 
105
            a['file'] = f
 
106
            a['type'] = torrent_type[p]
 
107
            i = d['info']
 
108
            l = 0
 
109
            nf = 0
 
110
            if i.has_key('length'):
 
111
                l = i.get('length',0)
 
112
                nf = 1
 
113
            elif i.has_key('files'):
 
114
                for li in i['files']:
 
115
                    nf += 1
 
116
                    if li.has_key('length'):
 
117
                        l += li['length']
 
118
            a['numfiles'] = nf
 
119
            a['length'] = l
 
120
            a['name'] = i.get('name', f)
 
121
            def setkey(k, d = d, a = a):
 
122
                if d.has_key(k):
 
123
                    a[k] = d[k]
 
124
            setkey('failure reason')
 
125
            setkey('warning message')
 
126
            setkey('announce-list')
 
127
            if return_metainfo:
 
128
                a['metainfo'] = d
 
129
        except:
 
130
            errfunc('**warning** '+p+' has errors')
 
131
            new_blocked[p] = 1
 
132
            continue
 
133
        try:
 
134
            ff.close()
 
135
        except:
 
136
            pass
 
137
        if NOISY:
 
138
            errfunc('... successful')
 
139
        new_parsed[h] = a
 
140
        added[h] = a
 
141
 
 
142
    for p,v in files.items():       # and finally, mark removed torrents
 
143
        if not new_files.has_key(p) and not blocked.has_key(p):
 
144
            if NOISY:
 
145
                errfunc('removing '+p)
 
146
            removed[v[1]] = parsed[v[1]]
 
147
 
 
148
    if NOISY:
 
149
        errfunc('done checking')
 
150
    return (new_parsed, new_files, new_blocked, added, removed)
 
151