Esempio n. 1
0
def main():
    (options, args, parser) = simple_options(_parser, __version__,
                                             __dependencies__)

    Profiler.start('main')
    Profiler.start('startup')

    # Sanity checks

    if 0 == len(args):
        LOG.error("no input files specified")
        parser.print_help()
        exit(1)

    if options.mode not in [
            'plugin', 'canvas', 'webworker', 'webworker-debug'
    ]:
        LOG.error("invalid mode %s", options.mode)
        parser.print_help()
        exit(1)

    if options.output is None:
        LOG.error("no output file specified (required in dependency mode)")
        parser.print_help()
        exit(1)

    # Create a jinja2 env

    env = env_create(options)
    input_js = args

    LOG.info("input files: %s", input_js)

    Profiler.stop('startup')
    Profiler.start('run')

    # Execute

    retval = 1
    try:

        if options.dependency:
            LOG.info("dependency generation selected")
            retval = tzjs_dump_dependencies(env, options, input_js)
        else:
            LOG.info("rendering tzjs")
            retval = tzjs_generate(env, options, input_js)

    except ToolsException, e:
        LOG.error(str(e))
        exit(1)
Esempio n. 2
0
def main():
    (options, args, parser) = simple_options(_parser, __version__,
                                             __dependencies__)

    Profiler.start('main')
    Profiler.start('startup')

    # Sanity checks

    if 0 == len(args):
        LOG.error("no input files specified")
        parser.print_help()
        exit(1)

    if options.mode not in [ 'plugin', 'canvas' ]:
        LOG.error("invalid mode %s" % options.mode)
        parser.print_help()
        exit(1)

    if options.output is None:
        LOG.error("no output file specified (required in dependency mode)")
        parser.print_help()
        exit(1)

    # Create a jinja2 env

    env = env_create(options)
    input_js = args

    LOG.info("input files: %s" % input_js)

    Profiler.stop('startup')
    Profiler.start('run')

    # Execute

    retval = 1
    try:

        if options.dependency:
            LOG.info("dependency generation selected")
            retval = tzjs_dump_dependencies(env, options, input_js)
        else:
            LOG.info("rendering tzjs")
            retval = tzjs_generate(env, options, input_js)

    except ToolsException, e:
        LOG.error(str(e))
        exit(1)
Esempio n. 3
0
 def _enable_profiler(_options, _opt_str, _value, _parser):
     Profiler.enable()
Esempio n. 4
0
def main():

    (options, args, parser) = simple_options(_parser, __version__,
                                             __dependencies__, input_required=False)

    Profiler.start('main')
    Profiler.start('startup')

    input_files = args

    # Check that if dump-default-template is set then output and exit

    if options.dump_default_template:
        exit(dump_default_template(options.output))
    elif 0 == len(args):
        LOG.error('No input files specified')
        parser.print_help()
        exit(1)

    LOG.info("options: %s" % options)
    LOG.info("args: %s" % args)
    LOG.info("parser: %s" % parser)
    LOG.info("templatedirs: %s" % options.templatedirs)

    if options.output is None:
        LOG.error("no output file specified (required in dependency mode)")
        parser.print_help()
        exit(1)

    # Check mode

    if options.mode not in [ 'plugin-debug', 'plugin', 'canvas-debug', 'canvas' ]:
        LOG.error('Unrecognised mode: %s' % options.mode)
        parser.print_help()
        exit(1)

    # Check a release source name is given if mode is one of release
    # or canvas

    if options.mode in [ 'plugin', 'canvas' ] and \
            not options.dependency and \
            not options.codefile:
        LOG.error('Missing code file name.  Use --code to specify.')
        parser.print_usage()
        exit(1)

    # Check input files and split them into (ordered) js and html

    (input_js, input_html) = check_input(input_files)

    LOG.info("js files: %s" % input_js)
    LOG.info("html files: %s" % input_html)

    # In debug and canvas-debug we need a .js input file

    if 0 == len(input_js):
        if options.mode in [ 'debug', 'canvas-debug' ]:
            LOG.error('Missing input .js file')
            parser.print_usage()
            exit(1)
    if 1 < len(input_html):
        LOG.error('Multiple html files specified: %s' % input_html)
        exit(1)

    # Create a jinja2 env

    env = env_create(options, DEFAULT_HTML_TEMPLATE)

    Profiler.stop('startup')
    Profiler.start('run')

    # Execute

    retval = 1
    try:

        if options.dependency:
            LOG.info("generating dependencies")
            retval = html_dump_dependencies(env, options, input_js, input_html)
            LOG.info("done generating dependencies")

        else:
            retval = html_generate(env, options, input_js, input_html)

    except ToolsException, e:
        #traceback.print_exc()
        LOG.error("%s" % str(e))
