jsdependency and compilejs.py tested OK

This commit is contained in:
Pratik Vyas 2011-06-13 17:10:36 +05:30
parent c22a25d204
commit 589f916daa
5 changed files with 107 additions and 50 deletions

View file

@ -1,11 +1,12 @@
class wnJSCompiler:
@staticmethod
def concate_files_in_dir(self,path,dest):
def concate_files_in_dir(path,dest):
"""
Concatenates all files in a directory
"""
import os
allfiles = []
dirname = path
l = os.listdir(path)
for i in l:
if os.path.isfile(os.path.join(dirname,i)):
@ -19,7 +20,7 @@ class wnJSCompiler:
@staticmethod
def getsubs(self,path):
def getsubs(path):
"""
gets all the sub directories of a directory (recursive)
"""
@ -30,42 +31,43 @@ class wnJSCompiler:
subs.append(os.path.join(root,i))
return subs
@staticmethod
def compilejs(self,path):
def compilejs(path):
"""
Compiles the js tree for ondemand import
"""
if not wnJSCompiler.is_changed(path):
return
import os
import webnotes.utils.jsnamespace as jsn
subs = self.getsubs(path)
subs = wnJSCompiler.getsubs(path)
for subdir in subs:
modname = jsn.jsNamespace.getmodname(subdir)
self.concate_files_in_dir(subdir,os.path.join(subdir, modname))
self.minifyjs(os.path.join(subdir, modname))
wnJSCompiler.concate_files_in_dir(subdir,os.path.join(subdir, modname))
wnJSCompiler.minifyjs(os.path.join(subdir, modname))
@staticmethod
def minifyjs(self,modpath):
def is_changed(path):
#compare new timestamps with the ones stored in file
from webnotes.utils import jstimestamp
try:
frm_file = jstimestamp.generateTimestamp.read_ts_from_file(path)
newts = jstimestamp.generateTimestamp.gents(path)
except IOError:
return True
if frm_file == newts:
return False
else:
return True
@staticmethod
def minifyjs(modpath):
"""
Stub to minify js
"""
pass
@staticmethod
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

