From 5aacd1ea75ff55d849c9700b39ceca9a26915f80 Mon Sep 17 00:00:00 2001 From: Pratik Vyas Date: Sat, 11 Jun 2011 23:43:10 +0530 Subject: [PATCH] Added wnJSCompiler that does the js preprocessing for ondemand js --- cgi-bin/compilejs.py | 74 +++++++++++++++++++++++++++ cgi-bin/webnotes/utils/jstimestamp.py | 2 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 cgi-bin/compilejs.py diff --git a/cgi-bin/compilejs.py b/cgi-bin/compilejs.py new file mode 100644 index 0000000000..66e30e71d5 --- /dev/null +++ b/cgi-bin/compilejs.py @@ -0,0 +1,74 @@ +class wnJSCompiler: + def concate_files_in_subdirs(self,path,dest): + """ + Concatenates all files in a subdir (recursive) + """ + import os + allfiles = [] + for root, subdir, files in os.walk(path): + for filename in files: + allfiles.append(os.path.join(root,filename)) + print path + print allfiles + print '----' + fout = open(dest,'w') + for filename in allfiles: + f = open(filename) + fout.write(f.read()) + f.close + fout.close + + + def getsubs(self,path): + """ + gets all the sub directories of a directory (recursive) + """ + import os + subs = [] + for root, subd, files in os.walk(path): + for i in subd: + subs.append(os.path.join(root,i)) + return subs + def compilejs(self,path): + """ + Compiles the js tree for ondemand import + """ + import os + subs = self.getsubs(path) + for subdir in subs: + modname = self.getmodname(subdir) + self.concate_files_in_subdirs(subdir,os.path.join(subdir, modname)) + self.minifyjs(os.path.join(subdir, modname)) + + def minifyjs(self,modpath): + """ + Stub to minify js + """ + pass + + def getmodname(self,modpath,ext='.js'): + """ + returns filename for the stiched file + """ + import os + b = modpath.split(os.sep) + modname = b[-1] + ext + return modname + + + def gentsfile(self,spath,dpath): + """ + function to generate timestamps of all files in spath + dpath is the file in which the timestamps JSON is stored + """ + import webnotes.utils.jstimestamp as jst + import json + a = jst.generateTimestamp() + f = open(dpath,'w') + f.write('wn = {}\n') + f.write('wn.timestamp = ') + f.write(json.dumps(a.gents(spath))) + f.close() +if __name__=="__main__": + a = wnJSCompiler() + print a.compilejs('../js/wntest') diff --git a/cgi-bin/webnotes/utils/jstimestamp.py b/cgi-bin/webnotes/utils/jstimestamp.py index 78e9247ee2..49b6cd1c49 100644 --- a/cgi-bin/webnotes/utils/jstimestamp.py +++ b/cgi-bin/webnotes/utils/jstimestamp.py @@ -5,6 +5,7 @@ class generateTimestamp: nono = ['./tiny_mce','./jquery'] oldcwd = os.getcwd() os.chdir(jsdir) + # TODO Sanitize the loop below for root, subfolders, files in os.walk('.'): if self.is_allowed(nono,root): for filename in files: @@ -36,7 +37,6 @@ class generateTimestamp: filename = filename.lstrip('./') filename = filename.rstrip('.js') filename = filename.replace('/','.') - print filename tsdict[filename] = ts os.chdir(oldcwd) return tsdict