Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

130 linhas
3.8 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. for target, sources in json.loads(f.read()).iteritems():
  59. # update app path
  60. sources = [os.path.join(app_path, source) for source in sources]
  61. build_maps[target] = sources
  62. return build_maps
  63. timestamps = {}
  64. def pack(target, sources, no_compress):
  65. from cStringIO import StringIO
  66. outtype, outtxt = target.split(".")[-1], ''
  67. jsm = JavascriptMinify()
  68. for f in sources:
  69. suffix = None
  70. if ':' in f: f, suffix = f.split(':')
  71. if not os.path.exists(f) or os.path.isdir(f): continue
  72. timestamps[f] = os.path.getmtime(f)
  73. try:
  74. with open(f, 'r') as sourcefile:
  75. data = unicode(sourcefile.read(), 'utf-8', errors='ignore')
  76. if outtype=="js" and (not no_compress) and suffix!="concat" and (".min." not in f):
  77. tmpin, tmpout = StringIO(data.encode('utf-8')), StringIO()
  78. jsm.minify(tmpin, tmpout)
  79. outtxt += unicode(tmpout.getvalue() or '', 'utf-8').strip('\n') + ';'
  80. else:
  81. outtxt += ('\n/*\n *\t%s\n */' % f)
  82. outtxt += '\n' + data + '\n'
  83. except Exception, e:
  84. print "--Error in:" + f + "--"
  85. print webnotes.get_traceback()
  86. if not no_compress and outtype == 'css':
  87. pass
  88. #outtxt = cssmin(outtxt)
  89. with open(target, 'w') as f:
  90. f.write(outtxt.encode("utf-8"))
  91. print "Wrote %s - %sk" % (target, str(int(os.path.getsize(target)/1024)))
  92. def files_dirty():
  93. for target, sources in get_build_maps().iteritems():
  94. for f in sources:
  95. if ':' in f: f, suffix = f.split(':')
  96. if not os.path.exists(f) or os.path.isdir(f): continue
  97. if os.path.getmtime(f) != timestamps.get(f):
  98. print f + ' dirty'
  99. return True
  100. else:
  101. return False