]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/pages/torrent/client/parsedir.py
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
20 def parsedir(directory
, parsed
, files
, blocked
,
21 exts
= ['.torrent'], return_metainfo
= False, errfunc
= _errfunc
):
23 errfunc('checking dir')
24 dirs_to_check
= [directory
]
28 while dirs_to_check
: # first, recurse directories and gather torrents
29 directory
= dirs_to_check
.pop()
31 for f
in os
.listdir(directory
):
39 p
= os
.path
.join(directory
, f
)
40 new_files
[p
] = [(os
.path
.getmtime(p
), os
.path
.getsize(p
)), 0]
41 torrent_type
[p
] = newtorrent
43 for f
in os
.listdir(directory
):
44 p
= os
.path
.join(directory
, f
)
46 dirs_to_check
.append(p
)
52 # files[path] = [(modification_time, size), hash], hash is 0 if the file
53 # has not been successfully parsed
54 for p
,v
in new_files
.items(): # re-add old items and check for changes
56 if not oldval
: # new file
60 if oldval
[0] == v
[0]: # file is unchanged from last parse
62 if blocked
.has_key(p
): # parseable + blocked means duplicate
63 to_add
.append(p
) # other duplicate may have gone away
65 new_parsed
[h
] = parsed
[h
]
68 new_blocked
[p
] = 1 # same broken unparseable file
70 if parsed
.has_key(h
) and not blocked
.has_key(p
):
72 errfunc('removing '+p
+' (will re-add)')
73 removed
[h
] = parsed
[h
]
77 for p
in to_add
: # then, parse new and changed torrents
78 new_file
= new_files
[p
]
80 if new_parsed
.has_key(h
): # duplicate
81 if not blocked
.has_key(p
) or files
[p
][0] != v
:
82 errfunc('**warning** '+
83 p
+' is a duplicate torrent for '+new_parsed
[h
]['path'])
91 d
= bdecode(ff
.read())
93 h
= sha(bencode(d
['info'])).digest()
95 if new_parsed
.has_key(h
):
96 errfunc('**warning** '+
97 p
+' is a duplicate torrent for '+new_parsed
[h
]['path'])
103 f
= os
.path
.basename(p
)
105 a
['type'] = torrent_type
[p
]
109 if i
.has_key('length'):
110 l
= i
.get('length',0)
112 elif i
.has_key('files'):
113 for li
in i
['files']:
115 if li
.has_key('length'):
119 a
['name'] = i
.get('name', f
)
120 def setkey(k
, d
= d
, a
= a
):
123 setkey('failure reason')
124 setkey('warning message')
125 setkey('announce-list')
129 errfunc('**warning** '+p
+' has errors')
137 errfunc('... successful')
141 for p
,v
in files
.items(): # and finally, mark removed torrents
142 if not new_files
.has_key(p
) and not blocked
.has_key(p
):
144 errfunc('removing '+p
)
145 removed
[v
[1]] = parsed
[v
[1]]
148 errfunc('done checking')
149 return (new_parsed
, new_files
, new_blocked
, added
, removed
)