Esempio n. 5
0
def html_generate(env, options, input_js, input_html):
    """
    Generate html based on the templates and build mode.
    """

    # - dev, canvas_dev:
    #     render top-level js files into a temporary file
    #     collect the .js files that need to be included
    #     setup includes, startup code and the js render result into variables
    #     render html template
    #
    # - release, canvas:
    #     need to know name of output js file
    #     setup startup code to point to .tzjs or .js file
    #     render html template

    # Load templates (using default html template if not specified)

    Profiler.start('load_templates')

    template_html = load_html_template(env, input_html)
    if template_html is None:
        LOG.error("failed to load file %s from template dirs" % input_html[0])
        exit(1)

    # Get context

    if len(input_js) > 0:
        title = input_js[0]
    elif options.codefile:
        title = options.codefile
    elif len(input_html) > 0:
        title = input_html[0]
    else:
        title = "Unknown"
    title = splitext(basename(title))[0]

    context = context_from_options(options, title)

    Profiler.stop('load_templates')
    Profiler.start('code_gen')

    # In development modes, render the JS code that needs embedding

    rendered_js = ""
    inc_js = []

    if options.mode in [ 'plugin-debug', 'canvas-debug' ]:
        inject_js = inject_js_from_options(options)

        Profiler.start('load_js_templates')
        templates_js = env_load_templates(env, input_js)
        Profiler.stop('load_js_templates')

        (rendered_js, inc_js) = render_js(context, options, templates_js,
                                          inject_js)

    # Add the HTML and JS code into the tz_* variables

    default_add_code(options, context, rendered_js, inc_js)

    Profiler.stop('code_gen')
    Profiler.start('html_render')

    # Render the template and write it out

    try:
        res = template_html.render(context)
    except Exception, e:
        raise ToolsException("Error in '%s': %s %s" \
                                 % (input_html, e.__class__.__name__, str(e)))
Esempio n. 6
0
    # Render the template and write it out

    try:
        res = template_html.render(context)
    except Exception, e:
        raise ToolsException("Error in '%s': %s %s" \
                                 % (input_html, e.__class__.__name__, str(e)))

    try:
        with open(options.output, "wb") as f:
            f.write(res)
    except IOError:
        raise ToolsException("failed to create file: %s" % options.output)

    Profiler.stop('html_render')

    return 0

############################################################

def main():

    (options, args, parser) = simple_options(_parser, __version__,
                                             __dependencies__, input_required=False)

    Profiler.start('main')
    Profiler.start('startup')

    input_files = args
