<s> from os . path import exists , join <EOL> import glob <EOL> import json <EOL> from pythonforandroid . logger import ( info , info_notify , warning , <EOL> Err_Style , Err_Fore ) <EOL> from pythonforandroid . util import current_directory <EOL> class Distribution ( object ) : <EOL> '''<STR_LIT>''' <EOL> ctx = None <EOL> name = None <EOL> needs_build = False <EOL> url = None <EOL> dist_dir = None <EOL> archs = [ ] <EOL> '''<STR_LIT>''' <EOL> recipes = [ ] <EOL> description = '<STR_LIT>' <EOL> def __init__ ( self , ctx ) : <EOL> self . ctx = ctx <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> self . name , '<STR_LIT:U+002CU+0020>' . join ( self . recipes ) ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> @ classmethod <EOL> def get_distribution ( cls , ctx , name = None , recipes = [ ] , allow_download = True , <EOL> force_build = False , <EOL> allow_build = True , extra_dist_dirs = [ ] , <EOL> require_perfect_match = False ) : <EOL> '''<STR_LIT>''' <EOL> existing_dists = Distribution . get_distributions ( ctx ) <EOL> needs_build = True <EOL> possible_dists = existing_dists <EOL> if name is not None and name : <EOL> possible_dists = [ d for d in possible_dists if d . name == name ] <EOL> _possible_dists = [ ] <EOL> for dist in possible_dists : <EOL> for recipe in recipes : <EOL> if recipe not in dist . recipes : <EOL> break <EOL> else : <EOL> _possible_dists . append ( dist ) <EOL> possible_dists = _possible_dists <EOL> if possible_dists : <EOL> info ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> pretty_log_dists ( possible_dists ) <EOL> else : <EOL> info ( '<STR_LIT>' ) <EOL> for dist in possible_dists : <EOL> if force_build : <EOL> continue <EOL> if ( set ( dist . recipes ) == set ( recipes ) or <EOL> ( set ( recipes ) . issubset ( set ( dist . recipes ) ) and <EOL> not require_perfect_match ) ) : <EOL> info_notify ( '<STR_LIT>' <EOL> . format ( dist . name ) ) <EOL> return dist <EOL> assert len ( possible_dists ) < <NUM_LIT:2> <EOL> if not name and possible_dists : <EOL> info ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( name , '<STR_LIT:U+002CU+0020>' . join ( recipes ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( possible_dists [ <NUM_LIT:0> ] . recipes ) ) ) <EOL> info ( '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> dist = Distribution ( ctx ) <EOL> dist . needs_build = True <EOL> if not name : <EOL> filen = '<STR_LIT>' <EOL> i = <NUM_LIT:1> <EOL> while exists ( join ( ctx . dist_dir , filen . format ( i ) ) ) : <EOL> i += <NUM_LIT:1> <EOL> name = filen . format ( i ) <EOL> dist . name = name <EOL> dist . dist_dir = join ( ctx . dist_dir , dist . name ) <EOL> dist . recipes = recipes <EOL> return dist <EOL> @ classmethod <EOL> def get_distributions ( cls , ctx , extra_dist_dirs = [ ] ) : <EOL> '''<STR_LIT>''' <EOL> if extra_dist_dirs : <EOL> warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> dist_dir = ctx . dist_dir <EOL> folders = glob . glob ( join ( dist_dir , '<STR_LIT:*>' ) ) <EOL> for dir in extra_dist_dirs : <EOL> folders . extend ( glob . glob ( join ( dir , '<STR_LIT:*>' ) ) ) <EOL> dists = [ ] <EOL> for folder in folders : <EOL> if exists ( join ( folder , '<STR_LIT>' ) ) : <EOL> with open ( join ( folder , '<STR_LIT>' ) ) as fileh : <EOL> dist_info = json . load ( fileh ) <EOL> dist = cls ( ctx ) <EOL> dist . name = folder . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> dist . dist_dir = folder <EOL> dist . needs_build = False <EOL> dist . recipes = dist_info [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in dist_info : <EOL> dist . archs = dist_info [ '<STR_LIT>' ] <EOL> dists . append ( dist ) <EOL> return dists <EOL> def save_info ( self ) : <EOL> '''<STR_LIT>''' <EOL> with current_directory ( self . dist_dir ) : <EOL> info ( '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as fileh : <EOL> json . dump ( { '<STR_LIT>' : self . name , <EOL> '<STR_LIT>' : [ arch . arch for arch in self . ctx . archs ] , <EOL> '<STR_LIT>' : self . ctx . recipe_build_order } , <EOL> fileh ) <EOL> def load_info ( self ) : <EOL> '''<STR_LIT>''' <EOL> with current_directory ( self . dist_dir ) : <EOL> filen = '<STR_LIT>' <EOL> if not exists ( filen ) : <EOL> return None <EOL> with open ( '<STR_LIT>' , '<STR_LIT:r>' ) as fileh : <EOL> dist_info = json . load ( fileh ) <EOL> return dist_info <EOL> def pretty_log_dists ( dists , log_func = info ) : <EOL> infos = [ ] <EOL> for dist in dists : <EOL> infos . append ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> name = dist . name , recipes = '<STR_LIT:U+002CU+0020>' . join ( dist . recipes ) , <EOL> archs = '<STR_LIT:U+002CU+0020>' . join ( dist . archs ) if dist . archs else '<STR_LIT>' , <EOL> Fore = Err_Fore , Style = Err_Style ) ) <EOL> for line in infos : <EOL> log_func ( '<STR_LIT:\t>' + line ) </s>
