<s> import hashlib <EOL> import logging <EOL> import os <EOL> import platform <EOL> import requests <EOL> import re <EOL> import urllib2 <EOL> from django . utils . translation import ugettext as _ <EOL> from freenasUI . common import pbi <EOL> from freenasUI . middleware . exceptions import MiddlewareError <EOL> if platform . machine ( ) == '<STR_LIT>' : <EOL> __arch = '<STR_LIT>' <EOL> else : <EOL> __arch = '<STR_LIT>' <EOL> PLUGINS_INDEX = '<STR_LIT>' % __arch <EOL> PLUGINS_META = '<STR_LIT>' % PLUGINS_INDEX <EOL> PLUGINS_REPO = '<STR_LIT>' % PLUGINS_META <EOL> PROGRESS_FILE = "<STR_LIT>" <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> class Plugin ( object ) : <EOL> repo_id = None <EOL> application = None <EOL> version = None <EOL> category = None <EOL> created = None <EOL> rootinstall = None <EOL> arch = None <EOL> author = None <EOL> url = None <EOL> license = None <EOL> type = None <EOL> keywords = None <EOL> icon = None <EOL> description = None <EOL> sha256 = None <EOL> file = None <EOL> icon = None <EOL> hash = None <EOL> urls = None <EOL> size = None <EOL> def __init__ ( <EOL> self , repo_id , application , version , category , created , <EOL> rootinstall , arch , author , url , license , type , keywords , icon , <EOL> description , sha256 , file , hash , urls , size <EOL> ) : <EOL> self . repo_id = repo_id <EOL> self . application = application <EOL> self . version = version <EOL> self . category = category <EOL> self . created = created <EOL> self . rootinstall = rootinstall <EOL> self . arch = arch <EOL> self . author = author <EOL> self . url = url <EOL> self . license = license <EOL> self . type = type <EOL> self . keywords = keywords <EOL> self . icon = icon <EOL> self . description = description <EOL> self . sha256 = sha256 <EOL> self . file = file <EOL> self . hash = hash <EOL> self . urls = urls <EOL> self . size = size <EOL> def __setattr__ ( self , application , value ) : <EOL> if not hasattr ( self , application ) : <EOL> raise ValueError ( application ) <EOL> object . __setattr__ ( self , application , value ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . application <EOL> @ property <EOL> def id ( self ) : <EOL> return self . hash <EOL> @ property <EOL> def name ( self ) : <EOL> return self . application <EOL> @ property <EOL> def unixname ( self ) : <EOL> return self . application . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> ] . lower ( ) <EOL> def download ( self , path ) : <EOL> if not self . repo_id : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not self . urls : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> rv = False <EOL> for url in self . urls : <EOL> rpath = "<STR_LIT>" % ( url , self . file ) <EOL> try : <EOL> rv = self . __download ( rpath , path ) <EOL> if rv : <EOL> break <EOL> except Exception , e : <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> url , <EOL> type ( e ) . __class__ , <EOL> e , <EOL> ) <EOL> raise MiddlewareError ( <EOL> _ ( "<STR_LIT>" % { <EOL> '<STR_LIT:url>' : url , <EOL> '<STR_LIT:error>' : e , <EOL> } ) <EOL> ) <EOL> return rv <EOL> def __download ( self , url , path ) : <EOL> response = urllib2 . urlopen ( url ) <EOL> try : <EOL> total_size = int ( <EOL> response . info ( ) . getheader ( '<STR_LIT>' ) . strip ( ) <EOL> ) <EOL> except Exception , e : <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> type ( e ) . __class__ , <EOL> e , <EOL> ) <EOL> total_size = None <EOL> rv = True <EOL> csize = <NUM_LIT> <EOL> downloaded = <NUM_LIT:0> <EOL> last_percent = <NUM_LIT:0> <EOL> with open ( path , '<STR_LIT>' ) as f , open ( PROGRESS_FILE , '<STR_LIT:w>' ) as f2 : <EOL> if total_size : <EOL> f2 . write ( "<STR_LIT>" ) <EOL> f2 . flush ( ) <EOL> while True : <EOL> chunk = response . read ( csize ) <EOL> if not chunk : <EOL> break <EOL> f . write ( chunk ) <EOL> if total_size : <EOL> downloaded += len ( chunk ) <EOL> percent = float ( downloaded ) / total_size <EOL> percent = round ( percent * <NUM_LIT:100> , <NUM_LIT:2> ) <EOL> if int ( percent ) != last_percent : <EOL> f2 . write ( "<STR_LIT>" % percent ) <EOL> f2 . flush ( ) <EOL> last_percent = int ( percent ) <EOL> if total_size and downloaded != total_size : <EOL> return False <EOL> if self . hash : <EOL> log . debug ( "<STR_LIT>" , self ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> dohash = hashlib . sha256 ( ) <EOL> while True : <EOL> chunk = f . read ( csize ) <EOL> if not chunk : <EOL> break <EOL> dohash . update ( chunk ) <EOL> if dohash . hexdigest ( ) != self . hash : <EOL> log . debug ( "<STR_LIT>" , url ) <EOL> return False <EOL> return rv <EOL> class Available ( object ) : <EOL> __cache = None <EOL> __repo_id = None <EOL> __repo_desc = None <EOL> __def_repo = None <EOL> def __init__ ( self ) : <EOL> self . __cache = dict ( ) <EOL> def _def_repo_id ( self , repo_id = None ) : <EOL> """<STR_LIT>""" <EOL> if self . __def_repo is not None : <EOL> return self . __repo_id <EOL> self . __def_repo = True <EOL> repo = self . get_repo ( repo_id = repo_id , create = True ) <EOL> if repo : <EOL> self . __repo_id = repo [ <NUM_LIT:0> ] <EOL> self . __repo_desc = repo [ <NUM_LIT:1> ] <EOL> return self . __repo_id <EOL> def create_repo ( self ) : <EOL> from freenasUI . middleware . notifier import notifier <EOL> url = PLUGINS_REPO <EOL> rpath = "<STR_LIT>" <EOL> r = requests . get ( url , timeout = <NUM_LIT:8> ) <EOL> if r . status_code != requests . codes . ok : <EOL> return False <EOL> with open ( rpath , "<STR_LIT:w>" ) as f : <EOL> for byte in r : <EOL> f . write ( byte ) <EOL> f . close ( ) <EOL> p = pbi . PBI ( ) <EOL> out = p . addrepo ( repofile = rpath ) <EOL> if not out : <EOL> return False <EOL> notifier ( ) . restart ( "<STR_LIT>" ) <EOL> return True <EOL> def get_repo ( self , repo_id = None , create = False ) : <EOL> p = pbi . PBI ( ) <EOL> if repo_id : <EOL> repos = p . listrepo ( repoid = repo_id ) <EOL> else : <EOL> repos = p . listrepo ( ) <EOL> if not repos and create is False : <EOL> return None <EOL> elif not repos and create is True : <EOL> if not self . create_repo ( ) : <EOL> return None <EOL> repos = p . listrepo ( ) <EOL> elif repos and repo_id is not None : <EOL> for repo in repos : <EOL> return repo <EOL> for repo in repos : <EOL> if re . match ( '<STR_LIT>' , repo [ <NUM_LIT:1> ] , re . I ) : <EOL> return repo <EOL> for repo in repos : <EOL> return repo <EOL> if create is True : <EOL> if not self . create_repo ( ) : <EOL> return None <EOL> repos = p . listrepo ( ) <EOL> if not repos : <EOL> return None <EOL> for repo in repos : <EOL> description = repo [ <NUM_LIT:1> ] <EOL> if re . match ( '<STR_LIT>' , description , re . I ) : <EOL> return repo <EOL> return None <EOL> def get_mirror_urls ( self , repo_id ) : <EOL> reposdir = "<STR_LIT>" <EOL> mirrorsdir = "<STR_LIT>" <EOL> if not repo_id : <EOL> repo_id = self . _def_repo_id ( ) <EOL> if not repo_id : <EOL> return None <EOL> urls = [ ] <EOL> for f in os . listdir ( reposdir ) : <EOL> parts = f . split ( '<STR_LIT:.>' ) <EOL> if not parts : <EOL> continue <EOL> id = parts [ <NUM_LIT:0> ] <EOL> if id == repo_id : <EOL> fp = "<STR_LIT>" % ( mirrorsdir , parts [ <NUM_LIT:1> ] ) <EOL> with open ( fp , "<STR_LIT:r>" ) as mirrors : <EOL> for line in mirrors : <EOL> mirror = line . strip ( ) <EOL> urls . append ( mirror ) <EOL> mirrors . close ( ) <EOL> return urls <EOL> def get_index_entry ( <EOL> self , repo_id = None , application = None , arch = None , version = None <EOL> ) : <EOL> reposdir = "<STR_LIT>" <EOL> indexdir = "<STR_LIT>" <EOL> if not application or not version : <EOL> return None <EOL> if not repo_id : <EOL> repo_id = self . _def_repo_id ( ) <EOL> if not repo_id : <EOL> return None <EOL> sha256 = None <EOL> for f in os . listdir ( reposdir ) : <EOL> parts = f . split ( '<STR_LIT:.>' ) <EOL> if not parts : <EOL> continue <EOL> id = parts [ <NUM_LIT:0> ] <EOL> if id == repo_id : <EOL> sha256 = parts [ <NUM_LIT:1> ] <EOL> break <EOL> if sha256 : <EOL> try : <EOL> indexfile = "<STR_LIT>" % ( indexdir , sha256 ) <EOL> with open ( indexfile , "<STR_LIT:r>" ) as f : <EOL> for line in f : <EOL> parts = line . split ( '<STR_LIT::>' ) <EOL> if ( <EOL> parts [ <NUM_LIT:0> ] == application . lower ( ) and <EOL> parts [ <NUM_LIT:1> ] == arch . lower ( ) and <EOL> parts [ <NUM_LIT:2> ] == version . lower ( ) <EOL> ) : <EOL> return parts <EOL> f . close ( ) <EOL> except Exception : <EOL> log . debug ( "<STR_LIT>" , sha256 ) <EOL> return None <EOL> return None <EOL> def get_icon_count ( self ) : <EOL> icon_count = <NUM_LIT:0> <EOL> repo_hashes = [ ] <EOL> for f in os . listdir ( pbi . PBID_REPOSDIR ) : <EOL> parts = f . split ( '<STR_LIT:.>' ) <EOL> repo_hashes . append ( parts [ - <NUM_LIT:1> ] ) <EOL> meta_files = { } <EOL> for h in repo_hashes : <EOL> meta_path = "<STR_LIT>" % ( pbi . PBID_INDEXDIR , h ) <EOL> if os . path . exists ( meta_path ) : <EOL> meta_files [ h ] = meta_path <EOL> meta = { } <EOL> total_count = <NUM_LIT:0> <EOL> for h in meta_files : <EOL> m = meta_files [ h ] <EOL> with open ( m , '<STR_LIT:r>' ) as f : <EOL> lines = [ ] <EOL> for l in f . readlines ( ) : <EOL> lines . append ( l . strip ( ) ) <EOL> total_count += len ( lines ) <EOL> meta [ h ] = lines <EOL> f . close ( ) <EOL> for h in meta : <EOL> m = meta [ h ] <EOL> for line in m : <EOL> parts = line . split ( '<STR_LIT:;>' ) <EOL> app = parts [ <NUM_LIT:0> ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ext = parts [ <NUM_LIT:2> ] . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> icon_path = "<STR_LIT>" % ( pbi . PBID_ICONSDIR , h , app , ext ) <EOL> if os . path . exists ( icon_path ) : <EOL> icon_count += <NUM_LIT:1> <EOL> return icon_count <EOL> def get_total_icon_count ( self ) : <EOL> total_count = <NUM_LIT:0> <EOL> repo_hashes = [ ] <EOL> for f in os . listdir ( pbi . PBID_REPOSDIR ) : <EOL> parts = f . split ( '<STR_LIT:.>' ) <EOL> repo_hashes . append ( parts [ - <NUM_LIT:1> ] ) <EOL> meta_files = { } <EOL> for h in repo_hashes : <EOL> meta_path = "<STR_LIT>" % ( pbi . PBID_INDEXDIR , h ) <EOL> if os . path . exists ( meta_path ) : <EOL> meta_files [ h ] = meta_path <EOL> meta = { } <EOL> for h in meta_files : <EOL> m = meta_files [ h ] <EOL> with open ( m , '<STR_LIT:r>' ) as f : <EOL> lines = [ ] <EOL> for l in f . readlines ( ) : <EOL> lines . append ( l . strip ( ) ) <EOL> total_count += len ( lines ) <EOL> meta [ h ] = lines <EOL> f . close ( ) <EOL> return total_count <EOL> def get_icon ( self , repo_id = None , oid = None ) : <EOL> icon_path = None <EOL> available = self . get_remote ( repo_id = None , cache = True ) <EOL> for p in available : <EOL> if p . id == oid : <EOL> icon_path = p . icon <EOL> break <EOL> icon = None <EOL> if icon_path : <EOL> try : <EOL> with open ( icon_path , '<STR_LIT:r>' ) as f : <EOL> icon = f . read ( ) <EOL> f . close ( ) <EOL> except : <EOL> log . debug ( "<STR_LIT>" , icon_path ) <EOL> icon = None <EOL> return icon <EOL> def get_update_status ( self , oid ) : <EOL> from freenasUI . plugins import models <EOL> status = False <EOL> iplugin = models . Plugins . objects . filter ( id = oid ) <EOL> if iplugin : <EOL> iplugin = iplugin [ <NUM_LIT:0> ] <EOL> rplugin = None <EOL> for rp in self . get_remote ( cache = True ) : <EOL> if rp . name . lower ( ) == iplugin . plugin_name . lower ( ) : <EOL> rplugin = rp <EOL> break <EOL> if rplugin and iplugin : <EOL> if str ( iplugin . plugin_version ) . lower ( ) != str ( rplugin . version ) . lower ( ) : <EOL> status = True <EOL> return status <EOL> def get_local ( self ) : <EOL> results = [ ] <EOL> return results <EOL> def get_remote ( self , repo_id = None , cache = False ) : <EOL> log . debug ( "<STR_LIT>" , repo_id , cache ) <EOL> if not repo_id : <EOL> repo_id = self . _def_repo_id ( ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> repo_id <EOL> ) <EOL> if cache and repo_id in self . __cache : <EOL> log . debug ( "<STR_LIT>" , repo_id ) <EOL> return self . __cache . get ( repo_id ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> repo_id <EOL> ) <EOL> p = pbi . PBI ( ) <EOL> p . pbid ( flags = pbi . PBID_FLAGS_REFRESH , sync = True ) <EOL> results = p . browser ( repo_id = repo_id , flags = pbi . PBI_BROWSER_FLAGS_VIEWALL ) <EOL> if not results : <EOL> log . debug ( <EOL> "<STR_LIT>" , repo_id <EOL> ) <EOL> return results <EOL> plugins = [ ] <EOL> results . sort ( key = lambda x : x [ '<STR_LIT>' ] . lower ( ) ) <EOL> for p in results : <EOL> log . debug ( "<STR_LIT>" , p ) <EOL> try : <EOL> index_entry = self . get_index_entry ( <EOL> repo_id , <EOL> application = p [ '<STR_LIT>' ] , <EOL> arch = p [ '<STR_LIT>' ] , <EOL> version = p [ '<STR_LIT>' ] <EOL> ) <EOL> log . debug ( "<STR_LIT>" , index_entry ) <EOL> if not index_entry : <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> p [ '<STR_LIT>' ] <EOL> ) <EOL> continue <EOL> urls = self . get_mirror_urls ( repo_id ) <EOL> log . debug ( "<STR_LIT>" , urls ) <EOL> item = self . _get_remote_item ( repo_id , p , index_entry , urls ) <EOL> log . debug ( "<STR_LIT>" , item ) <EOL> if item is False : <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> p [ '<STR_LIT>' ] <EOL> ) <EOL> continue <EOL> plugins . append ( item ) <EOL> except Exception as e : <EOL> log . debug ( "<STR_LIT>" , e ) <EOL> log . debug ( "<STR_LIT>" , plugins ) <EOL> self . __cache [ repo_id ] = plugins <EOL> return plugins <EOL> def _get_remote_item ( self , repo_id , p , ie , urls ) : <EOL> arch = p [ '<STR_LIT>' ] <EOL> if arch != platform . machine ( ) : <EOL> return False <EOL> return Plugin ( <EOL> repo_id = repo_id , <EOL> application = p [ '<STR_LIT>' ] , <EOL> version = p [ '<STR_LIT>' ] , <EOL> category = p [ '<STR_LIT>' ] , <EOL> created = p [ '<STR_LIT>' ] , <EOL> rootinstall = p [ '<STR_LIT>' ] , <EOL> arch = p [ '<STR_LIT>' ] , <EOL> author = p [ '<STR_LIT>' ] , <EOL> url = p [ '<STR_LIT>' ] , <EOL> license = p [ '<STR_LIT>' ] , <EOL> type = p [ '<STR_LIT>' ] , <EOL> keywords = p [ '<STR_LIT>' ] , <EOL> icon = p [ '<STR_LIT>' ] , <EOL> description = p [ '<STR_LIT>' ] , <EOL> sha256 = ie [ <NUM_LIT:3> ] , <EOL> file = ie [ <NUM_LIT:5> ] , <EOL> hash = ie [ <NUM_LIT:3> ] , <EOL> urls = urls , <EOL> size = ( long ( ie [ <NUM_LIT:9> ] ) * <NUM_LIT> ) <EOL> ) <EOL> def all ( self ) : <EOL> return self . get_local ( ) <EOL> availablePlugins = Available ( ) </s>
