PageRenderTime 33ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/builder/build.py

http://pyjamas.googlecode.com/
Python | 216 lines | 211 code | 4 blank | 1 comment | 3 complexity | cc876a7bbc0d661d5390ea4ba949a9bf MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import shutil
  5. from os.path import join, dirname, basename
  6. from optparse import OptionParser
  7. sys.path.append(join(dirname(__file__), "../pyjs"))
  8. import pyjs
  9. usage = """
  10. usage: %prog [options] <application name>
  11. This is the command line builder for the pyjamas project, which can be used to
  12. build Ajax applications from Python.
  13. For more information, see the website at http://pyjamas.pyworks.org/
  14. """
  15. # GWT1.2 Impl | GWT1.2 Output | Pyjamas 0.2 Platform | Pyjamas 0.2 Output
  16. # -------------+-----------------------+----------------------+----------------------
  17. # IE6 | ie6 | IE6 | ie6
  18. # Opera | opera | Opera | opera
  19. # Safari | safari | Safari | safari
  20. # -- | gecko1_8 | Mozilla | mozilla
  21. # -- | gecko | OldMoz | oldmoz
  22. # Standard | all | (default code) | all
  23. # Mozilla | gecko1_8, gecko | -- | --
  24. # Old | safari, gecko, opera | -- | --
  25. version = "%prog pyjamas version 2006-08-19"
  26. app_platforms = ['IE6', 'Opera', 'OldMoz', 'Safari', 'Mozilla']
  27. app_library_dirs = ["../library/builtins", "../library", "../addons"]
  28. def read_boilerplate(filename):
  29. return open(join(dirname(__file__), "boilerplate", filename)).read()
  30. def copy_boilerplate(filename, output_dir):
  31. filename = join(dirname(__file__), "boilerplate", filename)
  32. shutil.copy(filename, output_dir)
  33. # taken and modified from python2.4
  34. def copytree_exists(src, dst, symlinks=False):
  35. if not os.path.exists(src):
  36. return
  37. names = os.listdir(src)
  38. try:
  39. os.mkdir(dst)
  40. except:
  41. pass
  42. errors = []
  43. for name in names:
  44. if name.startswith('.svn'):
  45. continue
  46. srcname = os.path.join(src, name)
  47. dstname = os.path.join(dst, name)
  48. try:
  49. if symlinks and os.path.islink(srcname):
  50. linkto = os.readlink(srcname)
  51. os.symlink(linkto, dstname)
  52. elif os.path.isdir(srcname):
  53. copytree_exists(srcname, dstname, symlinks)
  54. else:
  55. shutil.copy2(srcname, dstname)
  56. except (IOError, os.error), why:
  57. errors.append((srcname, dstname, why))
  58. if errors:
  59. print errors
  60. def build(app_name, output="output", js_includes=()):
  61. dir_public = "public"
  62. print "Building '%(app_name)s' to output directory '%(output)s'" % locals()
  63. # check the output directory
  64. if os.path.exists(output) and not os.path.isdir(output):
  65. print >>sys.stderr, "Output destination %s exists and is not a directory" % output
  66. return
  67. if not os.path.isdir(output):
  68. try:
  69. print "Creating output directory"
  70. os.mkdir(output)
  71. except StandardError, e:
  72. print >>sys.stderr, "Exception creating output directory %s: %s" % (output, e)
  73. # Check that the app_name file exists
  74. py_app_name = app_name[:]
  75. if py_app_name[-2:] != "py":
  76. py_app_name = app_name + ".py"
  77. if app_name[-3:] == ".py":
  78. app_name = app_name[:-3]
  79. app_basename = basename(app_name)
  80. if not os.path.isfile(py_app_name):
  81. print >>sys.stderr, "Could not find %s" % py_app_name
  82. return
  83. ## public dir
  84. print "Copying: public directory"
  85. copytree_exists(dir_public, output)
  86. ## AppName.html - can be in current or public directory
  87. html_input_filename = app_name + ".html"
  88. html_output_filename = join(output, basename(html_input_filename))
  89. if os.path.isfile(html_input_filename):
  90. if not os.path.isfile(html_output_filename) or os.path.getmtime(html_input_filename) > os.path.getmtime(html_output_filename):
  91. try:
  92. shutil.copy(html_input_filename, html_output_filename)
  93. except:
  94. print >>sys.stderr, "Warning: Missing module HTML file %s" % html_input_filename
  95. print "Copying: %(html_input_filename)s" % locals()
  96. ## pygwt.js
  97. print "Copying: pygwt.js"
  98. pygwt_js_template = read_boilerplate("pygwt.js")
  99. pygwt_js_output = open(join(output, "pygwt.js"), "w")
  100. print >>pygwt_js_output, pygwt_js_template
  101. pygwt_js_output.close()
  102. ## Images
  103. print "Copying: Images and History"
  104. copy_boilerplate("tree_closed.gif", output)
  105. copy_boilerplate("tree_open.gif", output)
  106. copy_boilerplate("tree_white.gif", output)
  107. copy_boilerplate("history.html", output)
  108. ## AppName.nocache.html
  109. print "Creating: %(app_basename)s.nocache.html" % locals()
  110. home_nocache_html_template = read_boilerplate("home.nocache.html")
  111. home_nocache_html_output = open(join(output, app_basename + ".nocache.html"), "w")
  112. print >>home_nocache_html_output, home_nocache_html_template % dict(
  113. app_name = app_basename,
  114. safari_js = "Safari",
  115. ie6_js = "IE6",
  116. oldmoz_js = "OldMoz",
  117. moz_js = "Mozilla",
  118. opera_js = "Opera",
  119. )
  120. home_nocache_html_output.close()
  121. ## all.cache.html
  122. all_cache_html_template = read_boilerplate("all.cache.html")
  123. parser = pyjs.PlatformParser("platform")
  124. app_headers = ''
  125. app_body = '\n'.join(['<script type="text/javascript" src="%s"></script>'%script for script in js_includes])
  126. for platform in app_platforms:
  127. all_cache_name = platform + ".cache.html"
  128. print "Creating: " + all_cache_name
  129. parser.setPlatform(platform)
  130. app_translator = pyjs.AppTranslator(app_library_dirs, parser)
  131. app_libs = app_translator.translateLibraries(['pyjslib'])
  132. app_code = app_translator.translate(app_name)
  133. all_cache_html_output = open(join(output, all_cache_name), "w")
  134. print >>all_cache_html_output, all_cache_html_template % dict(
  135. app_name = app_basename,
  136. app_libs = app_libs,
  137. app_code = app_code,
  138. app_body = app_body,
  139. app_headers = app_headers
  140. )
  141. all_cache_html_output.close()
  142. ## Done.
  143. print "Done. You can run your app by opening '%(html_output_filename)s' in a browser" % locals()
  144. def main():
  145. global app_library_dirs
  146. global app_platforms
  147. parser = OptionParser(usage = usage, version = version)
  148. parser.add_option("-o", "--output", dest="output",
  149. help="directory to which the webapp should be written")
  150. parser.add_option("-j", "--include-js", dest="js_includes", action="append",
  151. help="javascripts to load into the same frame as the rest of the script")
  152. parser.add_option("-I", "--library_dir", dest="library_dirs", action="append",
  153. help="paths to search for python modules")
  154. parser.add_option("-P", "--platforms", dest="platforms",
  155. help="platforms to build for, comma-seperated")
  156. parser.set_defaults(output = "output", js_includes=[], library_dirs=[], platforms=(','.join(app_platforms)))
  157. (options, args) = parser.parse_args()
  158. if len(args) != 1:
  159. parser.error("incorrect number of arguments")
  160. app_library_dirs += options.library_dirs
  161. if options.platforms:
  162. app_platforms = options.platforms.split(',')
  163. build(args[0], options.output, options.js_includes)
  164. if __name__ == "__main__":
  165. main()