Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

139 wiersze
3.9 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_asset_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_asset_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(assets_path, 'js'),
  30. os.path.join(assets_path, 'css')]:
  31. if not os.path.exists(dir_path):
  32. os.makedirs(dir_path)
  33. # symlink app/public > assets/app
  34. for app_name in webnotes.get_all_apps(True):
  35. pymodule = webnotes.get_module(app_name)
  36. source = os.path.join(os.path.abspath(os.path.dirname(pymodule.__file__)), 'public')
  37. target = os.path.join(assets_path, app_name)
  38. if not os.path.exists(target) and os.path.exists(source):
  39. os.symlink(os.path.abspath(source), target)
  40. def build(no_compress=False):
  41. assets_path = os.path.join(webnotes.local.sites_path, "assets")
  42. for target, sources in get_build_maps().iteritems():
  43. pack(os.path.join(assets_path, target), sources, no_compress)
  44. shutil.copy(os.path.join(os.path.dirname(os.path.abspath(webnotes.__file__)), 'data', 'languages.txt'), webnotes.local.sites_path)
  45. # reset_app_html()
  46. def get_build_maps():
  47. """get all build.jsons with absolute paths"""
  48. # framework js and css files
  49. pymodules = [webnotes.get_module(app) for app in webnotes.get_all_apps(True)]
  50. app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
  51. build_maps = {}
  52. for app_path in app_paths:
  53. path = os.path.join(app_path, 'public', 'build.json')
  54. if os.path.exists(path):
  55. with open(path) as f:
  56. try:
  57. for target, sources in json.loads(f.read()).iteritems():
  58. # update app path
  59. source_paths = []
  60. for source in sources:
  61. if isinstance(source, list):
  62. s = webnotes.get_pymodule_path(source[0], *source[1].split("/"))
  63. else:
  64. s = os.path.join(app_path, source)
  65. source_paths.append(s)
  66. build_maps[target] = source_paths
  67. except Exception, e:
  68. print path
  69. raise
  70. return build_maps
  71. timestamps = {}
  72. def pack(target, sources, no_compress):
  73. from cStringIO import StringIO
  74. outtype, outtxt = target.split(".")[-1], ''
  75. jsm = JavascriptMinify()
  76. for f in sources:
  77. suffix = None
  78. if ':' in f: f, suffix = f.split(':')
  79. if not os.path.exists(f) or os.path.isdir(f): continue
  80. timestamps[f] = os.path.getmtime(f)
  81. try:
  82. with open(f, 'r') as sourcefile:
  83. data = unicode(sourcefile.read(), 'utf-8', errors='ignore')
  84. if outtype=="js" and (not no_compress) and suffix!="concat" and (".min." not in f):
  85. tmpin, tmpout = StringIO(data.encode('utf-8')), StringIO()
  86. jsm.minify(tmpin, tmpout)
  87. outtxt += unicode(tmpout.getvalue() or '', 'utf-8').strip('\n') + ';'
  88. else:
  89. outtxt += ('\n/*\n *\t%s\n */' % f)
  90. outtxt += '\n' + data + '\n'
  91. except Exception, e:
  92. print "--Error in:" + f + "--"
  93. print webnotes.get_traceback()
  94. if not no_compress and outtype == 'css':
  95. pass
  96. #outtxt = cssmin(outtxt)
  97. with open(target, 'w') as f:
  98. f.write(outtxt.encode("utf-8"))
  99. print "Wrote %s - %sk" % (target, str(int(os.path.getsize(target)/1024)))
  100. def files_dirty():
  101. for target, sources in get_build_maps().iteritems():
  102. for f in sources:
  103. if ':' in f: f, suffix = f.split(':')
  104. if not os.path.exists(f) or os.path.isdir(f): continue
  105. if os.path.getmtime(f) != timestamps.get(f):
  106. print f + ' dirty'
  107. return True
  108. else:
  109. return False