<s> from inifile import ini_write , ini_read <EOL> from bencode import bencode , bdecode <EOL> from types import IntType , LongType , StringType , FloatType <EOL> from CreateIcons import GetIcons , CreateIcon <EOL> from parseargs import defaultargs <EOL> from __init__ import product_name , version_short <EOL> import sys , os <EOL> from time import time , strftime <EOL> from binascii import b2a_hex as tohex , a2b_hex as unhex <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> try : <EOL> realpath = os . path . realpath <EOL> except : <EOL> realpath = lambda x : x <EOL> OLDICONPATH = os . path . abspath ( os . path . dirname ( realpath ( sys . argv [ <NUM_LIT:0> ] ) ) ) <EOL> DIRNAME = '<STR_LIT:.>' + product_name <EOL> def copyfile ( oldpath , newpath ) : <EOL> try : <EOL> f = open ( oldpath , '<STR_LIT:rb>' ) <EOL> r = f . read ( ) <EOL> success = True <EOL> except : <EOL> success = False <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> if not success : <EOL> return False <EOL> try : <EOL> f = open ( newpath , '<STR_LIT:wb>' ) <EOL> f . write ( r ) <EOL> except : <EOL> success = False <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> return success <EOL> class ConfigDir : <EOL> def __init__ ( self , config_type = None ) : <EOL> self . config_type = config_type <EOL> if config_type : <EOL> config_ext = '<STR_LIT:.>' + config_type <EOL> else : <EOL> config_ext = '<STR_LIT>' <EOL> def check_sysvars ( x ) : <EOL> y = os . path . expandvars ( x ) <EOL> if y != x and os . path . isdir ( y ) : <EOL> return y <EOL> return None <EOL> for d in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> dir_root = check_sysvars ( d ) <EOL> if dir_root : <EOL> break <EOL> else : <EOL> dir_root = os . path . expanduser ( '<STR_LIT>' ) <EOL> if not os . path . isdir ( dir_root ) : <EOL> dir_root = os . path . abspath ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) ) <EOL> dir_root = os . path . join ( dir_root , DIRNAME ) <EOL> self . dir_root = dir_root <EOL> if not os . path . isdir ( self . dir_root ) : <EOL> os . mkdir ( self . dir_root , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . dir_icons = os . path . join ( dir_root , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . dir_icons ) : <EOL> os . mkdir ( self . dir_icons ) <EOL> for icon in GetIcons ( ) : <EOL> i = os . path . join ( self . dir_icons , icon ) <EOL> if not os . path . exists ( i ) : <EOL> if not copyfile ( os . path . join ( OLDICONPATH , icon ) , i ) : <EOL> CreateIcon ( icon , self . dir_icons ) <EOL> self . dir_torrentcache = os . path . join ( dir_root , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . dir_torrentcache ) : <EOL> os . mkdir ( self . dir_torrentcache ) <EOL> self . dir_datacache = os . path . join ( dir_root , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . dir_datacache ) : <EOL> os . mkdir ( self . dir_datacache ) <EOL> self . dir_piececache = os . path . join ( dir_root , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . dir_piececache ) : <EOL> os . mkdir ( self . dir_piececache ) <EOL> self . configfile = os . path . join ( dir_root , '<STR_LIT>' + config_ext + '<STR_LIT>' ) <EOL> self . statefile = os . path . join ( dir_root , '<STR_LIT:state>' + config_ext ) <EOL> self . TorrentDataBuffer = { } <EOL> def setDefaults ( self , defaults , ignore = [ ] ) : <EOL> self . config = defaultargs ( defaults ) <EOL> for k in ignore : <EOL> if self . config . has_key ( k ) : <EOL> del self . config [ k ] <EOL> def checkConfig ( self ) : <EOL> return os . path . exists ( self . configfile ) <EOL> def loadConfig ( self ) : <EOL> try : <EOL> r = ini_read ( self . configfile ) [ '<STR_LIT>' ] <EOL> except : <EOL> return self . config <EOL> l = self . config . keys ( ) <EOL> for k , v in r . items ( ) : <EOL> if self . config . has_key ( k ) : <EOL> t = type ( self . config [ k ] ) <EOL> try : <EOL> if t == StringType : <EOL> self . config [ k ] = v <EOL> elif t == IntType or t == LongType : <EOL> self . config [ k ] = long ( v ) <EOL> elif t == FloatType : <EOL> self . config [ k ] = float ( v ) <EOL> l . remove ( k ) <EOL> except : <EOL> pass <EOL> if l : <EOL> self . saveConfig ( ) <EOL> return self . config <EOL> def saveConfig ( self , new_config = None ) : <EOL> if new_config : <EOL> for k , v in new_config . items ( ) : <EOL> if self . config . has_key ( k ) : <EOL> self . config [ k ] = v <EOL> try : <EOL> ini_write ( self . configfile , self . config , <EOL> '<STR_LIT>' + product_name + '<STR_LIT:/>' + version_short + '<STR_LIT:\n>' <EOL> + strftime ( '<STR_LIT>' ) ) <EOL> return True <EOL> except : <EOL> return False <EOL> def getConfig ( self ) : <EOL> return self . config <EOL> def getState ( self ) : <EOL> try : <EOL> f = open ( self . statefile , '<STR_LIT:rb>' ) <EOL> r = f . read ( ) <EOL> except : <EOL> r = None <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> try : <EOL> r = bdecode ( r ) <EOL> except : <EOL> r = None <EOL> return r <EOL> def saveState ( self , state ) : <EOL> try : <EOL> f = open ( self . statefile , '<STR_LIT:wb>' ) <EOL> f . write ( bencode ( state ) ) <EOL> success = True <EOL> except : <EOL> success = False <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> return success <EOL> def getTorrents ( self ) : <EOL> d = { } <EOL> for f in os . listdir ( self . dir_torrentcache ) : <EOL> f = os . path . basename ( f ) <EOL> try : <EOL> f , garbage = f . split ( '<STR_LIT:.>' ) <EOL> except : <EOL> pass <EOL> d [ unhex ( f ) ] = <NUM_LIT:1> <EOL> return d . keys ( ) <EOL> def getTorrentVariations ( self , t ) : <EOL> t = tohex ( t ) <EOL> d = [ ] <EOL> for f in os . listdir ( self . dir_torrentcache ) : <EOL> f = os . path . basename ( f ) <EOL> if f [ : len ( t ) ] == t : <EOL> try : <EOL> garbage , ver = f . split ( '<STR_LIT:.>' ) <EOL> except : <EOL> ver = '<STR_LIT:0>' <EOL> d . append ( int ( ver ) ) <EOL> d . sort ( ) <EOL> return d <EOL> def getTorrent ( self , t , v = - <NUM_LIT:1> ) : <EOL> t = tohex ( t ) <EOL> if v == - <NUM_LIT:1> : <EOL> v = max ( self . getTorrentVariations ( t ) ) <EOL> if v : <EOL> t += '<STR_LIT:.>' + str ( v ) <EOL> try : <EOL> f = open ( os . path . join ( self . dir_torrentcache , t ) , '<STR_LIT:rb>' ) <EOL> r = bdecode ( f . read ( ) ) <EOL> except : <EOL> r = None <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> return r <EOL> def writeTorrent ( self , data , t , v = - <NUM_LIT:1> ) : <EOL> t = tohex ( t ) <EOL> if v == - <NUM_LIT:1> : <EOL> try : <EOL> v = max ( self . getTorrentVariations ( t ) ) + <NUM_LIT:1> <EOL> except : <EOL> v = <NUM_LIT:0> <EOL> if v : <EOL> t += '<STR_LIT:.>' + str ( v ) <EOL> try : <EOL> f = open ( os . path . join ( self . dir_torrentcache , t ) , '<STR_LIT:wb>' ) <EOL> f . write ( bencode ( data ) ) <EOL> except : <EOL> v = None <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> return v <EOL> def getTorrentData ( self , t ) : <EOL> if self . TorrentDataBuffer . has_key ( t ) : <EOL> return self . TorrentDataBuffer [ t ] <EOL> t = os . path . join ( self . dir_datacache , tohex ( t ) ) <EOL> if not os . path . exists ( t ) : <EOL> return None <EOL> try : <EOL> f = open ( t , '<STR_LIT:rb>' ) <EOL> r = bdecode ( f . read ( ) ) <EOL> except : <EOL> r = None <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> self . TorrentDataBuffer [ t ] = r <EOL> return r <EOL> def writeTorrentData ( self , t , data ) : <EOL> self . TorrentDataBuffer [ t ] = data <EOL> try : <EOL> f = open ( os . path . join ( self . dir_datacache , tohex ( t ) ) , '<STR_LIT:wb>' ) <EOL> f . write ( bencode ( data ) ) <EOL> success = True <EOL> except : <EOL> success = False <EOL> try : <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> if not success : <EOL> self . deleteTorrentData ( t ) <EOL> return success <EOL> def deleteTorrentData ( self , t ) : <EOL> try : <EOL> os . remove ( os . path . join ( self . dir_datacache , tohex ( t ) ) ) <EOL> except : <EOL> pass <EOL> def getPieceDir ( self , t ) : <EOL> return os . path . join ( self . dir_piececache , tohex ( t ) ) <EOL> def deleteOldCacheData ( self , days , still_active = [ ] , delete_torrents = False ) : <EOL> if not days : <EOL> return <EOL> exptime = time ( ) - ( days * <NUM_LIT> * <NUM_LIT> ) <EOL> names = { } <EOL> times = { } <EOL> for f in os . listdir ( self . dir_torrentcache ) : <EOL> p = os . path . join ( self . dir_torrentcache , f ) <EOL> f = os . path . basename ( f ) <EOL> try : <EOL> f , garbage = f . split ( '<STR_LIT:.>' ) <EOL> except : <EOL> pass <EOL> try : <EOL> f = unhex ( f ) <EOL> assert len ( f ) == <NUM_LIT:20> <EOL> except : <EOL> continue <EOL> if delete_torrents : <EOL> names . setdefault ( f , [ ] ) . append ( p ) <EOL> try : <EOL> t = os . path . getmtime ( p ) <EOL> except : <EOL> t = time ( ) <EOL> times . setdefault ( f , [ ] ) . append ( t ) <EOL> for f in os . listdir ( self . dir_datacache ) : <EOL> p = os . path . join ( self . dir_datacache , f ) <EOL> try : <EOL> f = unhex ( os . path . basename ( f ) ) <EOL> assert len ( f ) == <NUM_LIT:20> <EOL> except : <EOL> continue <EOL> names . setdefault ( f , [ ] ) . append ( p ) <EOL> try : <EOL> t = os . path . getmtime ( p ) <EOL> except : <EOL> t = time ( ) <EOL> times . setdefault ( f , [ ] ) . append ( t ) <EOL> for f in os . listdir ( self . dir_piececache ) : <EOL> p = os . path . join ( self . dir_piececache , f ) <EOL> try : <EOL> f = unhex ( os . path . basename ( f ) ) <EOL> assert len ( f ) == <NUM_LIT:20> <EOL> except : <EOL> continue <EOL> for f2 in os . listdir ( p ) : <EOL> p2 = os . path . join ( p , f2 ) <EOL> names . setdefault ( f , [ ] ) . append ( p2 ) <EOL> try : <EOL> t = os . path . getmtime ( p2 ) <EOL> except : <EOL> t = time ( ) <EOL> times . setdefault ( f , [ ] ) . append ( t ) <EOL> names . setdefault ( f , [ ] ) . append ( p ) <EOL> for k , v in times . items ( ) : <EOL> if max ( v ) < exptime and not k in still_active : <EOL> for f in names [ k ] : <EOL> try : <EOL> os . remove ( f ) <EOL> except : <EOL> try : <EOL> os . removedirs ( f ) <EOL> except : <EOL> pass <EOL> def deleteOldTorrents ( self , days , still_active = [ ] ) : <EOL> self . deleteOldCacheData ( days , still_active , True ) <EOL> def getIconDir ( self ) : <EOL> return self . dir_icons </s>
