<s> import os , subprocess , sys , glob , string <EOL> import zipfile <EOL> from datetime import date <EOL> cwd = os . path . abspath ( os . path . dirname ( sys . _getframe ( <NUM_LIT:0> ) . f_code . co_filename ) ) <EOL> os . chdir ( cwd ) <EOL> required_module_keys = [ '<STR_LIT:name>' , '<STR_LIT:version>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> module_defaults = { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % str ( date . today ( ) . year ) , <EOL> } <EOL> module_license_default = "<STR_LIT>" <EOL> def find_sdk ( config ) : <EOL> sdk = config [ '<STR_LIT>' ] <EOL> return os . path . expandvars ( os . path . expanduser ( sdk ) ) <EOL> def replace_vars ( config , token ) : <EOL> idx = token . find ( '<STR_LIT>' ) <EOL> while idx != - <NUM_LIT:1> : <EOL> idx2 = token . find ( '<STR_LIT:)>' , idx + <NUM_LIT:2> ) <EOL> if idx2 == - <NUM_LIT:1> : break <EOL> key = token [ idx + <NUM_LIT:2> : idx2 ] <EOL> if not config . has_key ( key ) : break <EOL> token = token . replace ( '<STR_LIT>' % key , config [ key ] ) <EOL> idx = token . find ( '<STR_LIT>' ) <EOL> return token <EOL> def read_ti_xcconfig ( ) : <EOL> contents = open ( os . path . join ( cwd , '<STR_LIT>' ) ) . read ( ) <EOL> config = { } <EOL> for line in contents . splitlines ( False ) : <EOL> line = line . strip ( ) <EOL> if line [ <NUM_LIT:0> : <NUM_LIT:2> ] == '<STR_LIT>' : continue <EOL> idx = line . find ( '<STR_LIT:=>' ) <EOL> if idx > <NUM_LIT:0> : <EOL> key = line [ <NUM_LIT:0> : idx ] . strip ( ) <EOL> value = line [ idx + <NUM_LIT:1> : ] . strip ( ) <EOL> config [ key ] = replace_vars ( config , value ) <EOL> return config <EOL> def generate_doc ( config ) : <EOL> docdir = os . path . join ( cwd , '<STR_LIT>' ) <EOL> if not os . path . exists ( docdir ) : <EOL> print "<STR_LIT>" % docdir <EOL> return None <EOL> try : <EOL> import markdown2 as markdown <EOL> except ImportError : <EOL> import markdown <EOL> documentation = [ ] <EOL> for file in os . listdir ( docdir ) : <EOL> if file in ignoreFiles or os . path . isdir ( os . path . join ( docdir , file ) ) : <EOL> continue <EOL> md = open ( os . path . join ( docdir , file ) ) . read ( ) <EOL> html = markdown . markdown ( md ) <EOL> documentation . append ( { file : html } ) ; <EOL> return documentation <EOL> def compile_js ( manifest , config ) : <EOL> js_file = os . path . join ( cwd , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not os . path . exists ( js_file ) : return <EOL> from compiler import Compiler <EOL> try : <EOL> import json <EOL> except : <EOL> import simplejson as json <EOL> path = os . path . basename ( js_file ) <EOL> compiler = Compiler ( cwd , manifest [ '<STR_LIT>' ] , manifest [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> method = compiler . compile_commonjs_file ( path , js_file ) <EOL> exports = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> json . dump ( { '<STR_LIT>' : compiler . exports } , exports ) <EOL> exports . close ( ) <EOL> method += '<STR_LIT>' <EOL> f = os . path . join ( cwd , '<STR_LIT>' , '<STR_LIT>' ) <EOL> c = open ( f ) . read ( ) <EOL> templ_search = '<STR_LIT>' <EOL> idx = c . find ( templ_search ) + len ( templ_search ) <EOL> before = c [ <NUM_LIT:0> : idx ] <EOL> after = """<STR_LIT>""" <EOL> newc = before + method + after <EOL> if newc != c : <EOL> x = open ( f , '<STR_LIT:w>' ) <EOL> x . write ( newc ) <EOL> x . close ( ) <EOL> def die ( msg ) : <EOL> print msg <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def warn ( msg ) : <EOL> print "<STR_LIT>" % msg <EOL> def validate_license ( ) : <EOL> c = open ( os . path . join ( cwd , '<STR_LIT>' ) ) . read ( ) <EOL> if c . find ( module_license_default ) != - <NUM_LIT:1> : <EOL> warn ( '<STR_LIT>' ) <EOL> def validate_manifest ( ) : <EOL> path = os . path . join ( cwd , '<STR_LIT>' ) <EOL> f = open ( path ) <EOL> if not os . path . exists ( path ) : die ( "<STR_LIT>" % path ) <EOL> manifest = { } <EOL> for line in f . readlines ( ) : <EOL> line = line . strip ( ) <EOL> if line [ <NUM_LIT:0> : <NUM_LIT:1> ] == '<STR_LIT:#>' : continue <EOL> if line . find ( '<STR_LIT::>' ) < <NUM_LIT:0> : continue <EOL> key , value = line . split ( '<STR_LIT::>' ) <EOL> manifest [ key . strip ( ) ] = value . strip ( ) <EOL> for key in required_module_keys : <EOL> if not manifest . has_key ( key ) : die ( "<STR_LIT>" % key ) <EOL> if module_defaults . has_key ( key ) : <EOL> defvalue = module_defaults [ key ] <EOL> curvalue = manifest [ key ] <EOL> if curvalue == defvalue : warn ( "<STR_LIT>" % key ) <EOL> return manifest , path <EOL> ignoreFiles = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ignoreDirs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def zip_dir ( zf , dir , basepath , ignore = [ ] ) : <EOL> for root , dirs , files in os . walk ( dir ) : <EOL> for name in ignoreDirs : <EOL> if name in dirs : <EOL> dirs . remove ( name ) <EOL> for file in files : <EOL> if file in ignoreFiles : continue <EOL> e = os . path . splitext ( file ) <EOL> if len ( e ) == <NUM_LIT:2> and e [ <NUM_LIT:1> ] == '<STR_LIT>' : continue <EOL> from_ = os . path . join ( root , file ) <EOL> to_ = from_ . replace ( dir , basepath , <NUM_LIT:1> ) <EOL> zf . write ( from_ , to_ ) <EOL> def glob_libfiles ( ) : <EOL> files = [ ] <EOL> for libfile in glob . glob ( '<STR_LIT>' ) : <EOL> if libfile . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> files . append ( libfile ) <EOL> return files <EOL> def build_module ( manifest , config ) : <EOL> from tools import ensure_dev_path <EOL> ensure_dev_path ( ) <EOL> rc = os . system ( "<STR_LIT>" ) <EOL> if rc != <NUM_LIT:0> : <EOL> die ( "<STR_LIT>" ) <EOL> rc = os . system ( "<STR_LIT>" ) <EOL> if rc != <NUM_LIT:0> : <EOL> die ( "<STR_LIT>" ) <EOL> moduleid = manifest [ '<STR_LIT>' ] <EOL> libpaths = '<STR_LIT>' <EOL> for libfile in glob_libfiles ( ) : <EOL> libpaths += '<STR_LIT>' % libfile <EOL> os . system ( "<STR_LIT>" % ( libpaths , moduleid ) ) <EOL> def package_module ( manifest , mf , config ) : <EOL> name = manifest [ '<STR_LIT:name>' ] . lower ( ) <EOL> moduleid = manifest [ '<STR_LIT>' ] . lower ( ) <EOL> version = manifest [ '<STR_LIT:version>' ] <EOL> modulezip = '<STR_LIT>' % ( moduleid , version ) <EOL> if os . path . exists ( modulezip ) : os . remove ( modulezip ) <EOL> zf = zipfile . ZipFile ( modulezip , '<STR_LIT:w>' , zipfile . ZIP_DEFLATED ) <EOL> modulepath = '<STR_LIT>' % ( moduleid , version ) <EOL> zf . write ( mf , '<STR_LIT>' % modulepath ) <EOL> libname = '<STR_LIT>' % moduleid <EOL> zf . write ( '<STR_LIT>' % libname , '<STR_LIT>' % ( modulepath , libname ) ) <EOL> docs = generate_doc ( config ) <EOL> if docs != None : <EOL> for doc in docs : <EOL> for file , html in doc . iteritems ( ) : <EOL> filename = string . replace ( file , '<STR_LIT>' , '<STR_LIT>' ) <EOL> zf . writestr ( '<STR_LIT>' % ( modulepath , filename ) , html ) <EOL> for dn in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if os . path . exists ( dn ) : <EOL> zip_dir ( zf , dn , '<STR_LIT>' % ( modulepath , dn ) , [ '<STR_LIT>' ] ) <EOL> zf . write ( '<STR_LIT>' , '<STR_LIT>' % modulepath ) <EOL> zf . write ( '<STR_LIT>' , '<STR_LIT>' % modulepath ) <EOL> exports_file = '<STR_LIT>' <EOL> if os . path . exists ( exports_file ) : <EOL> zf . write ( exports_file , '<STR_LIT>' % ( modulepath , exports_file ) ) <EOL> zf . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> manifest , mf = validate_manifest ( ) <EOL> validate_license ( ) <EOL> config = read_ti_xcconfig ( ) <EOL> sdk = find_sdk ( config ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( sdk , '<STR_LIT>' ) ) <EOL> sys . path . append ( os . path . join ( sdk , "<STR_LIT>" ) ) <EOL> compile_js ( manifest , config ) <EOL> build_module ( manifest , config ) <EOL> package_module ( manifest , mf , config ) <EOL> sys . exit ( <NUM_LIT:0> ) </s>