Esempio n. 7
0
def tzjs_generate(env, options, input_js):

    # The set of files to be injected

    Profiler.start('find_inject_code')
    inject_js = inject_js_from_options(options)
    Profiler.stop('find_inject_code')

    if 0 < len(inject_js):
        LOG.info("Files to inject:")
        for i in inject_js:
            LOG.info(" - '%s'" % i)

    # Create a context and render the template

    Profiler.start('load_templates')
    context = context_from_options(options, input_js[0])
    templates_js = env_load_templates(env, input_js)
    Profiler.stop('load_templates')

    Profiler.start('render_js')
    (rendered_js, inc_js) = render_js(context, options, templates_js,
                                      inject_js)
    Profiler.stop('render_js')

    if 0 != len(inc_js):
        raise ToolsException("internal error")

    # If required, remove all calls to 'debug.*' methods BEFORE
    # compacting

    if options.stripdebug:

        strip_path = "strip-debug"
        if options.stripdebugpath:
            strip_path = normpath(abspath(options.stripdebugpath))

        LOG.info("Stripping debug method calls ...")

        # Check we can actually run strip debug, with the given path
        p = subprocess.Popen('%s -h' % strip_path, stdout=subprocess.PIPE,
                                                   stderr=subprocess.STDOUT,
                                                   shell=True)
        p.communicate()
        if p.returncode != 0:
            raise ToolsException( \
                "\n\tstrip-debug tool could not be found, check it's on your path\n"
                "\tor supply the path with --strip-debug <path>. To run maketzjs\n"
                "\twithout stripping debug code run with --no-strip-debug." )

        Profiler.start('strip_debug')

        strip_debug_flags = "-Ddebug=false"

        # Add the default flags first, in case the custom flags
        # override them.

        if options.verbose:
            strip_debug_flags += " -v"
        for s in options.stripnamespaces:
            strip_debug_flags += " --namespace %s" % s
        for v in options.stripvars:
            strip_debug_flags += " -D %s" % v
        if options.ignoreerrors:
            strip_debug_flags += " --ignore-errors"

        # Launch the strip command and pass in the full script via
        # streams.

        strip_cmd = "%s %s" % (strip_path, strip_debug_flags)
        LOG.info("Strip cmd: %s" % strip_cmd)
        p = subprocess.Popen(strip_cmd, shell=True,
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        (stripped_js, err) = p.communicate(rendered_js)
        strip_retval = p.wait()

        if 0 != strip_retval:
            with NamedTemporaryFile(delete = False) as t:
                t.write(rendered_js)

            raise ToolsException( \
                "strip-debug tool exited with code %d and stderr:\n\n%s\n"
                "The (merged) input probably contains a syntax error.  It has "
                "been written to:\n  %s\nfor inspection." \
                    % (strip_retval, err, t.name))

        if not err is None and len(err) > 0:
            print "error output from strip-debug tool:"
            print "%s" % err


        rendered_js = stripped_js

        Profiler.stop('strip_debug')

    # If required, compact the JS via a temporary file, otherwise just
    # write out directly to the output file.

    if options.yui or options.closure or options.uglifyjs:

        Profiler.start('compact')

        with NamedTemporaryFile(delete = False) as t:
            LOG.info("Writing temp JS to '%s'" % t.name)
            t.write(rendered_js)

        LOG.info("Compacting temp JS to '%s'" % options.output)
        tzjs_compact(options, t.name, options.output)
        remove(t.name)
        Profiler.stop('compact')

    else:

        LOG.info("Writing JS to '%s'" % options.output)
        Profiler.start('write_out')
        try:
            with open(options.output, 'wb') as f:
                f.write(rendered_js)
                LOG.info("Succeeded")
        except IOError:
            raise ToolsException("failed to write file: %s" % options.output)
        Profiler.stop('write_out')

    return 0
Esempio n. 8
0
    LOG.info("input files: %s" % input_js)

    Profiler.stop('startup')
    Profiler.start('run')

    # Execute

    retval = 1
    try:

        if options.dependency:
            LOG.info("dependency generation selected")
            retval = tzjs_dump_dependencies(env, options, input_js)
        else:
            LOG.info("rendering tzjs")
            retval = tzjs_generate(env, options, input_js)

    except ToolsException, e:
        LOG.error(str(e))
        exit(1)

    Profiler.stop('run')
    Profiler.stop('main')
    Profiler.dump_data()

    return retval

if __name__ == "__main__":
    exit(main())
Esempio n. 9
0
def tzjs_generate(env, options, input_js):

    # The set of files to be injected

    Profiler.start('find_inject_code')
    inject_js = inject_js_from_options(options)
    Profiler.stop('find_inject_code')

    if 0 < len(inject_js):
        LOG.info("Files to inject:")
        for i in inject_js:
            LOG.info(" - '%s'", i)

    # Create a context and render the template

    Profiler.start('load_templates')
    context = context_from_options(options, input_js[0])
    templates_js = env_load_templates(env, input_js)
    Profiler.stop('load_templates')

    Profiler.start('render_js')
    (rendered_js, inc_js) = render_js(context, options, templates_js,
                                      inject_js)
    rendered_js = rendered_js.encode('utf-8')
    Profiler.stop('render_js')

    if 0 != len(inc_js):
        raise ToolsException("internal error")

    # If required, remove all calls to 'debug.*' methods BEFORE
    # compacting

    # TODO: We write and read the files too many times.  Better to
    # write once to a temporary, keep track of the name and invoke
    # each external command on files, creating subsequent temporaries
    # as required.

    if options.stripdebug:

        strip_path = "strip-debug"
        if options.stripdebugpath:
            strip_path = normpath(abspath(options.stripdebugpath))

        LOG.info("Stripping debug method calls ...")

        # Check we can actually run strip debug, with the given path
        p = subprocess.Popen('%s -h' % strip_path,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             shell=True)
        p.communicate()
        if p.returncode != 0:
            raise ToolsException( \
                "\n\tstrip-debug tool could not be found, check it's on your path\n"
                "\tor supply the path with --strip-debug <path>. To run maketzjs\n"
                "\twithout stripping debug code run with --no-strip-debug." )

        Profiler.start('strip_debug')

        strip_debug_flags = "-Ddebug=false"

        # Add the default flags first, in case the custom flags
        # override them.

        if options.verbose:
            strip_debug_flags += " -v"
        for s in options.stripnamespaces:
            strip_debug_flags += " --namespace %s" % s
        for v in options.stripvars:
            strip_debug_flags += " -D %s" % v
        if options.ignoreerrors:
            strip_debug_flags += " --ignore-errors"

        # Launch the strip command and pass in the full script via
        # streams.

        with NamedTemporaryFile(delete = False) as t:
            LOG.info("Writing temp JS to '%s'", t.name)
            t.write(rendered_js)

        with NamedTemporaryFile(delete = False) as tmp_out:
            pass

        strip_cmd = "%s %s -o %s %s" % (strip_path, strip_debug_flags,
                                        tmp_out.name, t.name)
        LOG.info("Strip cmd: %s", strip_cmd)
        strip_retval = subprocess.call(strip_cmd, shell=True)

        if 0 != strip_retval:
            raise ToolsException( \
                "strip-debug tool exited with code %d\n"
                "The (merged) input probably contains a syntax error:\n"
                "  %s" % (strip_retval, t.name))

        rendered_js = read_file_utf8(tmp_out.name).encode('utf-8')
        remove(tmp_out.name)
        remove(t.name)

        Profiler.stop('strip_debug')

    # If required, compact the JS via a temporary file, otherwise just
    # write out directly to the output file.

    if options.mode != 'webworker-debug' and (options.yui or options.closure or options.uglifyjs):

        Profiler.start('compact')

        with NamedTemporaryFile(delete = False) as t:
            LOG.info("Writing temp JS to '%s'", t.name)
            t.write(rendered_js)

        LOG.info("Compacting temp JS to '%s'", options.output)
        tzjs_compact(options, t.name, options.output)
        remove(t.name)
        Profiler.stop('compact')

    else:

        LOG.info("Writing JS to '%s'", options.output)
        Profiler.start('write_out')
        try:
            with open(options.output, 'wb') as f:
                f.write(rendered_js)
                LOG.info("Succeeded")
        except IOError:
            raise ToolsException("failed to write file: %s" % options.output)
        Profiler.stop('write_out')

    return 0
Esempio n. 10
0
def main():

    (options, args, parser) = simple_options(_parser,
                                             __version__,
                                             __dependencies__,
                                             input_required=False)

    Profiler.start('main')
    Profiler.start('startup')

    input_files = args

    # Check that if dump-default-template is set then output and exit

    if options.dump_default_template:
        exit(dump_default_template(options.output))
    elif 0 == len(args):
        LOG.error('No input files specified')
        parser.print_help()
        exit(1)

    LOG.info("options: %s", options)
    LOG.info("args: %s", args)
    LOG.info("parser: %s", parser)
    LOG.info("templatedirs: %s", options.templatedirs)

    if options.output is None:
        LOG.error("no output file specified (required in dependency mode)")
        parser.print_help()
        exit(1)

    # Check mode

    if options.mode not in [
            'plugin-debug', 'plugin', 'canvas-debug', 'canvas'
    ]:
        LOG.error('Unrecognised mode: %s', options.mode)
        parser.print_help()
        exit(1)

    # Check a release source name is given if mode is one of release
    # or canvas

    if options.mode in [ 'plugin', 'canvas' ] and \
            not options.dependency and \
            not options.codefile:
        LOG.error('Missing code file name.  Use --code to specify.')
        parser.print_usage()
        exit(1)

    # Check input files and split them into (ordered) js and html

    (input_js, input_html) = check_input(input_files)

    LOG.info("js files: %s", input_js)
    LOG.info("html files: %s", input_html)

    # In debug and canvas-debug we need a .js input file

    if 0 == len(input_js):
        if options.mode in ['debug', 'canvas-debug']:
            LOG.error('Missing input .js file')
            parser.print_usage()
            exit(1)
    if 1 < len(input_html):
        LOG.error('Multiple html files specified: %s', input_html)
        exit(1)

    # Create a jinja2 env

    env = env_create(options, DEFAULT_HTML_TEMPLATE)

    Profiler.stop('startup')
    Profiler.start('run')

    # Execute

    retval = 1
    try:

        if options.dependency:
            LOG.info("generating dependencies")
            retval = html_dump_dependencies(env, options, input_js, input_html)
            LOG.info("done generating dependencies")

        else:
            retval = html_generate(env, options, input_js, input_html)

    except ToolsException, e:
        #traceback.print_exc()
        LOG.error("%s", str(e))
Esempio n. 11
0
def html_generate(env, options, input_js, input_html):
    """
    Generate html based on the templates and build mode.
    """

    # - dev, canvas_dev:
    #     render top-level js files into a temporary file
    #     collect the .js files that need to be included
    #     setup includes, startup code and the js render result into variables
    #     render html template
    #
    # - release, canvas:
    #     need to know name of output js file
    #     setup startup code to point to .tzjs or .js file
    #     render html template

    # Load templates (using default html template if not specified)

    Profiler.start('load_templates')

    template_html = load_html_template(env, input_html)
    if template_html is None:
        LOG.error("failed to load file %s from template dirs", input_html[0])
        exit(1)

    # Get context

    if len(input_js) > 0:
        title = input_js[0]
    elif options.codefile:
        title = options.codefile
    elif len(input_html) > 0:
        title = input_html[0]
    else:
        title = "Unknown"
    title = splitext(basename(title))[0]

    context = context_from_options(options, title)

    Profiler.stop('load_templates')
    Profiler.start('code_gen')

    # In development modes, render the JS code that needs embedding

    rendered_js = ""
    inc_js = []

    if options.mode in ['plugin-debug', 'canvas-debug']:
        inject_js = inject_js_from_options(options)

        Profiler.start('load_js_templates')
        templates_js = env_load_templates(env, input_js)
        Profiler.stop('load_js_templates')

        (rendered_js, inc_js) = render_js(context, options, templates_js,
                                          inject_js)

    # Add the HTML and JS code into the tz_* variables

    default_add_code(options, context, rendered_js, inc_js)

    Profiler.stop('code_gen')
    Profiler.start('html_render')

    # Render the template and write it out

    try:
        res = template_html.render(context)
    except Exception, e:
        raise ToolsException("Error in '%s': %s %s" \
                                 % (input_html, e.__class__.__name__, str(e)))
Esempio n. 12
0
    # Render the template and write it out

    try:
        res = template_html.render(context)
    except Exception, e:
        raise ToolsException("Error in '%s': %s %s" \
                                 % (input_html, e.__class__.__name__, str(e)))

    try:
        with open(options.output, "wb") as f:
            f.write(res.encode('utf-8'))
    except IOError:
        raise ToolsException("failed to create file: %s" % options.output)

    Profiler.stop('html_render')

    return 0


############################################################


def main():

    (options, args, parser) = simple_options(_parser,
                                             __version__,
                                             __dependencies__,
                                             input_required=False)

    Profiler.start('main')
Esempio n. 13
0
 def _enable_profiler(_options, _opt_str, _value, _parser):
     Profiler.enable()
Esempio n. 14
0
def tzjs_generate(env, options, input_js):

    # The set of files to be injected

    Profiler.start('find_inject_code')
    inject_js = inject_js_from_options(options)
    Profiler.stop('find_inject_code')

    if 0 < len(inject_js):
        LOG.info("Files to inject:")
        for i in inject_js:
            LOG.info(" - '%s'", i)

    # Create a context and render the template

    Profiler.start('load_templates')
    context = context_from_options(options, input_js[0])
    templates_js = env_load_templates(env, input_js)
    Profiler.stop('load_templates')

    Profiler.start('render_js')
    (rendered_js, inc_js) = render_js(context, options, templates_js,
                                      inject_js)
    rendered_js = rendered_js.encode('utf-8')
    Profiler.stop('render_js')

    if 0 != len(inc_js):
        raise ToolsException("internal error")

    # If required, remove all calls to 'debug.*' methods BEFORE
    # compacting

    # TODO: We write and read the files too many times.  Better to
    # write once to a temporary, keep track of the name and invoke
    # each external command on files, creating subsequent temporaries
    # as required.

    if options.stripdebug:

        strip_path = "strip-debug"
        if options.stripdebugpath:
            strip_path = normpath(abspath(options.stripdebugpath))

        LOG.info("Stripping debug method calls ...")

        # Check we can actually run strip debug, with the given path
        p = subprocess.Popen('%s -h' % strip_path,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             shell=True)
        p.communicate()
        if p.returncode != 0:
            raise ToolsException( \
                "\n\tstrip-debug tool could not be found, check it's on your path\n"
                "\tor supply the path with --strip-debug <path>. To run maketzjs\n"
                "\twithout stripping debug code run with --no-strip-debug." )

        Profiler.start('strip_debug')

        strip_debug_flags = "-Ddebug=false"

        # Add the default flags first, in case the custom flags
        # override them.

        if options.verbose:
            strip_debug_flags += " -v"
        for s in options.stripnamespaces:
            strip_debug_flags += " --namespace %s" % s
        for v in options.stripvars:
            strip_debug_flags += " -D %s" % v
        if options.ignoreerrors:
            strip_debug_flags += " --ignore-errors"

        # Launch the strip command and pass in the full script via
        # streams.

        with NamedTemporaryFile(delete=False) as t:
            LOG.info("Writing temp JS to '%s'", t.name)
            t.write(rendered_js)

        with NamedTemporaryFile(delete=False) as tmp_out:
            pass

        strip_cmd = "%s %s -o %s %s" % (strip_path, strip_debug_flags,
                                        tmp_out.name, t.name)
        LOG.info("Strip cmd: %s", strip_cmd)
        strip_retval = subprocess.call(strip_cmd, shell=True)

        if 0 != strip_retval:
            raise ToolsException( \
                "strip-debug tool exited with code %d\n"
                "The (merged) input probably contains a syntax error:\n"
                "  %s" % (strip_retval, t.name))

        rendered_js = read_file_utf8(tmp_out.name).encode('utf-8')
        remove(tmp_out.name)
        remove(t.name)

        Profiler.stop('strip_debug')

    # If required, compact the JS via a temporary file, otherwise just
    # write out directly to the output file.

    if options.mode != 'webworker-debug' and (options.yui or options.closure
                                              or options.uglifyjs):

        Profiler.start('compact')

        with NamedTemporaryFile(delete=False) as t:
            LOG.info("Writing temp JS to '%s'", t.name)
            t.write(rendered_js)

        LOG.info("Compacting temp JS to '%s'", options.output)
        tzjs_compact(options, t.name, options.output)
        remove(t.name)
        Profiler.stop('compact')

    else:

        LOG.info("Writing JS to '%s'", options.output)
        Profiler.start('write_out')
        try:
            with open(options.output, 'wb') as f:
                f.write(rendered_js)
                LOG.info("Succeeded")
        except IOError:
            raise ToolsException("failed to write file: %s" % options.output)
        Profiler.stop('write_out')

    return 0