<s> import logging <EOL> import os <EOL> from os import listdir , access , R_OK <EOL> from os . path import join as join_path <EOL> from time import time , localtime , strftime <EOL> import json <EOL> import yaml <EOL> from paste . deploy . converters import asbool <EOL> from turbulenz_local . lib . exceptions import ApiException <EOL> from turbulenz_local . lib . validation import ValidationException <EOL> from turbulenz_local . models . gamedetails import GameDetail , PathDetail , SlugDetail , ImageDetail , ListDetail , EngineDetail , AspectRatioDetail <EOL> from turbulenz_local . models . metrics import MetricsSession <EOL> from turbulenz_local . models . apiv1 . badges import GameBadges , BadgesUnsupportedException <EOL> from turbulenz_local . models . apiv1 . leaderboards import LeaderboardsList , LeaderboardError , LeaderboardsUnsupported <EOL> from turbulenz_local . models . apiv1 . gamenotifications import GameNotificationKeysList , GameNotificationsUnsupportedException <EOL> from turbulenz_local . models . apiv1 . store import StoreList , StoreError , StoreUnsupported <EOL> from turbulenz_local . tools import get_absolute_path , create_dir , load_json_asset <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class GameError ( Exception ) : <EOL> pass <EOL> class GameNotFoundError ( GameError ) : <EOL> pass <EOL> class GameDuplicateSlugError ( GameError ) : <EOL> pass <EOL> class GameSlugNotSpecifiedError ( GameError ) : <EOL> pass <EOL> class GamePathError ( GameError ) : <EOL> pass <EOL> class GamePathNotFoundError ( GameError ) : <EOL> pass <EOL> def read_manifest ( game_path , manifest_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> game_path = get_absolute_path ( game_path ) <EOL> game_path = join_path ( game_path , manifest_name ) <EOL> f = open ( unicode ( game_path ) , '<STR_LIT:r>' ) <EOL> try : <EOL> data = yaml . load ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> except IOError as e : <EOL> LOG . error ( '<STR_LIT>' , str ( e ) ) <EOL> raise GameError <EOL> else : <EOL> return data <EOL> def write_manifest ( data , manifest_name ) : <EOL> """<STR_LIT>""" <EOL> path = data . get ( '<STR_LIT:path>' ) <EOL> if not path : <EOL> raise GamePathError ( '<STR_LIT>' + '<STR_LIT:\n>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in data . iteritems ( ) ] ) ) <EOL> path = get_absolute_path ( path ) <EOL> try : <EOL> f = open ( join_path ( path , manifest_name ) , '<STR_LIT:w>' ) <EOL> try : <EOL> yaml . dump ( data , f , default_flow_style = False ) <EOL> finally : <EOL> f . close ( ) <EOL> except IOError as e : <EOL> LOG . error ( '<STR_LIT>' , str ( e ) ) <EOL> raise GamePathError ( '<STR_LIT>' ) <EOL> class Game ( object ) : <EOL> _executable_extensions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , game_list , game_path = None , slug = None , games_root = None , deploy_enable = False , manifest_name = None ) : <EOL> self . game_list = game_list <EOL> self . slug = None <EOL> self . title = None <EOL> self . path = None <EOL> self . cover_art = ImageDetail ( self , '<STR_LIT>' ) <EOL> self . title_logo = ImageDetail ( self , '<STR_LIT>' ) <EOL> self . modified = None <EOL> self . deployed = None <EOL> self . is_temporary = True <EOL> self . plugin_main = None <EOL> self . canvas_main = None <EOL> self . flash_main = None <EOL> self . mapping_table = None <EOL> self . deploy_files = None <EOL> self . has_mapping_table = None <EOL> self . engine_version = EngineDetail ( '<STR_LIT>' ) <EOL> self . is_multiplayer = False <EOL> self . aspect_ratio = AspectRatioDetail ( '<STR_LIT>' ) <EOL> if manifest_name is None : <EOL> self . manifest_name = '<STR_LIT>' <EOL> else : <EOL> self . manifest_name = manifest_name <EOL> if game_path is not None : <EOL> self . load ( game_path , self . manifest_name ) <EOL> elif slug is not None : <EOL> self . update ( { '<STR_LIT>' : slug } ) <EOL> self . games_root = games_root <EOL> self . deploy_enable = deploy_enable <EOL> def update ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . _set_slug ( data ) <EOL> self . _set_path ( data ) <EOL> self . _set_title ( data ) <EOL> self . _set_images ( data ) <EOL> self . _set_dates ( data ) <EOL> self . plugin_main = GameDetail ( data . get ( '<STR_LIT>' ) ) <EOL> self . canvas_main = GameDetail ( data . get ( '<STR_LIT>' ) ) <EOL> self . flash_main = GameDetail ( data . get ( '<STR_LIT>' ) ) <EOL> self . mapping_table = GameDetail ( data . get ( '<STR_LIT>' ) ) <EOL> self . deploy_files = ListDetail ( data . get ( '<STR_LIT>' , [ ] ) ) <EOL> self . engine_version = EngineDetail ( data . get ( '<STR_LIT>' ) ) <EOL> self . is_multiplayer = asbool ( data . get ( '<STR_LIT>' , False ) ) <EOL> self . aspect_ratio = AspectRatioDetail ( data . get ( '<STR_LIT>' ) ) <EOL> def _set_slug ( self , data ) : <EOL> old_slug = self . slug <EOL> self . slug = SlugDetail ( data . get ( '<STR_LIT>' ) ) <EOL> if old_slug and not old_slug == self . slug : <EOL> self . game_list . change_slug ( old_slug , self . slug ) <EOL> def _set_path ( self , data ) : <EOL> self . path = PathDetail ( data . get ( '<STR_LIT:path>' ) ) <EOL> def _set_title ( self , data ) : <EOL> self . title = GameDetail ( data . get ( '<STR_LIT:title>' ) ) <EOL> def _set_images ( self , data ) : <EOL> cover_art = data . get ( '<STR_LIT>' , None ) <EOL> if cover_art : <EOL> self . cover_art = ImageDetail ( self , cover_art ) <EOL> title_logo = data . get ( '<STR_LIT>' , None ) <EOL> if title_logo : <EOL> self . title_logo = ImageDetail ( self , title_logo ) <EOL> def _set_dates ( self , data ) : <EOL> self . modified = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . deployed = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self , attrs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:path>' not in attrs . keys ( ) : <EOL> raise GamePathNotFoundError ( '<STR_LIT>' ) <EOL> if not create_dir ( attrs [ '<STR_LIT:path>' ] ) : <EOL> raise GamePathError ( '<STR_LIT>' % attrs [ '<STR_LIT:path>' ] ) <EOL> self . update ( attrs ) <EOL> t = localtime ( time ( ) ) <EOL> self . modified = strftime ( "<STR_LIT>" , t ) <EOL> write_manifest ( self . to_dict ( ) , self . manifest_name ) <EOL> self . is_temporary = False <EOL> def load ( self , game_path = None , manifest_name = None ) : <EOL> """<STR_LIT>""" <EOL> if game_path is None : <EOL> game_path = self . path <EOL> if manifest_name is None : <EOL> manifest_name = self . manifest_name <EOL> game_data = read_manifest ( game_path , manifest_name ) <EOL> game_data [ '<STR_LIT:path>' ] = game_path <EOL> self . update ( game_data ) <EOL> self . is_temporary = False <EOL> def get_path ( self ) : <EOL> return self . path <EOL> def to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT:path>' : self . path , <EOL> '<STR_LIT:title>' : self . title , <EOL> '<STR_LIT>' : self . slug , <EOL> '<STR_LIT>' : self . is_temporary , <EOL> '<STR_LIT>' : self . cover_art . image_path , <EOL> '<STR_LIT>' : self . title_logo . image_path , <EOL> '<STR_LIT>' : self . modified , <EOL> '<STR_LIT>' : self . deployed , <EOL> '<STR_LIT>' : self . plugin_main , <EOL> '<STR_LIT>' : self . canvas_main , <EOL> '<STR_LIT>' : self . flash_main , <EOL> '<STR_LIT>' : self . mapping_table , <EOL> '<STR_LIT>' : self . deploy_files . items , <EOL> '<STR_LIT>' : self . engine_version , <EOL> '<STR_LIT>' : self . is_multiplayer , <EOL> '<STR_LIT>' : self . has_notifications , <EOL> '<STR_LIT>' : self . aspect_ratio <EOL> } <EOL> for k , v in data . iteritems ( ) : <EOL> try : <EOL> data [ k ] = v . encode ( '<STR_LIT:utf8>' ) <EOL> except ( KeyError , AttributeError ) : <EOL> pass <EOL> return data <EOL> @ classmethod <EOL> def iterate_dir ( cls , path , files , directories ) : <EOL> abs_static_path = get_absolute_path ( path ) <EOL> for file_name in listdir ( abs_static_path ) : <EOL> if os . path . isdir ( os . path . join ( abs_static_path , file_name ) ) != True : <EOL> parts = path . split ( '<STR_LIT:/>' ) <EOL> if len ( parts ) > <NUM_LIT:2> : <EOL> directory = parts [ - <NUM_LIT:1> ] <EOL> else : <EOL> directory = '<STR_LIT>' <EOL> files . append ( _File ( file_name , file_name , directory , os . path . join ( abs_static_path , file_name ) ) ) <EOL> else : <EOL> if file_name not in directories : <EOL> directories [ file_name ] = _File ( file_name ) <EOL> return directories . values ( ) + files <EOL> def get_static_files ( self , game_path , request_path , path ) : <EOL> static_path = os . path . join ( game_path , path ) <EOL> static_path_obj = PathDetail ( static_path ) <EOL> files = [ ] <EOL> directories = { } <EOL> if static_path_obj . is_correct ( ) : <EOL> files = self . iterate_dir ( static_path_obj , files , directories ) <EOL> else : <EOL> raise GamePathNotFoundError ( '<STR_LIT>' ) <EOL> if len ( files ) > <NUM_LIT:0> : <EOL> return files <EOL> return [ ] <EOL> def get_asset_list ( self , request_path , path = '<STR_LIT>' ) : <EOL> if self . path . is_correct ( ) : <EOL> game_path = self . path <EOL> abs_game_path = get_absolute_path ( game_path ) <EOL> j = load_json_asset ( os . path . join ( game_path , self . mapping_table ) ) <EOL> if j : <EOL> mapping_table = j . get ( '<STR_LIT>' ) or j . get ( '<STR_LIT>' , { } ) <EOL> self . has_mapping_table = True <EOL> files = [ ] <EOL> directories = { } <EOL> len_path = len ( path ) <EOL> if not path . endswith ( '<STR_LIT:/>' ) and len_path > <NUM_LIT:0> : <EOL> len_path += <NUM_LIT:1> <EOL> for k , v in mapping_table . iteritems ( ) : <EOL> if k . startswith ( path ) : <EOL> parts = k [ len_path : ] . split ( '<STR_LIT:/>' ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> abs_file_path = os . path . join ( abs_game_path , request_path , v ) <EOL> files . append ( _File ( parts [ <NUM_LIT:0> ] , v , '<STR_LIT>' % ( request_path , v ) , abs_file_path ) ) <EOL> else : <EOL> if parts [ <NUM_LIT:0> ] not in directories : <EOL> directories [ parts [ <NUM_LIT:0> ] ] = _File ( parts [ <NUM_LIT:0> ] ) <EOL> result = directories . values ( ) + files <EOL> if len ( result ) == <NUM_LIT:0> : <EOL> raise GamePathNotFoundError ( '<STR_LIT>' % path ) <EOL> return result <EOL> else : <EOL> self . has_mapping_table = False <EOL> return self . get_static_files ( game_path , request_path , path ) <EOL> else : <EOL> raise GamePathError ( '<STR_LIT>' % self . path ) <EOL> @ property <EOL> def has_metrics ( self ) : <EOL> return MetricsSession . has_metrics ( self . slug ) <EOL> @ property <EOL> def has_assets ( self ) : <EOL> asset_list = self . get_asset_list ( '<STR_LIT>' ) <EOL> return len ( asset_list ) > <NUM_LIT:0> <EOL> @ property <EOL> def has_notifications ( self ) : <EOL> try : <EOL> GameNotificationKeysList . get ( self ) <EOL> except GameNotificationsUnsupportedException : <EOL> return False <EOL> return True <EOL> def get_versions ( self ) : <EOL> versions = [ ] <EOL> if self . path . is_correct ( ) : <EOL> abs_path = get_absolute_path ( self . path ) <EOL> slug = self . slug + '<STR_LIT:/>' <EOL> executable_extensions = self . _executable_extensions <EOL> flash_dict = None <EOL> for file_name in listdir ( abs_path ) : <EOL> if file_name . endswith ( executable_extensions ) : <EOL> version = { '<STR_LIT:title>' : os . path . basename ( file_name ) , <EOL> '<STR_LIT:url>' : slug + file_name } <EOL> if file_name . endswith ( '<STR_LIT>' ) : <EOL> if flash_dict is None : <EOL> flash_dict = { } <EOL> flash_config_path = join_path ( abs_path , '<STR_LIT>' ) <EOL> if access ( flash_config_path , R_OK ) : <EOL> f = open ( unicode ( flash_config_path ) , '<STR_LIT:r>' ) <EOL> try : <EOL> flash_dict = yaml . load ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> version [ '<STR_LIT>' ] = flash_dict <EOL> versions . append ( version ) <EOL> return versions <EOL> def set_deployed ( self ) : <EOL> self . deployed = strftime ( "<STR_LIT>" , localtime ( time ( ) ) ) <EOL> write_manifest ( self . to_dict ( ) , self . manifest_name ) <EOL> def check_completeness ( self ) : <EOL> errors = [ ] <EOL> tmp = [ ] <EOL> if not self . deploy_enable : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> if self . is_temporary : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> path = self . path <EOL> if not path or not path . is_set ( ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> path_correct = False <EOL> else : <EOL> path_correct = path . is_correct ( ) <EOL> if not path_correct : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> if tmp : <EOL> errors . append ( ( '<STR_LIT>' , { '<STR_LIT>' : tmp } ) ) <EOL> tmp = [ ] <EOL> plugin_main = self . plugin_main <EOL> canvas_main = self . canvas_main <EOL> flash_main = self . flash_main <EOL> main_correct = True <EOL> if ( not plugin_main or not plugin_main . is_set ( ) ) and ( not canvas_main or not canvas_main . is_set ( ) ) and ( not flash_main or not flash_main . is_set ( ) ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> main_correct = False <EOL> abs_path = get_absolute_path ( path ) <EOL> if plugin_main : <EOL> plugin_main = join_path ( abs_path , plugin_main ) <EOL> if not access ( plugin_main , R_OK ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> if canvas_main : <EOL> canvas_main = join_path ( abs_path , canvas_main ) <EOL> if not access ( canvas_main , R_OK ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> if flash_main : <EOL> if not flash_main . startswith ( '<STR_LIT>' ) : <EOL> flash_main = join_path ( abs_path , flash_main ) <EOL> if not access ( flash_main , R_OK ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> mapping_table = self . mapping_table <EOL> if ( not mapping_table or not mapping_table . is_set ( ) ) and not flash_main : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> elif path_correct and main_correct : <EOL> mapping_table = join_path ( abs_path , mapping_table ) <EOL> if not access ( mapping_table , R_OK ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> deploy_files = self . deploy_files <EOL> if not deploy_files or not deploy_files . is_set ( ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> engine_version = self . engine_version <EOL> if not engine_version or not engine_version . is_set ( ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> elif not engine_version . is_correct ( ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> aspect_ratio = self . aspect_ratio <EOL> if not aspect_ratio or not aspect_ratio . is_set ( ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> elif not aspect_ratio . is_correct ( ) : <EOL> tmp . append ( '<STR_LIT>' ) <EOL> if tmp : <EOL> errors . append ( ( '<STR_LIT>' , { '<STR_LIT>' : tmp } ) ) <EOL> return ( len ( errors ) == <NUM_LIT:0> , { '<STR_LIT>' : errors } ) <EOL> @ property <EOL> def can_deploy ( self ) : <EOL> completeness = self . check_completeness ( ) <EOL> return completeness [ <NUM_LIT:0> ] <EOL> def validate_yaml ( self ) : <EOL> result = { } <EOL> try : <EOL> badges = GameBadges ( self ) <EOL> except BadgesUnsupportedException : <EOL> pass <EOL> except ApiException as e : <EOL> result [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ '<STR_LIT:%s>' % e ] <EOL> } ) ] <EOL> else : <EOL> issues = badges . validate ( ) <EOL> if issues : <EOL> result [ '<STR_LIT>' ] = issues <EOL> try : <EOL> notification_keys = GameNotificationKeysList . get ( self ) <EOL> except GameNotificationsUnsupportedException : <EOL> pass <EOL> except ApiException as e : <EOL> result [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ '<STR_LIT:%s>' % e ] <EOL> } ) ] <EOL> else : <EOL> issues = notification_keys . validate ( ) <EOL> if issues : <EOL> result [ '<STR_LIT>' ] = issues <EOL> try : <EOL> leaderboards = LeaderboardsList . load ( self ) <EOL> except LeaderboardsUnsupported : <EOL> pass <EOL> except LeaderboardError as e : <EOL> result [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % e ] <EOL> } ) ] <EOL> except KeyError as e : <EOL> result [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % e ] <EOL> } ) ] <EOL> except ValidationException as e : <EOL> result [ '<STR_LIT>' ] = e . issues <EOL> else : <EOL> issues = leaderboards . issues <EOL> if issues : <EOL> result [ '<STR_LIT>' ] = leaderboards . issues <EOL> try : <EOL> store = StoreList . load ( self ) <EOL> except StoreUnsupported : <EOL> pass <EOL> except StoreError as e : <EOL> result [ '<STR_LIT>' ] = [ ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % e ] <EOL> } ) ] <EOL> except ValidationException as e : <EOL> result [ '<STR_LIT>' ] = e . issues <EOL> else : <EOL> issues = store . issues <EOL> if issues : <EOL> result [ '<STR_LIT>' ] = store . issues <EOL> try : <EOL> for v in result . itervalues ( ) : <EOL> for item in v : <EOL> if item [ <NUM_LIT:1> ] [ '<STR_LIT>' ] : <EOL> return ( result , True ) <EOL> except ( KeyError , IndexError ) : <EOL> LOG . error ( '<STR_LIT>' ) <EOL> return ( result , True ) <EOL> return ( result , False ) <EOL> def status ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> if self . is_temporary : <EOL> return '<STR_LIT>' <EOL> if type ( fields ) is not list : <EOL> fields = [ fields ] <EOL> result = '<STR_LIT>' <EOL> for field in fields : <EOL> field = self . __getattribute__ ( field ) <EOL> if not field . is_set ( ) : <EOL> result = '<STR_LIT>' <EOL> elif not field . is_correct ( ) : <EOL> return "<STR_LIT>" <EOL> return result <EOL> def get_games_root ( self ) : <EOL> return self . games_root <EOL> def _shortern ( string , length = <NUM_LIT:30> ) : <EOL> if not string : <EOL> return string <EOL> str_len = len ( string ) <EOL> if str_len > length : <EOL> return '<STR_LIT>' % ( string [ - length : ] ) <EOL> return string <EOL> class _File ( object ) : <EOL> def __init__ ( self , name , request_name = None , request_path = None , abs_file_path = None ) : <EOL> self . name = name <EOL> self . short_name = _shortern ( name ) <EOL> self . request_name = request_name <EOL> self . short_request_name = _shortern ( request_name ) <EOL> self . request_path = request_path <EOL> self . abs_file_path = abs_file_path <EOL> if abs_file_path : <EOL> try : <EOL> self . size = os . path . getsize ( abs_file_path ) <EOL> except OSError : <EOL> self . size = <NUM_LIT:0> <EOL> else : <EOL> self . size = <NUM_LIT:0> <EOL> def can_view ( self ) : <EOL> if self . request_name : <EOL> _ , ext1 = os . path . splitext ( self . name ) <EOL> _ , ext2 = os . path . splitext ( self . request_name ) <EOL> return ext1 not in [ '<STR_LIT>' ] and ext2 == '<STR_LIT>' <EOL> return False <EOL> def can_disassemble ( self ) : <EOL> if self . request_name : <EOL> _ , ext = os . path . splitext ( self . request_name ) <EOL> return ext == '<STR_LIT>' <EOL> return False <EOL> def is_json ( self ) : <EOL> if self . request_name : <EOL> abs_static_path = self . abs_file_path or get_absolute_path ( self . request_name ) <EOL> try : <EOL> json_handle = open ( abs_static_path ) <EOL> json . load ( json_handle ) <EOL> except IOError as e : <EOL> LOG . error ( str ( e ) ) <EOL> return False <EOL> except ValueError as e : <EOL> return False <EOL> else : <EOL> return False <EOL> return True <EOL> def is_directory ( self ) : <EOL> return self . request_name is None <EOL> def as_dict ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . name , <EOL> '<STR_LIT>' : self . request_name , <EOL> '<STR_LIT>' : self . can_view ( ) , <EOL> '<STR_LIT>' : self . can_disassemble ( ) , <EOL> '<STR_LIT>' : self . is_directory ( ) , <EOL> '<STR_LIT:size>' : self . get_size ( ) <EOL> } <EOL> def get_size ( self ) : <EOL> return self . size </s>
