Ejemplo n.º 1
0
def generateAppFiles(data_dir, js_includes, app_name, debug, output, dynamic,
                     cache_buster, optimize):

    all_cache_html_template = read_boilerplate(data_dir, "all.cache.html")
    mod_cache_html_template = read_boilerplate(data_dir, "mod.cache.html")

    # clean out the old ones first
    for name in os.listdir(output):
        if CACHE_HTML_PAT.match(name):
            p = join(output, name)
            print "Deleting existing app file %s" % p
            os.unlink(p)

    app_files = []
    tmpl = read_boilerplate(data_dir, "all.cache.html")
    parser = pyjs.PlatformParser("platform")
    app_headers = ''
    scripts = [
        '<script type="text/javascript" src="%s"></script>' % script
        for script in js_includes
    ]
    app_body = '\n'.join(scripts)

    mod_code = {}
    mod_libs = {}
    modules = {}
    app_libs = {}
    early_app_libs = {}
    app_code = {}
    overrides = {}
    pover = {}
    app_modnames = {}
    mod_levels = {}

    # First, generate all the code.
    # Second, (dynamic only), post-analyse the places where modules
    # haven't changed
    # Third, write everything out.

    for platform in app_platforms:

        mod_code[platform] = {}
        mod_libs[platform] = {}
        modules[platform] = []
        pover[platform] = {}
        app_libs[platform] = ''
        early_app_libs[platform] = ''
        app_code[platform] = {}
        app_modnames[platform] = {}

        # Application.Platform.cache.html

        parser.setPlatform(platform)
        app_translator = pyjs.AppTranslator(parser=parser,
                                            dynamic=dynamic,
                                            optimize=optimize)
        early_app_libs[platform], appcode = \
            app_translator.translate(None, is_app=False,
                                     debug=debug,
                                     library_modules=['dynamicajax.js',
                                                      '_pyjs.js', 'sys',
                                                      'pyjslib'])
        pover[platform].update(app_translator.overrides.items())
        for mname, name in app_translator.overrides.items():
            pd = overrides.setdefault(mname, {})
            pd[platform] = name

        print appcode
        # mod_code[platform][app_name] = appcode

        # platform.Module.cache.js

        modules_done = ['pyjslib', 'sys', '_pyjs.js']
        # modules_to_do = [app_name] + app_translator.library_modules
        modules_to_do = [app_name] + app_translator.library_modules

        dependencies = {}

        deps = map(pyjs.strip_py, modules_to_do)
        for d in deps:
            sublist = add_subdeps(dependencies, d)
            modules_to_do += sublist
        deps = uniquify(deps)
        # dependencies[app_name] = deps

        modules[platform] = modules_done + modules_to_do

        while modules_to_do:

            # print "modules to do", modules_to_do

            mn = modules_to_do.pop()
            mod_name = pyjs.strip_py(mn)

            if mod_name in modules_done:
                continue

            modules_done.append(mod_name)

            mod_cache_name = "%s.%s.cache.js" % (platform.lower(), mod_name)

            parser.setPlatform(platform)
            mod_translator = pyjs.AppTranslator(parser=parser,
                                                optimize=optimize)
            mod_libs[platform][mod_name], mod_code[platform][mod_name] = \
                mod_translator.translate(mod_name,
                                         is_app=False,
                                         debug=debug)
            pover[platform].update(mod_translator.overrides.items())
            for mname, name in mod_translator.overrides.items():
                pd = overrides.setdefault(mname, {})
                pd[platform] = name

            mods = mod_translator.library_modules
            modules_to_do += mods
            modules[platform] += mods

            deps = map(pyjs.strip_py, mods)
            sd = subdeps(mod_name)
            if len(sd) > 1:
                deps += sd[:-1]
            while mod_name in deps:
                deps.remove(mod_name)

            # print
            # print
            # print "modname preadd:", mod_name, deps
            # print
            # print
            for d in deps:
                sublist = add_subdeps(dependencies, d)
                modules_to_do += sublist
            modules_to_do += add_subdeps(dependencies, mod_name)
            # print "modname:", mod_name, deps
            deps = uniquify(deps)
            # print "modname:", mod_name, deps
            dependencies[mod_name] = deps

        # work out the dependency ordering of the modules

        mod_levels[platform] = make_deps(None, dependencies, modules_done)

    # now write everything out

    for platform in app_platforms:

        early_app_libs_ = early_app_libs[platform]
        app_libs_ = app_libs[platform]
        app_code_ = app_code[platform]
        # modules_ = filter_mods(app_name, modules[platform])
        mods = flattenlist(mod_levels[platform])
        mods.reverse()
        modules_ = filter_mods(None, mods)

        for mod_name in modules_:

            mod_code_ = mod_code[platform][mod_name]

            mod_name = pyjs.strip_py(mod_name)

            override_name = "%s.%s" % (platform.lower(), mod_name)
            if override_name in pover[platform]:
                mod_cache_name = "%s.cache.js" % (override_name)
            else:
                mod_cache_name = "%s.cache.js" % (mod_name)

            print "Creating: " + mod_cache_name

            modlevels = make_deps(None, dependencies, dependencies[mod_name])

            modnames = []

            for md in modlevels:
                mnames = map(lambda x: "'%s'" % x, md)
                mnames = "new pyjslib.List([\n\t\t\t%s])" % ',\n\t\t\t'.join(
                    mnames)
                modnames.append(mnames)

            modnames.reverse()
            modnames = "new pyjslib.List([\n\t\t%s\n\t])" % ',\n\t\t'.join(
                modnames)

            # convert the overrides

            overnames = map(lambda x: "'%s': '%s'" % x,
                            pover[platform].items())
            overnames = "new pyjslib.Dict({\n\t\t%s\n\t})" % ',\n\t\t'.join(
                overnames)

            if dynamic:
                mod_cache_html_output = open(join(output, mod_cache_name), "w")
            else:
                mod_cache_html_output = StringIO()

            print >> mod_cache_html_output, mod_cache_html_template % dict(
                mod_name=mod_name,
                app_name=app_name,
                modnames=modnames,
                overrides=overnames,
                mod_libs=mod_libs[platform][mod_name],
                dynamic=dynamic,
                mod_code=mod_code_,
            )

            if dynamic:
                mod_cache_html_output.close()
            else:
                mod_cache_html_output.seek(0)
                app_libs_ += mod_cache_html_output.read()

        # write out the dependency ordering of the modules

        app_modnames = []

        for md in mod_levels[platform]:
            mnames = map(lambda x: "'%s'" % x, md)
            mnames = "new pyjslib.List([\n\t\t\t%s])" % ',\n\t\t\t'.join(
                mnames)
            app_modnames.append(mnames)

        app_modnames.reverse()
        app_modnames = "new pyjslib.List([\n\t\t%s\n\t])" % ',\n\t\t'.join(
            app_modnames)

        # convert the overrides

        overnames = map(lambda x: "'%s': '%s'" % x, pover[platform].items())
        overnames = "new pyjslib.Dict({\n\t\t%s\n\t})" % ',\n\t\t'.join(
            overnames)

        # print "platform names", platform, overnames
        # print pover

        # now write app.allcache including dependency-ordered list of
        # library modules

        file_contents = all_cache_html_template % dict(
            app_name=app_name,
            early_app_libs=early_app_libs_,
            app_libs=app_libs_,
            app_code=app_code_,
            app_body=app_body,
            overrides=overnames,
            platform=platform.lower(),
            dynamic=dynamic,
            app_modnames=app_modnames,
            app_headers=app_headers)
        if cache_buster:
            digest = md5.new(file_contents).hexdigest()
            file_name = "%s.%s.%s" % (platform.lower(), app_name, digest)
        else:
            file_name = "%s.%s" % (platform.lower(), app_name)
        file_name += ".cache.html"
        out_path = join(output, file_name)
        out_file = open(out_path, 'w')
        out_file.write(file_contents)
        out_file.close()
        app_files.append((platform.lower(), file_name))
        print "Created app file %s:%s: %s" % (app_name, platform, out_path)

    return app_files
