<s> """<STR_LIT>""" <EOL> import glob <EOL> import logging <EOL> import os <EOL> import json <EOL> import common <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class AppRegistry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _reg_path = os . path . join ( common . HUE_APP_REG_DIR , '<STR_LIT>' ) <EOL> self . _initialized = False <EOL> self . _apps = { } <EOL> self . _open ( ) <EOL> def _open ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( self . _reg_path ) : <EOL> reg_file = file ( self . _reg_path ) <EOL> app_list = json . load ( reg_file ) <EOL> reg_file . close ( ) <EOL> for app_json in app_list : <EOL> app_json . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> app = HueApp . create ( app_json ) <EOL> self . _apps [ app . name ] = app <EOL> self . _initialized = True <EOL> def _write ( self , path ) : <EOL> """<STR_LIT>""" <EOL> outfile = file ( path , '<STR_LIT:w>' ) <EOL> json . dump ( self . _apps . values ( ) , outfile , cls = AppJsonEncoder , indent = <NUM_LIT:2> ) <EOL> outfile . close ( ) <EOL> def contains ( self , app ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> existing = self . _apps [ app . name ] <EOL> return existing . version == app . version <EOL> except KeyError : <EOL> return False <EOL> def register ( self , app ) : <EOL> """<STR_LIT>""" <EOL> assert self . _initialized , "<STR_LIT>" <EOL> try : <EOL> existing = self . _apps [ app . name ] <EOL> version_diff = common . cmp_version ( existing . version , app . version ) <EOL> if version_diff == <NUM_LIT:0> : <EOL> LOG . warn ( '<STR_LIT>' % ( app , ) ) <EOL> return False <EOL> elif version_diff < <NUM_LIT:0> : <EOL> LOG . info ( '<STR_LIT>' % ( app , existing . version ) ) <EOL> elif version_diff > <NUM_LIT:0> : <EOL> LOG . error ( '<STR_LIT>' % ( existing . version , app ) ) <EOL> return False <EOL> except KeyError : <EOL> pass <EOL> LOG . info ( '<STR_LIT>' % ( app , ) ) <EOL> self . _apps [ app . name ] = app <EOL> return True <EOL> def unregister ( self , app_name ) : <EOL> """<STR_LIT>""" <EOL> assert self . _initialized , "<STR_LIT>" <EOL> app = self . _apps [ app_name ] <EOL> del self . _apps [ app_name ] <EOL> return app <EOL> def get_all_apps ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _apps . values ( ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . _initialized , "<STR_LIT>" <EOL> self . _write ( self . _reg_path ) <EOL> LOG . info ( '<STR_LIT>' % ( self . _reg_path , ) ) <EOL> class HueApp ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def create ( json ) : <EOL> return HueApp ( json [ '<STR_LIT:name>' ] , json [ '<STR_LIT:version>' ] , json [ '<STR_LIT:path>' ] , json [ '<STR_LIT>' ] , json [ '<STR_LIT>' ] ) <EOL> def __init__ ( self , name , version , path , desc , author ) : <EOL> self . name = name <EOL> self . version = version <EOL> self . path = path <EOL> self . desc = desc <EOL> self . author = author <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . version ) <EOL> def __cmp__ ( self , other ) : <EOL> if not isinstance ( other , HueApp ) : <EOL> raise TypeError <EOL> return cmp ( ( self . name , self . version ) , ( other . name , other . version ) ) <EOL> @ property <EOL> def rel_path ( self ) : <EOL> if os . path . isabs ( self . path ) : <EOL> return os . path . relpath ( self . path , common . APPS_ROOT ) <EOL> else : <EOL> return self . path <EOL> @ property <EOL> def abs_path ( self ) : <EOL> if not os . path . isabs ( self . path ) : <EOL> return os . path . abspath ( os . path . join ( common . APPS_ROOT , self . path ) ) <EOL> else : <EOL> return self . path <EOL> def use_rel_path ( self ) : <EOL> self . path = self . rel_path <EOL> def use_abs_path ( self ) : <EOL> self . path = self . abs_path <EOL> def jsonable ( self ) : <EOL> return dict ( name = self . name , version = self . version , path = self . path , <EOL> desc = self . desc , author = self . author ) <EOL> def find_ext_pys ( self ) : <EOL> """<STR_LIT>""" <EOL> return glob . glob ( os . path . join ( self . abs_path , '<STR_LIT>' , '<STR_LIT:*>' ) ) <EOL> def get_conffiles ( self ) : <EOL> """<STR_LIT>""" <EOL> return glob . glob ( os . path . join ( self . abs_path , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def install_conf ( self ) : <EOL> """<STR_LIT>""" <EOL> installed = [ ] <EOL> for target in self . get_conffiles ( ) : <EOL> link_name = os . path . join ( common . HUE_CONF_DIR , os . path . basename ( target ) ) <EOL> if os . path . islink ( link_name ) : <EOL> try : <EOL> cur = os . readlink ( link_name ) <EOL> if cur == target : <EOL> LOG . warn ( "<STR_LIT>" % ( link_name , ) ) <EOL> installed . append ( link_name ) <EOL> continue <EOL> if not os . path . exists ( cur ) : <EOL> os . unlink ( link_name ) <EOL> LOG . warn ( "<STR_LIT>" % ( link_name , ) ) <EOL> except OSError , ex : <EOL> LOG . warn ( "<STR_LIT>" % ( link_name , ex ) ) <EOL> try : <EOL> os . symlink ( target , link_name ) <EOL> LOG . info ( '<STR_LIT>' % ( link_name , target ) ) <EOL> installed . append ( link_name ) <EOL> except OSError , ex : <EOL> LOG . error ( "<STR_LIT>" % ( target , link_name , ex ) ) <EOL> for lnk in installed : <EOL> try : <EOL> os . unlink ( lnk ) <EOL> except OSError , ex2 : <EOL> LOG . error ( "<STR_LIT>" % ( link_name , ex2 ) ) <EOL> return False <EOL> return True <EOL> def uninstall_conf ( self ) : <EOL> """<STR_LIT>""" <EOL> app_conf_dir = os . path . abspath ( os . path . join ( self . abs_path , '<STR_LIT>' ) ) <EOL> if not os . path . isdir ( app_conf_dir ) : <EOL> return True <EOL> for name in os . listdir ( common . HUE_CONF_DIR ) : <EOL> path = os . path . join ( common . HUE_CONF_DIR , name ) <EOL> if not os . path . islink ( path ) : <EOL> continue <EOL> target = os . readlink ( path ) <EOL> target_dir = os . path . abspath ( os . path . dirname ( target ) ) <EOL> if target_dir == app_conf_dir : <EOL> try : <EOL> os . unlink ( path ) <EOL> LOG . info ( '<STR_LIT>' % ( path , target ) ) <EOL> except OSError , ex : <EOL> LOG . error ( "<STR_LIT>" % ( path , ex ) ) <EOL> return False <EOL> return True <EOL> class AppJsonEncoder ( json . JSONEncoder ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> json . JSONEncoder . __init__ ( self , ** kwargs ) <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , HueApp ) : <EOL> return obj . jsonable ( ) <EOL> return json . JSONEncoder . default ( self , obj ) </s>
