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.

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