def compileTemplateInner(src): print "Compiling template \""+src+"\"..." assert(bash.exists(src+tmpltExt)) assert(bash.exists(src+tspecExt)) sfil = bash.readfile(src+tspecExt); params = []; types = []; for line in sfil: line.strip(); if line.startswith("@param"): tkns = line[6:].split(None, 2); #Yeah yeah, the following has a bit of code duplication. Deal if len(tkns) == 1: params.append(tkns[0]); types.append(None); elif len(tkns) > 1: if tkns[0].startswith("{") and tkns[0].endswith("}"): params.append(tkns[1]); types.append(tkns[0][1:len(tkns[0])-1]); elif tkns[1].startswith("{") and tkns[1].endswith("}"): params.append(tkns[0]); types.append(tkns[1][1:len(tkns[1])-1]); else: params.append(tkns[0]); types.append(None); sfil.close(); content = ""; tfil = bash.readfile(src+tmpltExt); tmplt = tfil.read().rstrip(); tfil.close(); tokens = re.split(r'(?:\{\{|\}\})', tmplt); for i in xrange(0, len(tokens)): token = tokens[i]; if i % 2 == 0: content += "\""+token.replace( "\\", "\\\\" ).replace( "\t", "\\t" ).replace( "\r", "" ).replace( "\f", "\\f" ).replace( "\'", "\\\'" ).replace( "\"", "\\\"" ).replace( "\n", "\"+\n\t\t\t\"")+"\""; else: token = token.strip(); char = token[len(token)-1]; if (char != "?" and char != ":" and token.count("(") == token.count(")")): token = "("+token+")"; if char != "?" and char != ":" and char != "(": token = token+"+"; if '\n' in token: token="\n\t\t\t\t"+re.sub(r'\s+',' ',token)+"\n\t\t\t"; char = token[0]; if char != "?" and char != ":" and char != ")": token = "+"+token; content += token; content = re.sub(r'([^\\])""\s*\+', r'\1', content); return types, params, content;
def compileTemplateInner(src): print "Compiling template \"" + src + "\"..." assert (bash.exists(src + tmpltExt)) assert (bash.exists(src + tspecExt)) sfil = bash.readfile(src + tspecExt) params = [] types = [] for line in sfil: line.strip() if line.startswith("@param"): tkns = line[6:].split(None, 2) #Yeah yeah, the following has a bit of code duplication. Deal if len(tkns) == 1: params.append(tkns[0]) types.append(None) elif len(tkns) > 1: if tkns[0].startswith("{") and tkns[0].endswith("}"): params.append(tkns[1]) types.append(tkns[0][1:len(tkns[0]) - 1]) elif tkns[1].startswith("{") and tkns[1].endswith("}"): params.append(tkns[0]) types.append(tkns[1][1:len(tkns[1]) - 1]) else: params.append(tkns[0]) types.append(None) sfil.close() content = "" tfil = bash.readfile(src + tmpltExt) tmplt = tfil.read().rstrip() tfil.close() tokens = re.split(r'(?:\{\{|\}\})', tmplt) for i in xrange(0, len(tokens)): token = tokens[i] if i % 2 == 0: content += "\"" + token.replace("\\", "\\\\").replace( "\t", "\\t").replace("\r", "").replace("\f", "\\f").replace( "\'", "\\\'").replace("\"", "\\\"").replace( "\n", "\"+\n\t\t\t\"") + "\"" else: token = token.strip() char = token[len(token) - 1] if (char != "?" and char != ":" and token.count("(") == token.count(")")): token = "(" + token + ")" if char != "?" and char != ":" and char != "(": token = token + "+" if '\n' in token: token = "\n\t\t\t\t" + re.sub(r'\s+', ' ', token) + "\n\t\t\t" char = token[0] if char != "?" and char != ":" and char != ")": token = "+" + token content += token content = re.sub(r'([^\\])""\s*\+', r'\1', content) return types, params, content
def runOnFile(path, consts, funs): infil = bash.readfile(path) content = "" ifs = [] consts['FILE_NAME'] = os.path.basename(path) lNum = 1 for line in infil: l = line.strip() if l == "END_IF": ifs.pop() elif re.match(r'^IF_[A-Z]+_IS(?:_NOT)? .+', l) != None: neg = re.match(r'^IF_[A-Z]+_IS_NOT .+', l) != None var = re.sub(r'^IF_([A-Z]+)_IS(?:_NOT)? .+', r'\1', l) ifs.append( (consts[var] == re.sub(r'^IF_[A-Z]+_IS(?:_NOT)? (.+)', r'\1', l ) if var in consts else False) != neg) elif re.match(r'^IF(?:_NOT)?_[A-Z]+$', l) != None: neg = re.match(r'^IF_NOT_', l) != None var = re.sub(r'IF(?:_NOT)?_', "", l) val = var in consts and consts[var] == "true" ifs.append(val != neg) elif reduce(lambda x, y: x and y, ifs, True): content += runOnText(line, consts, funs, lNum) lNum += 1 infil.close() return runOnText(content, consts, funs)
def load(path): consts = {} funs = {} name = None body = None for line in bash.readfile(path): if re.match(r'\s*{{\s*\w+\s*}}\s*=', line) == None: if body != None: body += line else: if name != None: const, fun = parseMacro(body) if const != None: consts[name] = const if fun != None: funs[name] = fun name, body = line.split("=", 1) name = name.strip() name = name[2:len(name) - 2].strip() if name != None: const, fun = parseMacro(body) if const != None: consts[name] = const if fun != None: funs[name] = fun return consts, funs
def runOnFile(path, consts, funs): infil = bash.readfile(path); content = ""; ifs = []; consts['FILE_NAME'] = os.path.basename(path); lNum = 1; for line in infil: l = line.strip(); if l == "END_IF": ifs.pop(); elif re.match(r'^IF_[A-Z]+_IS(?:_NOT)? .+', l) != None: neg = re.match(r'^IF_[A-Z]+_IS_NOT .+', l) != None; var = re.sub(r'^IF_([A-Z]+)_IS(?:_NOT)? .+', r'\1', l); ifs.append((consts[var] == re.sub(r'^IF_[A-Z]+_IS(?:_NOT)? (.+)', r'\1', l) if var in consts else False) != neg); elif re.match(r'^IF(?:_NOT)?_[A-Z]+$', l) != None: neg = re.match(r'^IF_NOT_', l) != None; var = re.sub(r'IF(?:_NOT)?_', "", l); val = var in consts and consts[var] == "true"; ifs.append(val != neg); elif reduce(lambda x,y: x and y, ifs, True): content += runOnText(line, consts, funs, lNum); lNum += 1; infil.close(); return runOnText(content, consts, funs);
def load(path): consts = {}; funs = {}; name = None; body = None; for line in bash.readfile(path): if re.match(r'\s*{{\s*\w+\s*}}\s*=', line) == None: if body != None: body += line; else: if name != None: const, fun = parseMacro(body) if const != None: consts[name] = const; if fun != None: funs[name] = fun; name, body = line.split("=", 1); name = name.strip(); name = name[2:len(name)-2].strip(); if name != None: const, fun = parseMacro(body) if const != None: consts[name] = const; if fun != None: funs[name] = fun; return consts, funs;
def compileTemplate(src, dest, package=[]): print "Compiling template \""+src+"\"..."; #Preprocess content = "\"" + bash.readfile(src+tmpltExt).read().strip( ).replace( "\\", "\\\\" ).replace( "\t", "\\t" ).replace( "\r", "" ).replace( "\f", "\\f" ).replace( "\'", "\\\'" ).replace( "\"", "\\\"" ).replace( "\n", "\"+\n\t\t\t\"" ) + "\""; varName = src[src.rfind("/")+1:]; varName = varName[0].lower() + varName[1:]; #Output outfil = bash.writefile(dest); outfil.write("var templates = templates || {};\n") packageName = "templates"; for pack in package: packageName += "."+pack; outfil.write(packageName + " = " + packageName + " || {};\n"); outfil.write(packageName + "." + varName + " = " + content + ";") outfil.close();
def clearFolder(path, protectedList=None): protected = set(['.gitignore']); if protectedList != None: infil = bash.readfile(protectedList); for row in csv.reader(infil): protected |= set(row); infil.close() for fil in bash.ls(path): if not fil in protected: bash.rm_r(path+"/"+fil);
def clearFolder(path, protectedList=None): protected = set(['.gitignore']) if protectedList != None: infil = bash.readfile(protectedList) for row in csv.reader(infil): protected |= set(row) infil.close() for fil in bash.ls(path): if not fil in protected: bash.rm_r(path + "/" + fil)
def compileTemplate(src, dest, package=[]): print "Compiling template \"" + src + "\"..." #Preprocess content = "\"" + bash.readfile(src + tmpltExt).read().strip().replace( "\\", "\\\\").replace("\t", "\\t").replace("\r", "").replace( "\f", "\\f").replace("\'", "\\\'").replace("\"", "\\\"").replace( "\n", "\"+\n\t\t\t\"") + "\"" varName = src[src.rfind("/") + 1:] varName = varName[0].lower() + varName[1:] #Output outfil = bash.writefile(dest) outfil.write("var templates = templates || {};\n") packageName = "templates" for pack in package: packageName += "." + pack outfil.write(packageName + " = " + packageName + " || {};\n") outfil.write(packageName + "." + varName + " = " + content + ";") outfil.close()
def transferLeaf(src, dest, plat, debug, mVars, mFuns, parents, merge): if merge: assert(len(parents) > 0); isDir = bash.isdir(src) wantsMacros = not (isDir and (os.path.basename(src) in noTemplating)); ext = src[src.rfind("/_")+2:] if isDir else src[src.rfind(".")+1:]; outPath = dest+("/"+ext if isDir else ""); for parent in parents: if not bash.exists(outPath): bash.mkdir(outPath); outPath += "/" + parent; if not merge and not bash.exists(outPath): bash.mkdir(outPath); if isDir: # Get list of files to transfer baseFolders = ["", "/_"+plat] + (["/"+debugFolder, "/_"+plat+"/"+debugFolder] if debug else []); # Actually transfer if merge: ofpath = outPath+"."+ext; outfil = bash.writefile(ofpath); i = 0; while i < len(baseFolders): baseFolder = src+baseFolders[i]; if bash.exists(baseFolder): fils = bash.ls(baseFolder); oFil = baseFolder+"/"+orderFile; if bash.exists(oFil): oFilReader = bash.readfile(oFil); o = oFilReader.read().strip().split(); oFilReader.close(); fils = o+list(set(fils).difference(o)); for fil in fils: fname = baseFolder+"/"+fil; if bash.isdir(fname): if fil[0] != '_': baseFolders.insert(i+1, fname[len(src):]); elif fil.endswith("."+ext): infil = bash.readfile(fname); outfil.write(infil.read()+"\n"); infil.close() i += 1; outfil.close(); if wantsMacros: macros.run(ofpath, mVars, mFuns); if ext == "js" and not debug: print "\tCompressing \""+ofpath+"\"..." bash.compressJS(ofpath, (plat != webPlat)); else: for baseFolder in baseFolders: baseFolder = src+baseFolder; if bash.exists(baseFolder): for fil in bash.ls(baseFolder): fname = baseFolder+"/"+fil; if fil[0] != '_' or not bash.isdir(fname): oPath = outPath+"/"+fil; bash.cp_r(fname, oPath); if wantsMacros: macros.run(oPath, mVars, mFuns); else: oPath = outPath + ("."+ext if merge else ""); bash.cp_r(src, oPath); if wantsMacros: macros.run(oPath, mVars, mFuns);
def makeWebXML(src, dest): print "Building web.xml..." infil = bash.readfile(src); outfl = bash.writefile(dest); outfl.write("<?xml version=\"1.0\" encoding=\"UTF-8\" "+ "standalone=\"no\"?><web-app "+ "xmlns=\"http://java.sun.com/xml/ns/javaee\" "+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+ "version=\"2.5\" "+ "xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee"+ " "+ "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\">\n" + "\n" + "\t<!-- Custom MIME types -->\n" + "\t<mime-mapping>\n" + "\t\t<extension>woff</extension>\n" + "\t\t<mime-type>font/opentype</mime-type>\n" + "\t</mime-mapping>\n" + "\n" + "\t<!-- Google Endpoint API Stuff -->\n" + "\t<servlet>\n" + "\t\t<servlet-name>SystemServiceServlet</servlet-name>\n" + "\t\t<servlet-class>" + "com.google.api.server.spi.SystemServiceServlet" + "</servlet-class>\n" + "\t\t<init-param>\n" + "\t\t\t<param-name>services</param-name>\n" + "\t\t\t<param-value/>\n" + "\t\t</init-param>\n" + "\t</servlet>\n" + "\t<servlet-mapping>\n" + "\t\t<servlet-name>SystemServiceServlet</servlet-name>\n" + "\t\t<url-pattern>/_ah/spi/*</url-pattern>\n" + "\t</servlet-mapping>\n" + "\n" + "\t<!-- - - - - - - - - - - - - - - - - - -->\n" + "\t<!-- Servlets (mostly) -->\n" + "\t<!-- - - - - - - - - - - - - - - - - - -->\n\n"); prefix = "servlets" usedServlets = set([]); for line in infil: if "," in line: elems = line.split(",") assert(len(elems) > 1); fname = elems[0] if fname.startswith("."): fname = prefix + fname url = elems[1].strip().rstrip("/"); isJSP = "JSP" in fname.upper(); tagName = "jsp-file" if isJSP else "servlet-class"; servlet = None; if len(elems) < 3: if isJSP: servlet = re.sub(r'^.*\/(.*)\.jsp$', r'\1Servlet', fname) else: servlet = re.sub(r'^.*\.(.*)$', r'\1', fname); servlet = servlet[0].lower() + servlet[1:]; if servlet in usedServlets: i = 0; while (servlet+"_"+str(i)) in usedServlets: i += 1; servlet += "_"+str(i); usedServlets.add(servlet); else: servlet = elems[2].strip(); outfl.write("\t<servlet>\n" + "\t\t<servlet-name>"+servlet+"</servlet-name>\n" + "\t\t<"+tagName+">"+fname+"</"+tagName+">\n" + "\t</servlet>\n" + "\t<servlet-mapping>\n" + "\t\t<servlet-name>"+servlet+"</servlet-name>\n" + "\t\t<url-pattern>"+url+"</url-pattern>\n" + "\t</servlet-mapping>\n" + "\t<servlet-mapping>\n" + "\t\t<servlet-name>"+servlet+"</servlet-name>\n" + "\t\t<url-pattern>"+url+"/</url-pattern>\n" + "\t</servlet-mapping>\n"); else: line = line.strip() if line.startswith("WELCOME:"): line = line[8:].strip(); outfl.write("\t<!-- Default page to serve -->\n" + "\t<welcome-file-list>\n" + "\t\t<welcome-file>"+line+"</welcome-file>\n" + "\t</welcome-file-list>\n" + "\n") elif len(line) > 0: outfl.write("\n\t<!-- "+line+" -->\n"); prefix = "servlets."+ re.sub(r' ', r'_', re.sub(r'\s*-\s+', r'.', line.lower())) outfl.write("</web-app>") outfl.close(); infil.close();
############################################################################# debug = False; local = False; server = None; for (op, val) in getopt.getopt(sys.argv[1:], "lds", ["debug", "local", "server"])[0]: debug = debug or op == "-d" or op == "--debug"; local = local or op == "-l" or op == "--local"; if op == "-s" or op == "--server": server = val; if server == None: server = localServer if local else webServer; projectsForApp = set([]); projectsForAppFil = bash.readfile("build/app-web-projects.csv"); for row in csv.reader(projectsForAppFil): projectsForApp |= set(row); projectsForAppFil.close(); makeWebXML("server/servlet-list.csv", javaProjDir+"/war/WEB-INF/web.xml"); mVars, mFuns = macros.load("build/macros"); mVars['TEST'] = "false"; mVars['DEBUG'] = "true" if debug else "false"; mVars['LOCAL'] = "true" if local else "false"; if bash.exists(javaTmpltDir): bash.rm_r(javaTmpltDir); compileServerOnlyTemplates("server/templates", mVars, mFuns);
def transferLeaf(src, dest, plat, debug, mVars, mFuns, parents, merge): if merge: assert (len(parents) > 0) isDir = bash.isdir(src) wantsMacros = not (isDir and (os.path.basename(src) in noTemplating)) ext = src[src.rfind("/_") + 2:] if isDir else src[src.rfind(".") + 1:] outPath = dest + ("/" + ext if isDir else "") for parent in parents: if not bash.exists(outPath): bash.mkdir(outPath) outPath += "/" + parent if not merge and not bash.exists(outPath): bash.mkdir(outPath) if isDir: # Get list of files to transfer baseFolders = ["", "/_" + plat] + ( ["/" + debugFolder, "/_" + plat + "/" + debugFolder] if debug else []) # Actually transfer if merge: ofpath = outPath + "." + ext outfil = bash.writefile(ofpath) i = 0 while i < len(baseFolders): baseFolder = src + baseFolders[i] if bash.exists(baseFolder): fils = bash.ls(baseFolder) oFil = baseFolder + "/" + orderFile if bash.exists(oFil): oFilReader = bash.readfile(oFil) o = oFilReader.read().strip().split() oFilReader.close() fils = o + list(set(fils).difference(o)) for fil in fils: fname = baseFolder + "/" + fil if bash.isdir(fname): if fil[0] != '_': baseFolders.insert(i + 1, fname[len(src):]) elif fil.endswith("." + ext): infil = bash.readfile(fname) outfil.write(infil.read() + "\n") infil.close() i += 1 outfil.close() if wantsMacros: macros.run(ofpath, mVars, mFuns) if ext == "js" and not debug: print "\tCompressing \"" + ofpath + "\"..." bash.compressJS(ofpath, (plat != webPlat)) else: for baseFolder in baseFolders: baseFolder = src + baseFolder if bash.exists(baseFolder): for fil in bash.ls(baseFolder): fname = baseFolder + "/" + fil if fil[0] != '_' or not bash.isdir(fname): oPath = outPath + "/" + fil bash.cp_r(fname, oPath) if wantsMacros: macros.run(oPath, mVars, mFuns) else: oPath = outPath + ("." + ext if merge else "") bash.cp_r(src, oPath) if wantsMacros: macros.run(oPath, mVars, mFuns)
def makeWebXML(src, dest): print "Building web.xml..." infil = bash.readfile(src) outfl = bash.writefile(dest) outfl.write("<?xml version=\"1.0\" encoding=\"UTF-8\" " + "standalone=\"no\"?><web-app " + "xmlns=\"http://java.sun.com/xml/ns/javaee\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "version=\"2.5\" " + "xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee" + " " + "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\">\n" + "\n" + "\t<!-- Custom MIME types -->\n" + "\t<mime-mapping>\n" + "\t\t<extension>woff</extension>\n" + "\t\t<mime-type>font/opentype</mime-type>\n" + "\t</mime-mapping>\n" + "\n" + "\t<!-- Google Endpoint API Stuff -->\n" + "\t<servlet>\n" + "\t\t<servlet-name>SystemServiceServlet</servlet-name>\n" + "\t\t<servlet-class>" + "com.google.api.server.spi.SystemServiceServlet" + "</servlet-class>\n" + "\t\t<init-param>\n" + "\t\t\t<param-name>services</param-name>\n" + "\t\t\t<param-value/>\n" + "\t\t</init-param>\n" + "\t</servlet>\n" + "\t<servlet-mapping>\n" + "\t\t<servlet-name>SystemServiceServlet</servlet-name>\n" + "\t\t<url-pattern>/_ah/spi/*</url-pattern>\n" + "\t</servlet-mapping>\n" + "\n" + "\t<!-- - - - - - - - - - - - - - - - - - -->\n" + "\t<!-- Servlets (mostly) -->\n" + "\t<!-- - - - - - - - - - - - - - - - - - -->\n\n") prefix = "servlets" usedServlets = set([]) for line in infil: if "," in line: elems = line.split(",") assert (len(elems) > 1) fname = elems[0] if fname.startswith("."): fname = prefix + fname url = elems[1].strip().rstrip("/") isJSP = "JSP" in fname.upper() tagName = "jsp-file" if isJSP else "servlet-class" servlet = None if len(elems) < 3: if isJSP: servlet = re.sub(r'^.*\/(.*)\.jsp$', r'\1Servlet', fname) else: servlet = re.sub(r'^.*\.(.*)$', r'\1', fname) servlet = servlet[0].lower() + servlet[1:] if servlet in usedServlets: i = 0 while (servlet + "_" + str(i)) in usedServlets: i += 1 servlet += "_" + str(i) usedServlets.add(servlet) else: servlet = elems[2].strip() outfl.write("\t<servlet>\n" + "\t\t<servlet-name>" + servlet + "</servlet-name>\n" + "\t\t<" + tagName + ">" + fname + "</" + tagName + ">\n" + "\t</servlet>\n" + "\t<servlet-mapping>\n" + "\t\t<servlet-name>" + servlet + "</servlet-name>\n" + "\t\t<url-pattern>" + url + "</url-pattern>\n" + "\t</servlet-mapping>\n" + "\t<servlet-mapping>\n" + "\t\t<servlet-name>" + servlet + "</servlet-name>\n" + "\t\t<url-pattern>" + url + "/</url-pattern>\n" + "\t</servlet-mapping>\n") else: line = line.strip() if line.startswith("WELCOME:"): line = line[8:].strip() outfl.write("\t<!-- Default page to serve -->\n" + "\t<welcome-file-list>\n" + "\t\t<welcome-file>" + line + "</welcome-file>\n" + "\t</welcome-file-list>\n" + "\n") elif len(line) > 0: outfl.write("\n\t<!-- " + line + " -->\n") prefix = "servlets." + re.sub( r' ', r'_', re.sub(r'\s*-\s+', r'.', line.lower())) outfl.write("</web-app>") outfl.close() infil.close()
############################################################################# debug = False local = False server = None for (op, val) in getopt.getopt(sys.argv[1:], "lds", ["debug", "local", "server"])[0]: debug = debug or op == "-d" or op == "--debug" local = local or op == "-l" or op == "--local" if op == "-s" or op == "--server": server = val if server == None: server = localServer if local else webServer projectsForApp = set([]) projectsForAppFil = bash.readfile("build/app-web-projects.csv") for row in csv.reader(projectsForAppFil): projectsForApp |= set(row) projectsForAppFil.close() makeWebXML("server/servlet-list.csv", javaProjDir + "/war/WEB-INF/web.xml") mVars, mFuns = macros.load("build/macros") mVars['TEST'] = "false" mVars['DEBUG'] = "true" if debug else "false" mVars['LOCAL'] = "true" if local else "false" if bash.exists(javaTmpltDir): bash.rm_r(javaTmpltDir) compileServerOnlyTemplates("server/templates", mVars, mFuns) platforms = ["web", "iOS"]