<s> import os <EOL> import re <EOL> import sys <EOL> from imp import load_source <EOL> from multiprocessing import cpu_count <EOL> from os . path import isdir , isfile , join <EOL> import click <EOL> from platformio import app , exception , util <EOL> from platformio . app import get_state_item , set_state_item <EOL> from platformio . pkgmanager import PackageManager <EOL> PLATFORM_PACKAGES = { <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] <EOL> } <EOL> def get_packages ( ) : <EOL> return PLATFORM_PACKAGES <EOL> class PlatformFactory ( object ) : <EOL> @ staticmethod <EOL> def get_clsname ( type_ ) : <EOL> return "<STR_LIT>" % ( type_ . upper ( ) [ <NUM_LIT:0> ] , type_ . lower ( ) [ <NUM_LIT:1> : ] ) <EOL> @ staticmethod <EOL> def load_module ( type_ , path ) : <EOL> module = None <EOL> try : <EOL> module = load_source ( <EOL> "<STR_LIT>" % type_ , path ) <EOL> except ImportError : <EOL> raise exception . UnknownPlatform ( type_ ) <EOL> return module <EOL> @ classmethod <EOL> @ util . memoized <EOL> def _lookup_platforms ( cls ) : <EOL> platforms = { } <EOL> for d in ( util . get_home_dir ( ) , util . get_source_dir ( ) ) : <EOL> pdir = join ( d , "<STR_LIT>" ) <EOL> if not isdir ( pdir ) : <EOL> continue <EOL> for p in sorted ( os . listdir ( pdir ) ) : <EOL> if ( p in ( "<STR_LIT>" , "<STR_LIT>" ) or not <EOL> p . endswith ( "<STR_LIT>" ) ) : <EOL> continue <EOL> type_ = p [ : - <NUM_LIT:3> ] <EOL> path = join ( pdir , p ) <EOL> try : <EOL> isplatform = hasattr ( <EOL> cls . load_module ( type_ , path ) , <EOL> cls . get_clsname ( type_ ) <EOL> ) <EOL> if isplatform : <EOL> platforms [ type_ ] = path <EOL> except exception . UnknownPlatform : <EOL> pass <EOL> return platforms <EOL> @ classmethod <EOL> def get_platforms ( cls , installed = False ) : <EOL> platforms = cls . _lookup_platforms ( ) <EOL> if not installed : <EOL> return platforms <EOL> installed_platforms = { } <EOL> for type_ in get_state_item ( "<STR_LIT>" , [ ] ) : <EOL> if type_ in platforms : <EOL> installed_platforms [ type_ ] = platforms [ type_ ] <EOL> return installed_platforms <EOL> @ classmethod <EOL> def newPlatform ( cls , type_ ) : <EOL> platforms = cls . get_platforms ( ) <EOL> if type_ not in platforms : <EOL> raise exception . UnknownPlatform ( type_ ) <EOL> _instance = getattr ( <EOL> cls . load_module ( type_ , platforms [ type_ ] ) , <EOL> cls . get_clsname ( type_ ) <EOL> ) ( ) <EOL> assert isinstance ( _instance , BasePlatform ) <EOL> return _instance <EOL> class BasePlatform ( object ) : <EOL> PACKAGES = { } <EOL> LINE_ERROR_RE = re . compile ( r"<STR_LIT>" , re . I ) <EOL> def __init__ ( self ) : <EOL> self . _found_error = False <EOL> self . _last_echo_line = None <EOL> self . _verbose_level = <NUM_LIT:3> <EOL> def get_type ( self ) : <EOL> return self . __class__ . __name__ [ : - <NUM_LIT:8> ] . lower ( ) <EOL> def get_name ( self ) : <EOL> return self . get_type ( ) . title ( ) <EOL> def get_build_script ( self ) : <EOL> builtin = join ( util . get_source_dir ( ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" % <EOL> self . get_type ( ) ) <EOL> if isfile ( builtin ) : <EOL> return builtin <EOL> raise NotImplementedError ( ) <EOL> def get_description ( self ) : <EOL> if self . __doc__ : <EOL> doclines = [ l . strip ( ) for l in self . __doc__ . splitlines ( ) if <EOL> l . strip ( ) ] <EOL> return "<STR_LIT:U+0020>" . join ( doclines [ : - <NUM_LIT:1> ] ) . strip ( ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def get_vendor_url ( self ) : <EOL> if self . __doc__ and "<STR_LIT:http>" in self . __doc__ : <EOL> return self . __doc__ [ self . __doc__ . index ( "<STR_LIT:http>" ) : ] . strip ( ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def is_embedded ( self ) : <EOL> for name , opts in self . get_packages ( ) . items ( ) : <EOL> if name == "<STR_LIT>" or opts . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> return True <EOL> return False <EOL> def get_packages ( self ) : <EOL> return self . PACKAGES <EOL> def get_package_alias ( self , pkgname ) : <EOL> return self . PACKAGES [ pkgname ] . get ( "<STR_LIT>" ) <EOL> def pkg_aliases_to_names ( self , aliases ) : <EOL> names = [ ] <EOL> for alias in aliases : <EOL> name = alias <EOL> for _name , _opts in self . get_packages ( ) . items ( ) : <EOL> if _opts . get ( "<STR_LIT>" ) == alias : <EOL> name = None <EOL> names . append ( _name ) <EOL> if name : <EOL> names . append ( name ) <EOL> return names <EOL> def get_default_packages ( self ) : <EOL> return [ k for k , v in self . get_packages ( ) . items ( ) <EOL> if v . get ( "<STR_LIT:default>" , False ) ] <EOL> def get_installed_packages ( self ) : <EOL> pm = PackageManager ( ) <EOL> return [ n for n in self . get_packages ( ) . keys ( ) if pm . is_installed ( n ) ] <EOL> def install ( self , with_packages = None , without_packages = None , <EOL> skip_default_packages = False ) : <EOL> with_packages = set ( <EOL> self . pkg_aliases_to_names ( with_packages or [ ] ) ) <EOL> without_packages = set ( <EOL> self . pkg_aliases_to_names ( without_packages or [ ] ) ) <EOL> upkgs = with_packages | without_packages <EOL> ppkgs = set ( self . get_packages ( ) . keys ( ) ) <EOL> if not upkgs . issubset ( ppkgs ) : <EOL> raise exception . UnknownPackage ( "<STR_LIT:U+002CU+0020>" . join ( upkgs - ppkgs ) ) <EOL> requirements = [ ] <EOL> for name , opts in self . get_packages ( ) . items ( ) : <EOL> if name in without_packages : <EOL> continue <EOL> elif ( name in with_packages or ( not skip_default_packages and <EOL> opts . get ( "<STR_LIT:default>" ) ) ) : <EOL> requirements . append ( name ) <EOL> pm = PackageManager ( ) <EOL> for name in requirements : <EOL> pm . install ( name ) <EOL> data = get_state_item ( "<STR_LIT>" , [ ] ) <EOL> if self . get_type ( ) not in data : <EOL> data . append ( self . get_type ( ) ) <EOL> set_state_item ( "<STR_LIT>" , data ) <EOL> return len ( requirements ) <EOL> def uninstall ( self ) : <EOL> platform = self . get_type ( ) <EOL> installed_platforms = PlatformFactory . get_platforms ( <EOL> installed = True ) . keys ( ) <EOL> if platform not in installed_platforms : <EOL> raise exception . PlatformNotInstalledYet ( platform ) <EOL> deppkgs = set ( ) <EOL> for item in installed_platforms : <EOL> if item == platform : <EOL> continue <EOL> p = PlatformFactory . newPlatform ( item ) <EOL> deppkgs = deppkgs . union ( set ( p . get_packages ( ) . keys ( ) ) ) <EOL> pm = PackageManager ( ) <EOL> for name in self . get_packages ( ) . keys ( ) : <EOL> if not pm . is_installed ( name ) or name in deppkgs : <EOL> continue <EOL> pm . uninstall ( name ) <EOL> installed_platforms . remove ( platform ) <EOL> set_state_item ( "<STR_LIT>" , installed_platforms ) <EOL> return True <EOL> def update ( self ) : <EOL> pm = PackageManager ( ) <EOL> for name in self . get_installed_packages ( ) : <EOL> pm . update ( name ) <EOL> def is_outdated ( self ) : <EOL> pm = PackageManager ( ) <EOL> obsolated = pm . get_outdated ( ) <EOL> return not set ( self . get_packages ( ) . keys ( ) ) . isdisjoint ( set ( obsolated ) ) <EOL> def configure_default_packages ( self , envoptions , targets ) : <EOL> for pkg_name in self . pkg_aliases_to_names ( [ "<STR_LIT>" ] ) : <EOL> for framework in envoptions . get ( "<STR_LIT>" , "<STR_LIT>" ) . split ( "<STR_LIT:U+002C>" ) : <EOL> framework = framework . lower ( ) . strip ( ) <EOL> if not framework : <EOL> continue <EOL> if framework in pkg_name : <EOL> self . PACKAGES [ pkg_name ] [ '<STR_LIT:default>' ] = True <EOL> self . PACKAGES [ '<STR_LIT>' ] = { "<STR_LIT:default>" : True } <EOL> if any ( [ "<STR_LIT>" in t for t in targets ] + [ "<STR_LIT>" in targets ] ) : <EOL> for _name , _opts in self . PACKAGES . iteritems ( ) : <EOL> if _opts . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> self . PACKAGES [ _name ] [ '<STR_LIT:default>' ] = True <EOL> elif "<STR_LIT>" in targets : <EOL> self . PACKAGES [ _name ] [ '<STR_LIT:default>' ] = False <EOL> def _install_default_packages ( self ) : <EOL> installed_platforms = PlatformFactory . get_platforms ( <EOL> installed = True ) . keys ( ) <EOL> if ( self . get_type ( ) in installed_platforms and <EOL> set ( self . get_default_packages ( ) ) <= <EOL> set ( self . get_installed_packages ( ) ) ) : <EOL> return True <EOL> if ( not app . get_setting ( "<STR_LIT>" ) or <EOL> self . get_type ( ) in installed_platforms or <EOL> click . confirm ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . get_type ( ) ) ) : <EOL> return self . install ( ) <EOL> else : <EOL> raise exception . PlatformNotInstalledYet ( self . get_type ( ) ) <EOL> def run ( self , variables , targets , verbose ) : <EOL> assert isinstance ( variables , list ) <EOL> assert isinstance ( targets , list ) <EOL> envoptions = { } <EOL> for v in variables : <EOL> _name , _value = v . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> envoptions [ _name . lower ( ) ] = _value <EOL> self . configure_default_packages ( envoptions , targets ) <EOL> self . _install_default_packages ( ) <EOL> self . _verbose_level = int ( verbose ) <EOL> if "<STR_LIT>" in targets : <EOL> targets . remove ( "<STR_LIT>" ) <EOL> targets . append ( "<STR_LIT:-c>" ) <EOL> if "<STR_LIT>" not in envoptions : <EOL> variables . append ( "<STR_LIT>" % self . get_build_script ( ) ) <EOL> for v in variables : <EOL> if not v . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> _ , path = v . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> if not isfile ( path ) : <EOL> raise exception . BuildScriptNotFound ( path ) <EOL> installed_packages = PackageManager . get_installed ( ) <EOL> for name , options in self . get_packages ( ) . items ( ) : <EOL> if "<STR_LIT>" not in options or name not in installed_packages : <EOL> continue <EOL> variables . append ( <EOL> "<STR_LIT>" % ( options [ '<STR_LIT>' ] . upper ( ) , name ) ) <EOL> self . _found_error = False <EOL> result = self . _run_scons ( variables , targets ) <EOL> assert "<STR_LIT>" in result <EOL> if self . _last_echo_line == "<STR_LIT:.>" : <EOL> click . echo ( "<STR_LIT>" ) <EOL> return result <EOL> def _run_scons ( self , variables , targets ) : <EOL> if "<STR_LIT>" in os . environ : <EOL> _PYTHONPATH = os . environ . get ( "<STR_LIT>" ) . split ( os . pathsep ) <EOL> else : <EOL> _PYTHONPATH = [ ] <EOL> for p in os . sys . path : <EOL> if p not in _PYTHONPATH : <EOL> _PYTHONPATH . append ( p ) <EOL> os . environ [ '<STR_LIT>' ] = os . pathsep . join ( _PYTHONPATH ) <EOL> result = util . exec_command ( <EOL> [ <EOL> os . path . normpath ( sys . executable ) , <EOL> join ( util . get_home_dir ( ) , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % self . get_job_nums ( ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , join ( util . get_source_dir ( ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] + variables + targets , <EOL> stdout = util . AsyncPipe ( self . on_run_out ) , <EOL> stderr = util . AsyncPipe ( self . on_run_err ) <EOL> ) <EOL> return result <EOL> def on_run_out ( self , line ) : <EOL> self . _echo_line ( line , level = <NUM_LIT:3> ) <EOL> def on_run_err ( self , line ) : <EOL> is_error = self . LINE_ERROR_RE . search ( line ) is not None <EOL> if is_error : <EOL> self . _found_error = True <EOL> self . _echo_line ( line , level = <NUM_LIT:1> if is_error else <NUM_LIT:2> ) <EOL> def _echo_line ( self , line , level ) : <EOL> assert <NUM_LIT:1> <= level <= <NUM_LIT:3> <EOL> fg = ( "<STR_LIT>" , "<STR_LIT>" , None ) [ level - <NUM_LIT:1> ] <EOL> if level == <NUM_LIT:3> and "<STR_LIT>" in line : <EOL> fg = "<STR_LIT>" <EOL> if level > self . _verbose_level : <EOL> click . secho ( "<STR_LIT:.>" , fg = fg , err = level < <NUM_LIT:3> , nl = False ) <EOL> self . _last_echo_line = "<STR_LIT:.>" <EOL> return <EOL> if self . _last_echo_line == "<STR_LIT:.>" : <EOL> click . echo ( "<STR_LIT>" ) <EOL> self . _last_echo_line = line <EOL> click . secho ( line , fg = fg , err = level < <NUM_LIT:3> ) <EOL> @ staticmethod <EOL> def get_job_nums ( ) : <EOL> try : <EOL> return cpu_count ( ) <EOL> except NotImplementedError : <EOL> return <NUM_LIT:1> </s>
