<s> import os , re <EOL> import jasy . core . Cache <EOL> import jasy . core . Config as Config <EOL> import jasy . core . File as File <EOL> import jasy . core . Console as Console <EOL> import jasy . core . Util as Util <EOL> import jasy . vcs . Repository as Repository <EOL> import jasy . item . Abstract <EOL> import jasy . item . Doc <EOL> import jasy . item . Translation <EOL> import jasy . item . Class <EOL> import jasy . item . Asset <EOL> from jasy import UserError <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> classExtensions = ( "<STR_LIT>" ) <EOL> translationExtensions = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> docFiles = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> repositoryFolder = re . compile ( r"<STR_LIT>" ) <EOL> projects = { } <EOL> def getProjectFromPath ( path , config = None , version = None ) : <EOL> global projects <EOL> if not path in projects : <EOL> projects [ path ] = Project ( path , config , version ) <EOL> return projects [ path ] <EOL> def getProjectDependencies ( project , checkoutDirectory = "<STR_LIT>" , updateRepositories = True ) : <EOL> """<STR_LIT>""" <EOL> def __resolve ( project ) : <EOL> name = project . getName ( ) <EOL> Console . info ( "<STR_LIT>" , Console . colorize ( name , "<STR_LIT>" ) ) <EOL> Console . indent ( ) <EOL> requires = project . getRequires ( checkoutDirectory , updateRepositories ) <EOL> Console . outdent ( ) <EOL> if not requires : <EOL> return <EOL> Console . debug ( "<STR_LIT>" , len ( requires ) ) <EOL> Console . indent ( ) <EOL> for requiredProject in reversed ( requires ) : <EOL> requiredName = requiredProject . getName ( ) <EOL> if not requiredName in names : <EOL> Console . debug ( "<STR_LIT>" , requiredName , requiredProject . version , project . getName ( ) ) <EOL> names [ requiredName ] = True <EOL> result . append ( requiredProject ) <EOL> else : <EOL> Console . debug ( "<STR_LIT>" , requiredName , requiredProject . version , project . getName ( ) ) <EOL> for requiredProject in requires : <EOL> if requiredProject . hasRequires ( ) : <EOL> __resolve ( requiredProject ) <EOL> Console . outdent ( ) <EOL> result = [ project ] <EOL> names = { <EOL> project . getName ( ) : True <EOL> } <EOL> __resolve ( project ) <EOL> return result <EOL> def getProjectNameFromPath ( path ) : <EOL> name = os . path . basename ( path ) <EOL> clone = repositoryFolder . match ( name ) <EOL> if clone is not None : <EOL> name = clone . group ( <NUM_LIT:1> ) <EOL> if "<STR_LIT:->" in name : <EOL> name = name [ : name . rindex ( "<STR_LIT:->" ) ] <EOL> return name <EOL> class Project ( ) : <EOL> kind = "<STR_LIT:none>" <EOL> scanned = False <EOL> def __init__ ( self , path , config = None , version = None ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . isdir ( path ) : <EOL> raise UserError ( "<STR_LIT>" % path ) <EOL> self . __path = os . path . abspath ( os . path . expanduser ( path ) ) <EOL> self . version = version <EOL> self . classes = { } <EOL> self . assets = { } <EOL> self . docs = { } <EOL> self . translations = { } <EOL> self . __config = Config . Config ( config ) <EOL> self . __config . loadValues ( os . path . join ( self . __path , "<STR_LIT>" ) , optional = True ) <EOL> try : <EOL> File . mkdir ( os . path . join ( self . __path , "<STR_LIT>" ) ) <EOL> self . __cache = jasy . core . Cache . Cache ( self . __path , filename = "<STR_LIT>" ) <EOL> except IOError as err : <EOL> raise UserError ( "<STR_LIT>" % ( self . __path , err ) ) <EOL> if version is None : <EOL> self . __modified = True <EOL> else : <EOL> cachedVersion = self . __cache . read ( "<STR_LIT>" ) <EOL> self . __modified = cachedVersion != version <EOL> self . __cache . store ( "<STR_LIT>" , version ) <EOL> self . __name = self . __config . get ( "<STR_LIT:name>" , getProjectNameFromPath ( self . __path ) ) <EOL> self . __requires = self . __config . get ( "<STR_LIT>" , { } ) <EOL> self . __package = self . __config . get ( "<STR_LIT>" , self . __name if self . __config . has ( "<STR_LIT:name>" ) else None ) <EOL> self . __fields = self . __config . get ( "<STR_LIT>" , { } ) <EOL> self . __setup = self . __config . get ( "<STR_LIT>" ) <EOL> def scan ( self ) : <EOL> if self . scanned : <EOL> return <EOL> updatemsg = "<STR_LIT>" if self . __modified else "<STR_LIT>" <EOL> Console . info ( "<STR_LIT>" , self . __name , Console . colorize ( updatemsg , "<STR_LIT>" ) ) <EOL> Console . indent ( ) <EOL> setup = self . __setup <EOL> if setup and self . __modified : <EOL> Console . info ( "<STR_LIT>" ) <EOL> Console . indent ( ) <EOL> for cmd in setup : <EOL> Console . info ( "<STR_LIT>" , cmd ) <EOL> result = None <EOL> try : <EOL> result = None <EOL> result = Util . executeCommand ( cmd , "<STR_LIT>" % cmd , path = self . __path ) <EOL> except Exception as ex : <EOL> if result : <EOL> Console . error ( result ) <EOL> raise UserError ( "<STR_LIT>" % ( self . __name , ex ) ) <EOL> Console . outdent ( ) <EOL> if self . __config . has ( "<STR_LIT:content>" ) : <EOL> self . kind = "<STR_LIT>" <EOL> self . __addContent ( self . __config . get ( "<STR_LIT:content>" ) ) <EOL> elif self . __hasDir ( "<STR_LIT:source>" ) : <EOL> self . kind = "<STR_LIT>" <EOL> if self . __hasDir ( "<STR_LIT>" ) : <EOL> self . __addDir ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if self . __hasDir ( "<STR_LIT>" ) : <EOL> self . __addDir ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if self . __hasDir ( "<STR_LIT>" ) : <EOL> self . __addDir ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> elif self . __hasDir ( "<STR_LIT:src>" ) : <EOL> self . kind = "<STR_LIT>" <EOL> self . __addDir ( "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> else : <EOL> self . kind = "<STR_LIT>" <EOL> if self . __hasDir ( "<STR_LIT:class>" ) : <EOL> self . __addDir ( "<STR_LIT:class>" , "<STR_LIT>" ) <EOL> if self . __hasDir ( "<STR_LIT>" ) : <EOL> self . __addDir ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if self . __hasDir ( "<STR_LIT>" ) : <EOL> self . __addDir ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> summary = [ ] <EOL> for section in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> content = getattr ( self , section , None ) <EOL> if content : <EOL> summary . append ( "<STR_LIT>" % ( len ( content ) , section ) ) <EOL> if summary : <EOL> Console . info ( "<STR_LIT>" % ( Console . colorize ( "<STR_LIT>" % self . kind , "<STR_LIT>" ) , Console . colorize ( "<STR_LIT:U+002CU+0020>" . join ( summary ) , "<STR_LIT>" ) ) ) <EOL> else : <EOL> Console . error ( "<STR_LIT>" ) <EOL> self . scanned = True <EOL> Console . outdent ( ) <EOL> def __hasDir ( self , directory ) : <EOL> full = os . path . join ( self . __path , directory ) <EOL> if os . path . exists ( full ) : <EOL> if not os . path . isdir ( full ) : <EOL> raise UserError ( "<STR_LIT>" % full ) <EOL> return True <EOL> return False <EOL> def __addContent ( self , content ) : <EOL> Console . debug ( "<STR_LIT>" ) <EOL> Console . indent ( ) <EOL> for fileId in content : <EOL> fileContent = content [ fileId ] <EOL> if len ( fileContent ) == <NUM_LIT:0> : <EOL> raise UserError ( "<STR_LIT>" ) <EOL> if fileId . endswith ( "<STR_LIT>" ) : <EOL> raise UserError ( "<STR_LIT>" % fileId ) <EOL> fileExtension = os . path . splitext ( fileContent [ <NUM_LIT:0> ] ) [ <NUM_LIT:1> ] <EOL> if len ( fileContent ) == <NUM_LIT:1> : <EOL> filePath = os . path . join ( self . __path , fileContent [ <NUM_LIT:0> ] ) <EOL> else : <EOL> filePath = [ os . path . join ( self . __path , filePart ) for filePart in fileContent ] <EOL> if fileExtension in classExtensions : <EOL> construct = jasy . item . Class . ClassItem <EOL> dist = self . classes <EOL> elif fileExtension in translationExtensions : <EOL> construct = jasy . item . Translation . TranslationItem <EOL> dist = self . translations <EOL> else : <EOL> construct = jasy . item . Asset . AssetItem <EOL> dist = self . assets <EOL> if fileId in dist : <EOL> raise UserError ( "<STR_LIT>" % fileId ) <EOL> item = construct ( self , fileId ) . attach ( filePath ) <EOL> Console . debug ( "<STR_LIT>" % ( item . kind , fileId ) ) <EOL> dist [ fileId ] = item <EOL> Console . outdent ( ) <EOL> def __addDir ( self , directory , distname ) : <EOL> Console . debug ( "<STR_LIT>" % directory ) <EOL> Console . indent ( ) <EOL> path = os . path . join ( self . __path , directory ) <EOL> if not os . path . exists ( path ) : <EOL> return <EOL> for dirPath , dirNames , fileNames in os . walk ( path ) : <EOL> for dirName in dirNames : <EOL> if dirName . startswith ( "<STR_LIT:.>" ) : <EOL> dirNames . remove ( dirName ) <EOL> if os . path . exists ( os . path . join ( dirPath , dirName , "<STR_LIT>" ) ) : <EOL> dirNames . remove ( dirName ) <EOL> relDirPath = os . path . relpath ( dirPath , path ) <EOL> for fileName in fileNames : <EOL> if fileName [ <NUM_LIT:0> ] == "<STR_LIT:.>" : <EOL> continue <EOL> relPath = os . path . normpath ( os . path . join ( relDirPath , fileName ) ) . replace ( os . sep , "<STR_LIT:/>" ) <EOL> fullPath = os . path . join ( dirPath , fileName ) <EOL> self . addFile ( relPath , fullPath , distname ) <EOL> Console . outdent ( ) <EOL> def addFile ( self , relPath , fullPath , distname , override = False ) : <EOL> fileName = os . path . basename ( relPath ) <EOL> fileExtension = os . path . splitext ( fileName ) [ <NUM_LIT:1> ] <EOL> if self . __package : <EOL> fileId = "<STR_LIT>" % self . __package <EOL> else : <EOL> fileId = "<STR_LIT>" <EOL> if fileExtension in classExtensions and distname == "<STR_LIT>" : <EOL> fileId += os . path . splitext ( relPath ) [ <NUM_LIT:0> ] <EOL> construct = jasy . item . Class . ClassItem <EOL> dist = self . classes <EOL> elif fileExtension in translationExtensions and distname == "<STR_LIT>" : <EOL> fileId += os . path . splitext ( relPath ) [ <NUM_LIT:0> ] <EOL> construct = jasy . item . Translation . TranslationItem <EOL> dist = self . translations <EOL> elif fileName in docFiles : <EOL> fileId += os . path . dirname ( relPath ) <EOL> fileId = fileId . strip ( "<STR_LIT:/>" ) <EOL> construct = jasy . item . Doc . DocItem <EOL> dist = self . docs <EOL> else : <EOL> fileId += relPath <EOL> construct = jasy . item . Asset . AssetItem <EOL> dist = self . assets <EOL> if construct != jasy . item . Asset . AssetItem : <EOL> fileId = fileId . replace ( "<STR_LIT:/>" , "<STR_LIT:.>" ) <EOL> if fileId in dist and not override : <EOL> raise UserError ( "<STR_LIT>" % fileId ) <EOL> item = construct ( self , fileId ) . attach ( fullPath ) <EOL> Console . debug ( "<STR_LIT>" % ( item . kind , fileId ) ) <EOL> dist [ fileId ] = item <EOL> def hasRequires ( self ) : <EOL> return len ( self . __requires ) > <NUM_LIT:0> <EOL> def getRequires ( self , checkoutDirectory = "<STR_LIT>" , updateRepositories = True ) : <EOL> """<STR_LIT>""" <EOL> global projects <EOL> result = [ ] <EOL> for entry in self . __requires : <EOL> if type ( entry ) is dict : <EOL> source = entry [ "<STR_LIT:source>" ] <EOL> config = Util . getKey ( entry , "<STR_LIT>" ) <EOL> version = Util . getKey ( entry , "<STR_LIT:version>" ) <EOL> kind = Util . getKey ( entry , "<STR_LIT>" ) <EOL> else : <EOL> source = entry <EOL> config = None <EOL> version = None <EOL> kind = None <EOL> if version is not None : <EOL> version = str ( version ) <EOL> revision = None <EOL> if Repository . isUrl ( source ) : <EOL> kind = kind or Repository . getType ( source ) <EOL> path = os . path . abspath ( os . path . join ( checkoutDirectory , Repository . getTargetFolder ( source , version ) ) ) <EOL> if not path in projects : <EOL> revision = Repository . update ( source , version , path , updateRepositories ) <EOL> if revision is None : <EOL> raise UserError ( "<STR_LIT>" % source ) <EOL> else : <EOL> kind = "<STR_LIT>" <EOL> if not source . startswith ( ( "<STR_LIT:/>" , "<STR_LIT>" ) ) : <EOL> path = os . path . join ( self . __path , source ) <EOL> else : <EOL> path = os . path . abspath ( os . path . expanduser ( source ) ) <EOL> if path in projects : <EOL> project = projects [ path ] <EOL> else : <EOL> fullversion = [ ] <EOL> if version is None and revision is not None : <EOL> version = "<STR_LIT>" <EOL> if version is not None : <EOL> if "<STR_LIT:/>" in version : <EOL> fullversion . append ( version [ version . rindex ( "<STR_LIT:/>" ) + <NUM_LIT:1> : ] ) <EOL> else : <EOL> fullversion . append ( version ) <EOL> if revision is not None : <EOL> if type ( revision ) is str and len ( revision ) > <NUM_LIT:20> : <EOL> fullversion . append ( revision [ : <NUM_LIT:10> ] ) <EOL> else : <EOL> fullversion . append ( revision ) <EOL> if fullversion : <EOL> fullversion = "<STR_LIT:->" . join ( fullversion ) <EOL> else : <EOL> fullversion = None <EOL> project = Project ( path , config , fullversion ) <EOL> projects [ path ] = project <EOL> result . append ( project ) <EOL> return result <EOL> def getFields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __fields <EOL> def getClassByName ( self , className ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . getClasses ( ) [ className ] <EOL> except KeyError : <EOL> return None <EOL> def getName ( self ) : <EOL> return self . __name <EOL> def getPath ( self ) : <EOL> return self . __path <EOL> def getPackage ( self ) : <EOL> return self . __package <EOL> def getConfigValue ( self , key , default = None ) : <EOL> return self . __config . get ( key , default ) <EOL> def toRelativeUrl ( self , path , prefix = "<STR_LIT>" , subpath = "<STR_LIT:source>" ) : <EOL> root = os . path . join ( self . __path , subpath ) <EOL> relpath = os . path . relpath ( path , root ) <EOL> if prefix : <EOL> if not prefix [ - <NUM_LIT:1> ] == os . sep : <EOL> prefix += os . sep <EOL> relpath = os . path . normpath ( prefix + relpath ) <EOL> return relpath . replace ( os . sep , "<STR_LIT:/>" ) <EOL> def getCache ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __cache <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> Console . info ( "<STR_LIT>" % self . __name ) <EOL> self . __cache . clear ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __cache : <EOL> self . __cache . close ( ) <EOL> self . __cache = None <EOL> self . classes = None <EOL> self . assets = None <EOL> self . docs = None <EOL> self . translations = None <EOL> def pause ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __cache . close ( ) <EOL> def resume ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __cache . open ( ) <EOL> def getDocs ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . scanned : <EOL> self . scan ( ) <EOL> return self . docs <EOL> def getClasses ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . scanned : <EOL> self . scan ( ) <EOL> return self . classes <EOL> def getAssets ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . scanned : <EOL> self . scan ( ) <EOL> return self . assets <EOL> def getTranslations ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . scanned : <EOL> self . scan ( ) <EOL> return self . translations </s>
