Added wnJSCompiler that does the js preprocessing for ondemand js

This commit is contained in:
Pratik Vyas 2011-06-11 23:43:10 +05:30
parent 5b1792ae98
commit 5aacd1ea75
2 changed files with 75 additions and 1 deletions

74
cgi-bin/compilejs.py Normal file
View file

@ -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')

View file

@ -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