您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

144 行
4.1 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
  9. from cssmin import cssmin
  10. def bundle(no_compress):
  11. """concat / minify js files"""
  12. # build js files
  13. make_site_public_dirs()
  14. build(no_compress)
  15. def watch(no_compress):
  16. """watch and rebuild if necessary"""
  17. import time
  18. build(no_compress=True)
  19. while True:
  20. if files_dirty():
  21. build_js_css_packs()
  22. time.sleep(3)
  23. def make_site_public_dirs():
  24. assets_path = os.path.join(webnotes.local.sites_path, "assets")
  25. site_public_path = os.path.join(webnotes.local.site_path, 'public')
  26. for dir_path in [
  27. os.path.join(site_public_path, 'backups'),
  28. os.path.join(site_public_path, 'files'),
  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. # symlink assets > site/public/assets
  41. site_public_assets = os.path.join(site_public_path, "assets")
  42. if not os.path.exists(site_public_assets):
  43. os.symlink(os.path.abspath(assets_path), site_public_assets)
  44. def clear_pyc_files():
  45. from webnotes.utils import get_base_path
  46. for path, folders, files in os.walk(get_base_path()):
  47. for dontwalk in ('locale', '.git', 'public'):
  48. if dontwalk in folders:
  49. folders.remove(dontwalk)
  50. for f in files:
  51. if f.decode("utf-8").endswith(".pyc"):
  52. os.remove(os.path.join(path, f))
  53. def build(no_compress=False):
  54. assets_path = os.path.join(webnotes.local.sites_path, "assets")
  55. for target, sources in get_build_maps().iteritems():
  56. pack(os.path.join(assets_path, target), sources, no_compress)
  57. # reset_app_html()
  58. def get_build_maps():
  59. """get all build.jsons with absolute paths"""
  60. # framework js and css files
  61. pymodules = [webnotes.get_module(app) for app in webnotes.get_all_apps(True)]
  62. app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
  63. build_maps = {}
  64. for app_path in app_paths:
  65. path = os.path.join(app_path, 'public', 'build.json')
  66. if os.path.exists(path):
  67. with open(path) as f:
  68. for target, sources in json.loads(f.read()).iteritems():
  69. # update app path
  70. sources = [os.path.join(app_path, source) for source in sources]
  71. build_maps[target] = sources
  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