<s> from bencode import bencode , bdecode <EOL> from BT1 . btformats import check_info <EOL> from os . path import exists , isfile <EOL> from sha import sha <EOL> import sys , os <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> NOISY = False <EOL> def _errfunc ( x ) : <EOL> print "<STR_LIT>" + x <EOL> def parsedir ( directory , parsed , files , blocked , <EOL> exts = [ '<STR_LIT>' ] , return_metainfo = False , errfunc = _errfunc ) : <EOL> if NOISY : <EOL> errfunc ( '<STR_LIT>' ) <EOL> dirs_to_check = [ directory ] <EOL> new_files = { } <EOL> new_blocked = { } <EOL> torrent_type = { } <EOL> while dirs_to_check : <EOL> directory = dirs_to_check . pop ( ) <EOL> newtorrents = False <EOL> for f in os . listdir ( directory ) : <EOL> newtorrent = None <EOL> for ext in exts : <EOL> if f . endswith ( ext ) : <EOL> newtorrent = ext [ <NUM_LIT:1> : ] <EOL> break <EOL> if newtorrent : <EOL> newtorrents = True <EOL> p = os . path . join ( directory , f ) <EOL> new_files [ p ] = [ ( os . path . getmtime ( p ) , os . path . getsize ( p ) ) , <NUM_LIT:0> ] <EOL> torrent_type [ p ] = newtorrent <EOL> if not newtorrents : <EOL> for f in os . listdir ( directory ) : <EOL> p = os . path . join ( directory , f ) <EOL> if os . path . isdir ( p ) : <EOL> dirs_to_check . append ( p ) <EOL> new_parsed = { } <EOL> to_add = [ ] <EOL> added = { } <EOL> removed = { } <EOL> for p , v in new_files . items ( ) : <EOL> oldval = files . get ( p ) <EOL> if not oldval : <EOL> to_add . append ( p ) <EOL> continue <EOL> h = oldval [ <NUM_LIT:1> ] <EOL> if oldval [ <NUM_LIT:0> ] == v [ <NUM_LIT:0> ] : <EOL> if h : <EOL> if blocked . has_key ( p ) : <EOL> to_add . append ( p ) <EOL> else : <EOL> new_parsed [ h ] = parsed [ h ] <EOL> new_files [ p ] = oldval <EOL> else : <EOL> new_blocked [ p ] = <NUM_LIT:1> <EOL> continue <EOL> if parsed . has_key ( h ) and not blocked . has_key ( p ) : <EOL> if NOISY : <EOL> errfunc ( '<STR_LIT>' + p + '<STR_LIT>' ) <EOL> removed [ h ] = parsed [ h ] <EOL> to_add . append ( p ) <EOL> to_add . sort ( ) <EOL> for p in to_add : <EOL> new_file = new_files [ p ] <EOL> v , h = new_file <EOL> if new_parsed . has_key ( h ) : <EOL> if not blocked . has_key ( p ) or files [ p ] [ <NUM_LIT:0> ] != v : <EOL> errfunc ( '<STR_LIT>' + <EOL> p + '<STR_LIT>' + new_parsed [ h ] [ '<STR_LIT:path>' ] ) <EOL> new_blocked [ p ] = <NUM_LIT:1> <EOL> continue <EOL> if NOISY : <EOL> errfunc ( '<STR_LIT>' + p ) <EOL> try : <EOL> ff = open ( p , '<STR_LIT:rb>' ) <EOL> d = bdecode ( ff . read ( ) ) <EOL> check_info ( d [ '<STR_LIT:info>' ] ) <EOL> h = sha ( bencode ( d [ '<STR_LIT:info>' ] ) ) . digest ( ) <EOL> new_file [ <NUM_LIT:1> ] = h <EOL> if new_parsed . has_key ( h ) : <EOL> errfunc ( '<STR_LIT>' + <EOL> p + '<STR_LIT>' + new_parsed [ h ] [ '<STR_LIT:path>' ] ) <EOL> new_blocked [ p ] = <NUM_LIT:1> <EOL> continue <EOL> a = { } <EOL> a [ '<STR_LIT:path>' ] = p <EOL> f = os . path . basename ( p ) <EOL> a [ '<STR_LIT:file>' ] = f <EOL> a [ '<STR_LIT:type>' ] = torrent_type [ p ] <EOL> i = d [ '<STR_LIT:info>' ] <EOL> l = <NUM_LIT:0> <EOL> nf = <NUM_LIT:0> <EOL> if i . has_key ( '<STR_LIT>' ) : <EOL> l = i . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> nf = <NUM_LIT:1> <EOL> elif i . has_key ( '<STR_LIT>' ) : <EOL> for li in i [ '<STR_LIT>' ] : <EOL> nf += <NUM_LIT:1> <EOL> if li . has_key ( '<STR_LIT>' ) : <EOL> l += li [ '<STR_LIT>' ] <EOL> a [ '<STR_LIT>' ] = nf <EOL> a [ '<STR_LIT>' ] = l <EOL> a [ '<STR_LIT:name>' ] = i . get ( '<STR_LIT:name>' , f ) <EOL> def setkey ( k , d = d , a = a ) : <EOL> if d . has_key ( k ) : <EOL> a [ k ] = d [ k ] <EOL> setkey ( '<STR_LIT>' ) <EOL> setkey ( '<STR_LIT>' ) <EOL> setkey ( '<STR_LIT>' ) <EOL> if return_metainfo : <EOL> a [ '<STR_LIT>' ] = d <EOL> except : <EOL> errfunc ( '<STR_LIT>' + p + '<STR_LIT>' ) <EOL> new_blocked [ p ] = <NUM_LIT:1> <EOL> continue <EOL> try : <EOL> ff . close ( ) <EOL> except : <EOL> pass <EOL> if NOISY : <EOL> errfunc ( '<STR_LIT>' ) <EOL> new_parsed [ h ] = a <EOL> added [ h ] = a <EOL> for p , v in files . items ( ) : <EOL> if not new_files . has_key ( p ) and not blocked . has_key ( p ) : <EOL> if NOISY : <EOL> errfunc ( '<STR_LIT>' + p ) <EOL> removed [ v [ <NUM_LIT:1> ] ] = parsed [ v [ <NUM_LIT:1> ] ] <EOL> if NOISY : <EOL> errfunc ( '<STR_LIT>' ) <EOL> return ( new_parsed , new_files , new_blocked , added , removed ) </s>