Ejemplo n.º 2
0
def main():

    global file_name

    parser = OptionParser(usage = usage)
    pyjs.add_compile_options(parser)
    parser.add_option("-o", "--output",
        dest="output",
        help="File to which the generated javascript should be written")

    parser.add_option("-i", "--input",
        dest="input",
        help="File from which the generated javascript should be read")

    parser.set_defaults(\
        output = None,
        input = None,
    )
    (options, args) = parser.parse_args()

    file_name = args[0]
    if len(args) > 1:
        module_name = args[1]
    else:
        module_name = None

    debug = 0

    if options.input:
        txt = open(options.input, 'r').read()
    else:
        parser = pyjs.PlatformParser("platform", verbose=False)
        parser.setPlatform("pysm")

        if file_name.endswith(".py"):
            file_name = file_name[:-3]

        app_translator = pyjs.AppTranslator(
                app_library_dirs, parser,
                verbose = False,
                debug = options.debug,
                print_statements = options.print_statements,
                function_argument_checking = options.function_argument_checking,
                attribute_checking = options.attribute_checking,
                source_tracking = options.source_tracking,
                line_tracking = options.line_tracking,
                store_source = options.store_source,
        )
        app_libs, txt = app_translator.translate(file_name, debug=debug,
                                      library_modules=['_pyjs.js', 'sys', 'pyjslib'])

        template = """
var $pyjs = new Object();
$pyjs.modules = {};
$pyjs.modules_hash = {};
$pyjs.options = new Object();
$pyjs.options.set_all = function (v) {
    $pyjs.options.arg_ignore = v;
    $pyjs.options.arg_count = v;
    $pyjs.options.arg_is_instance = v;
    $pyjs.options.arg_instance_type = v;
    $pyjs.options.arg_kwarg_dup = v;
    $pyjs.options.arg_kwarg_unexpected_keyword = v;
    $pyjs.options.arg_kwarg_multiple_values = v;
}
$pyjs.options.set_all(true);
$pyjs.trackstack = [];
$pyjs.track = {module:'__main__', lineno: 1};
$pyjs.trackstack.push($pyjs.track);
%(app_libs)s


%(module)s

"""

        txt = template % {'app_libs': app_libs, 'module_name': file_name,
                          'module': txt}

        txt += "sys();\n" 
        txt += "pyjslib();\n" 
        txt += "%s();\n" % file_name

    if options.output:
        fp = open(options.output, 'w')
        fp.write(txt)
        fp.close()

    rt = spidermonkey.Runtime()
    global cx
    cx = rt.new_context()
    cx.add_global("pysm_print_fn", pysm_print_fn)
    cx.add_global("pysm_import_module", pysm_import_module)

    cx.execute(txt)
Ejemplo n.º 3
0
    print >> home_nocache_html_output, home_nocache_html_template % dict(
        app_name=app_name,
        safari_js="Safari",
        ie6_js="IE6",
        oldmoz_js="OldMoz",
        moz_js="Mozilla",
        opera_js="Opera",
    )

    home_nocache_html_output.close()

    ## all.cache.html

    all_cache_html_template = read_boilerplate("all.cache.html")

    parser = pyjs.PlatformParser("platform")

    for platform in app_platforms:
        all_cache_name = platform + ".cache.html"
        print "Creating: " + all_cache_name

        parser.setPlatform(platform)
        app_translator = pyjs.AppTranslator(app_library_dirs, parser)
        app_libs = app_translator.translateLibraries(['pyjslib'])
        app_code = app_translator.translate(app_name)

        all_cache_html_output = open(join(output, all_cache_name), "w")

        print >> all_cache_html_output, all_cache_html_template % dict(
            app_name=app_name,
            app_libs=app_libs,