svgui/pyjs/build.py
changeset 1826 91796f408540
parent 1783 3311eea28d56
child 1832 0f1081928d65
equal deleted inserted replaced
1825:bcb50e736a46 1826:91796f408540
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 
     2 
       
     3 from __future__ import print_function
     3 import sys
     4 import sys
     4 import os
     5 import os
     5 import shutil
     6 import shutil
     6 from copy import copy
     7 from copy import copy
     7 from os.path import join, dirname, basename, abspath, split, isfile, isdir
     8 from os.path import join, dirname, basename, abspath, split, isfile, isdir
   101             else:
   102             else:
   102                 shutil.copy2(srcname, dstname)
   103                 shutil.copy2(srcname, dstname)
   103         except (IOError, os.error), why:
   104         except (IOError, os.error), why:
   104             errors.append((srcname, dstname, why))
   105             errors.append((srcname, dstname, why))
   105     if errors:
   106     if errors:
   106         print errors
   107         print(errors)
   107 
   108 
   108 
   109 
   109 def check_html_file(source_file, dest_path):
   110 def check_html_file(source_file, dest_path):
   110     """ Checks if a base HTML-file is available in the PyJamas
   111     """ Checks if a base HTML-file is available in the PyJamas
   111         output directory.
   112         output directory.
   173     # directory or at the place given if it is an absolute path.
   174     # directory or at the place given if it is an absolute path.
   174     output = os.path.abspath(output)
   175     output = os.path.abspath(output)
   175     msg = "Building '%(app_name)s' to output directory '%(output)s'" % locals()
   176     msg = "Building '%(app_name)s' to output directory '%(output)s'" % locals()
   176     if debug:
   177     if debug:
   177         msg += " with debugging statements"
   178         msg += " with debugging statements"
   178     print msg
   179     print(msg)
   179 
   180 
   180     # check the output directory
   181     # check the output directory
   181     if os.path.exists(output) and not os.path.isdir(output):
   182     if os.path.exists(output) and not os.path.isdir(output):
   182         print >>sys.stderr, "Output destination %s exists and is not a directory" % output
   183         print("Output destination %s exists and is not a directory" % output, file=sys.stderr)
   183         return
   184         return
   184     if not os.path.isdir(output):
   185     if not os.path.isdir(output):
   185         try:
   186         try:
   186             print "Creating output directory"
   187             print("Creating output directory")
   187             os.mkdir(output)
   188             os.mkdir(output)
   188         except StandardError, e:
   189         except StandardError, e:
   189             print >>sys.stderr, "Exception creating output directory %s: %s" % (output, e)
   190             print("Exception creating output directory %s: %s" % (output, e), file=sys.stderr)
   190 
   191 
   191     # public dir
   192     # public dir
   192     for p in pyjs.path:
   193     for p in pyjs.path:
   193         pub_dir = join(p, 'public')
   194         pub_dir = join(p, 'public')
   194         if isdir(pub_dir):
   195         if isdir(pub_dir):
   195             print "Copying: public directory of library %r" % p
   196             print("Copying: public directory of library %r" % p)
   196             copytree_exists(pub_dir, output)
   197             copytree_exists(pub_dir, output)
   197 
   198 
   198     # AppName.html - can be in current or public directory
   199     # AppName.html - can be in current or public directory
   199     html_input_filename = app_name + ".html"
   200     html_input_filename = app_name + ".html"
   200     html_output_filename = join(output, basename(html_input_filename))
   201     html_output_filename = join(output, basename(html_input_filename))
   203                os.path.getmtime(html_input_filename) > \
   204                os.path.getmtime(html_input_filename) > \
   204                os.path.getmtime(html_output_filename):
   205                os.path.getmtime(html_output_filename):
   205             try:
   206             try:
   206                 shutil.copy(html_input_filename, html_output_filename)
   207                 shutil.copy(html_input_filename, html_output_filename)
   207             except Exception:
   208             except Exception:
   208                 print >>sys.stderr, "Warning: Missing module HTML file %s" % html_input_filename
   209                 print("Warning: Missing module HTML file %s" % html_input_filename, file=sys.stderr)
   209 
   210 
   210             print "Copying: %(html_input_filename)s" % locals()
   211             print("Copying: %(html_input_filename)s" % locals())
   211 
   212 
   212     if check_html_file(html_input_filename, output):
   213     if check_html_file(html_input_filename, output):
   213         print >>sys.stderr, "Warning: Module HTML file %s has been auto-generated" % html_input_filename
   214         print("Warning: Module HTML file %s has been auto-generated" % html_input_filename, file=sys.stderr)
   214 
   215 
   215     # pygwt.js
   216     # pygwt.js
   216 
   217 
   217     print "Copying: pygwt.js"
   218     print("Copying: pygwt.js")
   218 
   219 
   219     pygwt_js_template = read_boilerplate(data_dir, "pygwt.js")
   220     pygwt_js_template = read_boilerplate(data_dir, "pygwt.js")
   220     pygwt_js_output = open(join(output, "pygwt.js"), "w")
   221     pygwt_js_output = open(join(output, "pygwt.js"), "w")
   221 
   222 
   222     print >>pygwt_js_output, pygwt_js_template
   223     print(pygwt_js_template, file=pygwt_js_output)
   223 
   224 
   224     pygwt_js_output.close()
   225     pygwt_js_output.close()
   225 
   226 
   226     # Images
   227     # Images
   227 
   228 
   228     print "Copying: Images and History"
   229     print("Copying: Images and History")
   229     copy_boilerplate(data_dir, "corner_dialog_topleft_black.png", output)
   230     copy_boilerplate(data_dir, "corner_dialog_topleft_black.png", output)
   230     copy_boilerplate(data_dir, "corner_dialog_topright_black.png", output)
   231     copy_boilerplate(data_dir, "corner_dialog_topright_black.png", output)
   231     copy_boilerplate(data_dir, "corner_dialog_bottomright_black.png", output)
   232     copy_boilerplate(data_dir, "corner_dialog_bottomright_black.png", output)
   232     copy_boilerplate(data_dir, "corner_dialog_bottomleft_black.png", output)
   233     copy_boilerplate(data_dir, "corner_dialog_bottomleft_black.png", output)
   233     copy_boilerplate(data_dir, "corner_dialog_edge_black.png", output)
   234     copy_boilerplate(data_dir, "corner_dialog_edge_black.png", output)
   245     app_files = generateAppFiles(data_dir, js_includes, app_name, debug,
   246     app_files = generateAppFiles(data_dir, js_includes, app_name, debug,
   246                                  output, dynamic, cache_buster, optimize)
   247                                  output, dynamic, cache_buster, optimize)
   247 
   248 
   248     # AppName.nocache.html
   249     # AppName.nocache.html
   249 
   250 
   250     print "Creating: %(app_name)s.nocache.html" % locals()
   251     print("Creating: %(app_name)s.nocache.html" % locals())
   251 
   252 
   252     home_nocache_html_template = read_boilerplate(data_dir, "home.nocache.html")
   253     home_nocache_html_template = read_boilerplate(data_dir, "home.nocache.html")
   253     home_nocache_html_output = open(join(output, app_name + ".nocache.html"),
   254     home_nocache_html_output = open(join(output, app_name + ".nocache.html"),
   254                                     "w")
   255                                     "w")
   255 
   256 
   256     # the selector templ is added to the selectScript function
   257     # the selector templ is added to the selectScript function
   257     select_tmpl = """O(["true","%s"],"%s");"""
   258     select_tmpl = """O(["true","%s"],"%s");"""
   258     script_selectors = StringIO()
   259     script_selectors = StringIO()
   259 
   260 
   260     for platform, file_prefix in app_files:
   261     for platform, file_prefix in app_files:
   261         print >> script_selectors, select_tmpl % (platform, file_prefix)
   262         print(select_tmpl % (platform, file_prefix), file=script_selectors)
   262 
   263 
   263     print >>home_nocache_html_output, home_nocache_html_template % dict(
   264     print(
   264         app_name=app_name,
   265         home_nocache_html_template % dict(
   265         script_selectors=script_selectors.getvalue(),
   266             app_name=app_name,
   266     )
   267             script_selectors=script_selectors.getvalue(),
       
   268         ), file=home_nocache_html_output)
   267 
   269 
   268     home_nocache_html_output.close()
   270     home_nocache_html_output.close()
   269 
   271 
   270     print "Done. You can run your app by opening '%(html_output_filename)s' in a browser" % locals()
   272     print("Done. You can run your app by opening '%(html_output_filename)s' in a browser" % locals())
   271 
   273 
   272 
   274 
   273 def generateAppFiles(data_dir, js_includes, app_name, debug, output, dynamic,
   275 def generateAppFiles(data_dir, js_includes, app_name, debug, output, dynamic,
   274                      cache_buster, optimize):
   276                      cache_buster, optimize):
   275 
   277 
   278 
   280 
   279     # clean out the old ones first
   281     # clean out the old ones first
   280     for name in os.listdir(output):
   282     for name in os.listdir(output):
   281         if CACHE_HTML_PAT.match(name):
   283         if CACHE_HTML_PAT.match(name):
   282             p = join(output, name)
   284             p = join(output, name)
   283             print "Deleting existing app file %s" % p
   285             print("Deleting existing app file %s" % p)
   284             os.unlink(p)
   286             os.unlink(p)
   285 
   287 
   286     app_files = []
   288     app_files = []
   287     tmpl = read_boilerplate(data_dir, "all.cache.html")
   289     tmpl = read_boilerplate(data_dir, "all.cache.html")
   288     parser = pyjs.PlatformParser("platform")
   290     parser = pyjs.PlatformParser("platform")
   332         pover[platform].update(app_translator.overrides.items())
   334         pover[platform].update(app_translator.overrides.items())
   333         for mname, name in app_translator.overrides.items():
   335         for mname, name in app_translator.overrides.items():
   334             pd = overrides.setdefault(mname, {})
   336             pd = overrides.setdefault(mname, {})
   335             pd[platform] = name
   337             pd[platform] = name
   336 
   338 
   337         print appcode
   339         print(appcode)
   338         # mod_code[platform][app_name] = appcode
   340         # mod_code[platform][app_name] = appcode
   339 
   341 
   340         # platform.Module.cache.js
   342         # platform.Module.cache.js
   341 
   343 
   342         modules_done = ['pyjslib', 'sys', '_pyjs.js']
   344         modules_done = ['pyjslib', 'sys', '_pyjs.js']
   430             if override_name in pover[platform]:
   432             if override_name in pover[platform]:
   431                 mod_cache_name = "%s.cache.js" % (override_name)
   433                 mod_cache_name = "%s.cache.js" % (override_name)
   432             else:
   434             else:
   433                 mod_cache_name = "%s.cache.js" % (mod_name)
   435                 mod_cache_name = "%s.cache.js" % (mod_name)
   434 
   436 
   435             print "Creating: " + mod_cache_name
   437             print("Creating: " + mod_cache_name)
   436 
   438 
   437             modlevels = make_deps(None, dependencies, dependencies[mod_name])
   439             modlevels = make_deps(None, dependencies, dependencies[mod_name])
   438 
   440 
   439             modnames = []
   441             modnames = []
   440 
   442 
   454             if dynamic:
   456             if dynamic:
   455                 mod_cache_html_output = open(join(output, mod_cache_name), "w")
   457                 mod_cache_html_output = open(join(output, mod_cache_name), "w")
   456             else:
   458             else:
   457                 mod_cache_html_output = StringIO()
   459                 mod_cache_html_output = StringIO()
   458 
   460 
   459             print >>mod_cache_html_output, mod_cache_html_template % dict(
   461             print(mod_cache_html_template % dict(
   460                 mod_name=mod_name,
   462                 mod_name=mod_name,
   461                 app_name=app_name,
   463                 app_name=app_name,
   462                 modnames=modnames,
   464                 modnames=modnames,
   463                 overrides=overnames,
   465                 overrides=overnames,
   464                 mod_libs=mod_libs[platform][mod_name],
   466                 mod_libs=mod_libs[platform][mod_name],
   465                 dynamic=dynamic,
   467                 dynamic=dynamic,
   466                 mod_code=mod_code_,
   468                 mod_code=mod_code_,
   467             )
   469             ), file=mod_cache_html_output)
   468 
   470 
   469             if dynamic:
   471             if dynamic:
   470                 mod_cache_html_output.close()
   472                 mod_cache_html_output.close()
   471             else:
   473             else:
   472                 mod_cache_html_output.seek(0)
   474                 mod_cache_html_output.seek(0)
   516         out_path = join(output, file_name)
   518         out_path = join(output, file_name)
   517         out_file = open(out_path, 'w')
   519         out_file = open(out_path, 'w')
   518         out_file.write(file_contents)
   520         out_file.write(file_contents)
   519         out_file.close()
   521         out_file.close()
   520         app_files.append((platform.lower(), file_name))
   522         app_files.append((platform.lower(), file_name))
   521         print "Created app file %s:%s: %s" % (
   523         print("Created app file %s:%s: %s" % (
   522             app_name, platform, out_path)
   524             app_name, platform, out_path))
   523 
   525 
   524     return app_files
   526     return app_files
   525 
   527 
   526 
   528 
   527 def flattenlist(ll):
   529 def flattenlist(ll):
   617 # third will be those modules that have the first and second...
   619 # third will be those modules that have the first and second...
   618 # etc.
   620 # etc.
   619 
   621 
   620 
   622 
   621 def make_deps(app_name, deps, mod_list):
   623 def make_deps(app_name, deps, mod_list):
   622     print "Calculating Dependencies ..."
   624     print("Calculating Dependencies ...")
   623     mod_list = filter_mods(app_name, mod_list)
   625     mod_list = filter_mods(app_name, mod_list)
   624     deps = filter_deps(app_name, deps)
   626     deps = filter_deps(app_name, deps)
   625 
   627 
   626     if not mod_list:
   628     if not mod_list:
   627         return []
   629         return []