You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
4.0 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. from webnotes.utils.minify import JavascriptMinify
  5. """
  6. Build the `public` folders and setup languages
  7. """
  8. import os, sys, webnotes, json, shutil
  9. from cssmin import cssmin
  10. import webnotes.translate
  11. def bundle(no_compress):
  12. """concat / minify js files"""
  13. # build js files
  14. make_site_public_dirs()
  15. build(no_compress)
  16. webnotes.translate.clear_cache()
  17. def watch(no_compress):
  18. """watch and rebuild if necessary"""
  19. import time
  20. build(no_compress=True)
  21. while True:
  22. if files_dirty():
  23. build(no_compress=True)
  24. time.sleep(3)
  25. def make_site_public_dirs():
  26. assets_path = os.path.join(webnotes.local.sites_path, "assets")
  27. site_public_path = os.path.join(webnotes.local.site_path, 'public')
  28. for dir_path in [
  29. os.path.join(site_public_path, 'backups'),
  30. os.path.join(site_public_path, 'files'),
  31. os.path.join(assets_path, 'js'),
  32. os.path.join(assets_path, 'css')]:
  33. if not os.path.exists(dir_path):
  34. os.makedirs(dir_path)
  35. # symlink app/public > assets/app
  36. for app_name in webnotes.get_all_apps(True):
  37. pymodule = webnotes.get_module(app_name)
  38. source = os.path.join(os.path.abspath(os.path.dirname(pymodule.__file__)), 'public')
  39. target = os.path.join(assets_path, app_name)
  40. if not os.path.exists(target) and os.path.exists(source):
  41. os.symlink(os.path.abspath(source), target)
  42. def build(no_compress=False):
  43. assets_path = os.path.join(webnotes.local.sites_path, "assets")
  44. for target, sources in get_build_maps().iteritems():
  45. pack(os.path.join(assets_path, target), sources, no_compress)
  46. shutil.copy(os.path.join(os.path.dirname(os.path.abspath(webnotes.__file__)), 'data', 'languages.txt'), webnotes.local.sites_path)
  47. # reset_app_html()
  48. def get_build_maps():
  49. """get all build.jsons with absolute paths"""
  50. # framework js and css files
  51. pymodules = [webnotes.get_module(app) for app in webnotes.get_all_apps(True)]
  52. app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
  53. build_maps = {}
  54. for app_path in app_paths:
  55. path = os.path.join(app_path, 'public', 'build.json')
  56. if os.path.exists(path):
  57. with open(path) as f:
  58. try:
  59. for target, sources in json.loads(f.read()).iteritems():
  60. # update app path
  61. source_paths = []
  62. for source in sources:
  63. if isinstance(source, list):
  64. s = webnotes.get_pymodule_path(source[0], *source[1].split("/"))
  65. else:
  66. s = os.path.join(app_path, source)
  67. source_paths.append(s)
  68. build_maps[target] = source_paths
  69. except Exception, e:
  70. print path
  71. raise
  72. return build_maps
  73. timestamps = {}
  74. def pack(target, sources, no_compress):
  75. from cStringIO import StringIO
  76. outtype, outtxt = target.split(".")[-1], ''
  77. jsm = JavascriptMinify()
  78. for f in sources:
  79. suffix = None
  80. if ':' in f: f, suffix = f.split(':')
  81. if not os.path.exists(f) or os.path.isdir(f): continue
  82. timestamps[f] = os.path.getmtime(f)
  83. try:
  84. with open(f, 'r') as sourcefile:
  85. data = unicode(sourcefile.read(), 'utf-8', errors='ignore')
  86. if outtype=="js" and (not no_compress) and suffix!="concat" and (".min." not in f):
  87. tmpin, tmpout = StringIO(data.encode('utf-8')), StringIO()
  88. jsm.minify(tmpin, tmpout)
  89. outtxt += unicode(tmpout.getvalue() or '', 'utf-8').strip('\n') + ';'
  90. else:
  91. outtxt += ('\n/*\n *\t%s\n */' % f)
  92. outtxt += '\n' + data + '\n'
  93. except Exception, e:
  94. print "--Error in:" + f + "--"
  95. print webnotes.get_traceback()
  96. if not no_compress and outtype == 'css':
  97. pass
  98. #outtxt = cssmin(outtxt)
  99. with open(target, 'w') as f:
  100. f.write(outtxt.encode("utf-8"))
  101. print "Wrote %s - %sk" % (target, str(int(os.path.getsize(target)/1024)))
  102. def files_dirty():
  103. for target, sources in get_build_maps().iteritems():
  104. for f in sources:
  105. if ':' in f: f, suffix = f.split(':')
  106. if not os.path.exists(f) or os.path.isdir(f): continue
  107. if os.path.getmtime(f) != timestamps.get(f):
  108. print f + ' dirty'
  109. return True
  110. else:
  111. return False