@ -28,9 +28,10 @@ try:
global jsonout
import webnotes.utils.jsnamespace as jsn
filename = jsn.jsNamespace.modname_to_filename(module_name,jsdir)
print 'filename is ' + filename
import os
try:
f = open(os.path.join(filename)
f = open(os.path.join(filename))
try:
out = f.read()
finally:
@ -40,6 +41,11 @@ try:
jsonout[module_name]=out
def load_js_module(module_name):
from webnotes import defs
devmode = getattr(defs,'developer_mode')
if devmode:
import compilejs
compilejs.wnJSCompiler.compilejs(jsdir)
if module_name not in jsonout:
dependent_mods = get_dependencies(module_name)
for module in dependent_mods:
@ -48,7 +54,8 @@ try:
def get_dependencies(module_name):
import webnotes.utils.jsdependency as jsd
ret = jsd.jsDependencyBuilder.build_dependency(module_name)
print 'module_name is ' + module_name
ret = jsd.jsDependencyBuilder.build_dependency(jsdir,module_name)
return ret
@ -67,7 +74,7 @@ try:
except:
pass
load_js_module('test.js')
load_js_module('wntest.a.s')
if compress and len(out)>512:
out_buf = compress_string(str_out)
@ -88,4 +95,4 @@ try:
except Exception, e:
print "Content-Type: text/javascript"
print
print getTraceback().replace('\n','<br>')
print getTraceback()#.replace('\n','<br>')

View file

@ -1,6 +1,6 @@
class jsDependencyBuilder:
@staticmethod
def read_code(self,js_path):
def read_code(js_path):
try:
f = open(js_path)
try:
@ -11,19 +11,21 @@ class jsDependencyBuilder:
raise e
return code
@staticmethod
def read_imports(self,code):
def read_imports(code):
import re
p = re.compile('\$import\(\' (?P<name> [^)]*)\' \)', re.VERBOSE)
return p.findall(code)
@staticmethod
def build_dependency(self,modname,depends= set()):
def build_dependency(jsdir,modname,depends= set()):
import webnotes.utils.jsnamespace as jsn
jsdir = '../js'
js_path = jsn.jsNamespace.modname_to_filename(modname,jsdir)
code = self.read_code(js_path)
curdepend = self.read_imports(code)
code = jsDependencyBuilder.read_code(js_path)
curdepend = jsDependencyBuilder.read_imports(code)
for i in curdepend:
if i not in depends:
depends.add(i)
depends = depends.union( self.build_dependency(i,depends))
depends = depends.union( jsDependencyBuilder.build_dependency(jsdir,i,depends))
return depends
def build_dependency_from_file(modname,depends= set()):
# TODO STUB to read dependency from dependency tree stored in file
return jsDependencyBuilder.build_dependency(modname)

View file

@ -1,25 +1,43 @@
class jsNamespace:
@staticmethod
package_prefix = '_'
@staticmethod
def modname_to_filename(modname,jsdir, ext='.js'):
import os
path = modname.replace('.',os.sep)
path = os.path.join(jsdir,path)
if os.path.isdir(path):
#package
packagename = self.package_prefix + path.split(os.sep)[-1]
if jsNamespace.is_package(modname,jsdir):
packagename = jsNamespace.getmodfilename(modname)
path = os.path.join(path,packagename)
elif os.path.isfile(path + ext):
elif jsNamespace.is_jsfile(modname,jsdir,ext):
path = path + ext
else:
path = 'notf' # TODO raise exception that it doesnt exist
return path
@staticmethod
def is_package(modname,jsdir):
import os
path = modname.replace('.',os.sep)
path = os.path.join(jsdir,path)
return os.path.isdir(path)
@staticmethod
def is_jsfile(modname,jsdir,ext='.js'):
import os
path = modname.replace('.',os.sep)
path = os.path.join(jsdir,path)
return os.path.isfile(path + ext)
def getmodname(self,modpath,ext='.js'):
@staticmethod
def getmodname(modpath,ext='.js'):
"""
returns filename for the stiched file
"""
import os
b = modpath.split(os.sep)
modname = package_prefix + b[-1] + ext
modname = jsNamespace.package_prefix + b[-1] + ext
return modname
@staticmethod
def getmodfilename(modname,ext='.js'):
ret = modname.split('.')
ret = ret[-1]
ret = jsNamespace.getmodname(ret)
return ret

View file

@ -1,5 +1,7 @@
class generateTimestamp:
def list_js_files(self,jsdir,ext='js'):
ts_filename = 'timestamp.json'
@staticmethod
def list_js_files(jsdir,ext='js'):
import os
all_files= []
nono = ['./tiny_mce','./jquery']
@ -7,7 +9,7 @@ class generateTimestamp:
os.chdir(jsdir)
# TODO Sanitize the loop below
for root, subfolders, files in os.walk('.'):
if self.is_allowed(nono,root):
if generateTimestamp.is_allowed(nono,root):
for filename in files:
if filename.endswith(ext):
all_files.append(os.path.join(root,filename))
@ -19,20 +21,22 @@ class generateTimestamp:
all_files.remove(j)
return all_files
def is_allowed(self,disallowed,item):
@staticmethod
def is_allowed(disallowed,item):
for i in disallowed:
if item.startswith(i):
return False
return True
def get_timestamp_dict(self,jsdir,filelist):
@staticmethod
def get_timestamp_dict(jsdir,filelist):
tsdict={}
import os
import webnotes.modules as webmod
oldcwd = os.getcwd()
os.chdir(jsdir)
for filename in self.list_js_files('.'):
for filename in generateTimestamp.list_js_files('.'):
ts = webmod.get_file_timestamp(filename)
filename = filename.lstrip('./')
filename = filename.rstrip('.js')
@ -41,6 +45,30 @@ class generateTimestamp:
os.chdir(oldcwd)
return tsdict
def gents(self,jsdir):
fl=self.list_js_files(jsdir)
return self.get_timestamp_dict(jsdir,fl)
@staticmethod
def read_ts_from_file(jsdir):
import json
filename=generateTimestamp.ts_filename
f = open(generateTimestamp.ts_filename)
tsjson = eval(f.read())
f.close()
ret = json.loads(tsjson)
return ret
@staticmethod
def gents(jsdir):
fl=generateTimestamp.list_js_files(jsdir)
return generateTimestamp.get_timestamp_dict(jsdir,fl)
@staticmethod
def gentsfile(jsdir):
"""
function to generate timestamps of all files in spath
dpath is the file in which the timestamps JSON is stored
"""
import json
import os
tsdict = generateTimestamp.gents(jsdir)
f = open(os.path.join(jsdir,generateTimestamp.ts_filename),'w')
f.write(json.dumps(tsdict))
f.close()