Esempio n. 1
0
 def get_content(self, txtpath, encoding):
     if txtpath.basename == "commandline.rst":
         result = []
         for line in txtpath.read().splitlines():
             if line.startswith('.. GENERATE:'):
                 start = line[len('.. GENERATE:'):].strip()
                 descr = start_to_descr[start]
                 line = make_cmdline_overview(descr, title=False).text()
             result.append(line)
         return "\n".join(result)
     fullpath = txtpath.purebasename
     start = fullpath.split(".")[0]
     path = fullpath.rsplit(".", 1)[0]
     basedescr = start_to_descr.get(start)
     if basedescr is None:
         return txtpath.read()
     if fullpath.count(".") == 0:
         descr = basedescr
         path = ""
     else:
         conf = Config(basedescr)
         subconf, step = conf._cfgimpl_get_home_by_path(
                 fullpath.split(".", 1)[1])
         descr = getattr(subconf._cfgimpl_descr, step)
     text = unicode(descr.make_rest_doc(path).text())
     if txtpath.check(file=True):
         content = txtpath.read()
         if content:
             text += "\nDescription\n==========="
             return u"%s\n\n%s" % (text, unicode(txtpath.read(), encoding))
     return text
Esempio n. 2
0
def make_cmdline_overview(descr, title=True):
    content = Rest()
    if title:
        content.add(Title("Overview of Command Line Options for '%s'" % (descr._name,), abovechar="=", belowchar="="))
    cmdlines = []
    config = Config(descr)
    for path in config.getpaths(include_groups=False):
        subconf, step = config._cfgimpl_get_home_by_path(path)
        fullpath = descr._name + "." + path
        prefix = fullpath.rsplit(".", 1)[0]
        subdescr = getattr(subconf._cfgimpl_descr, step)
        cmdline = get_cmdline(subdescr.cmdline, fullpath)
        if cmdline is not None:
            header = _get_section_header(cmdline, fullpath, subdescr)
            cmdlines.append((header, cmdline, fullpath, subdescr))
    cmdlines.sort(key=lambda x: (x[0], x[1].strip("-")))
    currheader = ""
    curr = content
    for header, cmdline, fullpath, subdescr in cmdlines:
        if header != currheader:
            content.add(Title(header, abovechar="", belowchar="="))
            curr = content.add(Paragraph())
            currheader = header
        curr.add(ListItem(Link(cmdline + ":", fullpath + ".html"), Text(subdescr.doc)))
    return content
Esempio n. 3
0
 def make_rest_doc(self, path=""):
     fullpath = get_fullpath(self, path)
     content = Rest(Title(fullpath, abovechar="=", belowchar="="),
                    Directive("contents"))
     if path:
         content.add(Paragraph(Link("back to parent", path + ".html")))
     content.join(Title("Basic Option Information"),
                  ListItem(Strong("name:"), self._name),
                  ListItem(Strong("description:"), self.doc),
                  Title("Sub-Options"))
     stack = []
     prefix = fullpath
     curr = content
     config = Config(self)
     for ending in self.getpaths(include_groups=True):
         subpath = fullpath + "." + ending
         while not (subpath.startswith(prefix)
                    and subpath[len(prefix)] == "."):
             curr, prefix = stack.pop()
         print subpath, fullpath, ending, curr
         sub, step = config._cfgimpl_get_home_by_path(ending)
         doc = getattr(sub._cfgimpl_descr, step).doc
         if doc:
             new = curr.add(
                 ListItem(Link(subpath + ":", subpath + ".html"), Em(doc)))
         else:
             new = curr.add(ListItem(Link(subpath + ":",
                                          subpath + ".html")))
         stack.append((curr, prefix))
         prefix = subpath
         curr = new
     return content
Esempio n. 4
0
def get_combined_translation_config(other_optdescr=None, existing_config=None, overrides=None, translating=False):
    if overrides is None:
        overrides = {}
    d = BoolOption("translating", "indicates whether we are translating currently", default=False, cmdline=None)
    if other_optdescr is None:
        children = []
        newname = ""
    else:
        children = [other_optdescr]
        newname = other_optdescr._name
    if existing_config is None:
        children += [d, translation_optiondescription]
    else:
        children += [child for child in existing_config._cfgimpl_descr._children if child._name != newname]
    descr = OptionDescription("pypy", "all options", children)
    config = Config(descr, **overrides)
    if translating:
        config.translating = True
    if existing_config is not None:
        for child in existing_config._cfgimpl_descr._children:
            if child._name == newname:
                continue
            value = getattr(existing_config, child._name)
            config._cfgimpl_values[child._name] = value
    return config
Esempio n. 5
0
def make_cmdline_overview(descr, title=True):
    content = Rest()
    if title:
        content.add(
            Title("Overview of Command Line Options for '%s'" %
                  (descr._name, ),
                  abovechar="=",
                  belowchar="="))
    cmdlines = []
    config = Config(descr)
    for path in config.getpaths(include_groups=False):
        subconf, step = config._cfgimpl_get_home_by_path(path)
        fullpath = (descr._name + "." + path)
        prefix = fullpath.rsplit(".", 1)[0]
        subdescr = getattr(subconf._cfgimpl_descr, step)
        cmdline = get_cmdline(subdescr.cmdline, fullpath)
        if cmdline is not None:
            header = _get_section_header(cmdline, fullpath, subdescr)
            cmdlines.append((header, cmdline, fullpath, subdescr))
    cmdlines.sort(key=lambda x: (x[0], x[1].strip("-")))
    currheader = ""
    curr = content
    for header, cmdline, fullpath, subdescr in cmdlines:
        if header != currheader:
            content.add(Title(header, abovechar="", belowchar="="))
            curr = content.add(Paragraph())
            currheader = header
        curr.add(
            ListItem(Link(cmdline + ":", fullpath + ".html"),
                     Text(subdescr.doc)))
    return content
Esempio n. 6
0
 def make_rest_doc(self, path=""):
     fullpath = get_fullpath(self, path)
     content = Rest(
         Title(fullpath, abovechar="=", belowchar="="),
         Directive("contents"))
     if path:
         content.add(
             Paragraph(Link("back to parent", path + ".html")))
     content.join(
         Title("Basic Option Information"),
         ListItem(Strong("name:"), self._name),
         ListItem(Strong("description:"), self.doc),
         Title("Sub-Options"))
     stack = []
     prefix = fullpath
     curr = content
     config = Config(self)
     for ending in self.getpaths(include_groups=True):
         subpath = fullpath + "." + ending
         while not (subpath.startswith(prefix) and
                    subpath[len(prefix)] == "."):
             curr, prefix = stack.pop()
         print subpath, fullpath, ending, curr
         sub, step = config._cfgimpl_get_home_by_path(ending)
         doc = getattr(sub._cfgimpl_descr, step).doc
         if doc:
             new = curr.add(ListItem(Link(subpath + ":", subpath + ".html"),
                                     Em(doc)))
         else:
             new = curr.add(ListItem(Link(subpath + ":", subpath + ".html")))
         stack.append((curr, prefix))
         prefix = subpath
         curr = new
     return content
Esempio n. 7
0
 def get_content(self, txtpath, encoding):
     if txtpath.basename == "commandline.txt":
         result = [".. contents::"]
         for descr in all_optiondescrs:
             result.append(".. %s_:\n" % (descr._name, ))
             result.append(make_cmdline_overview(descr).text())
             result.append("")
         result.append(txtpath.read())
         return "\n".join(result)
     fullpath = txtpath.purebasename
     start = fullpath.split(".")[0]
     path = fullpath.rsplit(".", 1)[0]
     basedescr = start_to_descr.get(start)
     if basedescr is None:
         return txtpath.read()
     if fullpath.count(".") == 0:
         descr = basedescr
         path = ""
     else:
         conf = Config(basedescr)
         subconf, step = conf._cfgimpl_get_home_by_path(
                 fullpath.split(".", 1)[1])
         descr = getattr(subconf._cfgimpl_descr, step)
     text = unicode(descr.make_rest_doc(path).text())
     if txtpath.check(file=True):
         content = txtpath.read()
         if content:
             text += "\nDescription\n==========="
             return u"%s\n\n%s" % (text, unicode(txtpath.read(), encoding))
     return text
Esempio n. 8
0
def get_combined_translation_config(other_optdescr=None,
                                    existing_config=None,
                                    overrides=None,
                                    translating=False):
    if overrides is None:
        overrides = {}
    d = BoolOption("translating",
                   "indicates whether we are translating currently",
                   default=False,
                   cmdline=None)
    if other_optdescr is None:
        children = []
        newname = ""
    else:
        children = [other_optdescr]
        newname = other_optdescr._name
    if existing_config is None:
        children += [d, translation_optiondescription]
    else:
        children += [
            child for child in existing_config._cfgimpl_descr._children
            if child._name != newname
        ]
    descr = OptionDescription("pypy", "all options", children)
    config = Config(descr, **overrides)
    if translating:
        config.translating = True
    if existing_config is not None:
        for child in existing_config._cfgimpl_descr._children:
            if child._name == newname:
                continue
            value = getattr(existing_config, child._name)
            config._cfgimpl_values[child._name] = value
    return config
Esempio n. 9
0
def test_check_documentation():
    from pypy.doc.config.confrest import all_optiondescrs
    configdocdir = thisdir.dirpath().dirpath().join("doc", "config")
    for descr in all_optiondescrs:
        prefix = descr._name
        c = Config(descr)
        for path in c.getpaths(include_groups=True):
            fn = prefix + "." + path + ".txt"
            assert configdocdir.join(fn).check()
Esempio n. 10
0
def test_check_documentation():
    def check_file_exists(fn):
        assert configdocdir.join(fn).check()

    from pypy.doc.config.confrest import all_optiondescrs
    configdocdir = thisdir.dirpath().dirpath().join("doc", "config")
    for descr in all_optiondescrs:
        prefix = descr._name
        c = Config(descr)
        for path in c.getpaths(include_groups=True):
            fn = prefix + "." + path + ".txt"
            yield check_file_exists, fn
Esempio n. 11
0
def rpython2javascript(mod, function_names, jsconfig=None, use_pdb=True):
    if isinstance(function_names, str):
        function_names = [function_names]
        # avoid confusion
    if mod is None:
        # this means actual module, which is quite hairy to get in python,
        # so we cheat
        import sys
        mod = sys.modules[sys._getframe(1).f_globals['__name__']]
    
    if jsconfig is None:
        jsconfig = Config(js_optiondescr)
    if use_pdb:
        jsconfig.use_pdb = True
    module_name = mod.__name__
    if not function_names and 'main' in mod.__dict__:
        function_names.append('main')
    for func_name in function_names:
        if func_name not in mod.__dict__:
            raise FunctionNotFound("function %r was not found in module %r" % (func_name, module_name))
        func_code = mod.__dict__[func_name]
        if func_code.func_defaults:
            lgt = len(func_code.func_defaults)
        else:
            lgt = 0
        if func_code.func_code.co_argcount > 0 and func_code.func_code. \
                co_argcount != lgt:
            raise BadSignature("Function %s does not have default arguments" % func_name)
    source_ssf = get_source_ssf(mod, module_name, function_names)
    exec(source_ssf) in globals()
    # now we gonna just cut off not needed function
    # XXX: Really do that
    #options = optparse.Values(defaults=DEFAULT_OPTIONS)
    from pypy.config.pypyoption import get_pypy_config
    config = get_pypy_config(translating=True)
    driver = TranslationDriver(config=config)
    try:
        driver.setup(some_strange_function_which_will_never_be_called, [], policy = JsPolicy())
        driver.proceed(["compile_js"])
        if jsconfig.view:
            driver.translator.view()
        return driver.gen.tmpfile.open().read()
        # XXX: Add some possibility to write down selected file
    except Exception, e:
        # do something nice with it
        debug(driver, use_pdb)
Esempio n. 12
0
 def make_rest_doc(self, path=""):
     fullpath = get_fullpath(self, path)
     content = Rest(
         Title(fullpath, abovechar="=", belowchar="="))
     toctree = []
     for child in self._children:
         subpath = fullpath + "." + child._name
         toctree.append(subpath)
     content.add(Directive("toctree", *toctree, **{'maxdepth': 4}))
     content.join(
         ListItem(Strong("name:"), self._name),
         ListItem(Strong("description:"), self.doc))
     stack = []
     curr = content
     config = Config(self)
     return content
Esempio n. 13
0
def main_(argv=None):
    starttime = time.time()
    config, parser = option.get_standard_options()
    interactiveconfig = Config(cmdline_optiondescr)
    to_optparse(interactiveconfig, parser=parser)
    parser.add_option('--cc',
                      type=str,
                      action="callback",
                      callback=set_compiler,
                      help="Compiler to use for compiling generated C")
    args = option.process_options(parser, argv[1:])
    if interactiveconfig.verbose:
        error.RECORD_INTERPLEVEL_TRACEBACK = True
    # --allworkingmodules takes really long to start up, but can be forced on
    config.objspace.suggest(allworkingmodules=False)
    if config.objspace.allworkingmodules:
        pypyoption.enable_allworkingmodules(config)

    # create the object space

    space = option.make_objspace(config)

    space._starttime = starttime
    space.setitem(space.sys.w_dict, space.wrap('executable'),
                  space.wrap(argv[0]))

    # set warning control options (if any)
    warn_arg = interactiveconfig.warn
    if warn_arg is not None:
        space.appexec([space.wrap(warn_arg)], """(arg): 
        import sys
        sys.warnoptions.append(arg)""")

    w_path = space.sys.get('path')
    path = os.getenv('PYTHONPATH')
    if path:
        path = path.split(os.pathsep)
    else:
        path = []
    path.insert(0, '')
    for i, dir in enumerate(path):
        space.call_method(w_path, 'insert', space.wrap(i), space.wrap(dir))

    # store the command-line arguments into sys.argv
    go_interactive = interactiveconfig.interactive
    banner = ''
    exit_status = 0
    if interactiveconfig.runcommand is not None:
        args = ['-c'] + args
    for arg in args:
        space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))

    # load the source of the program given as command-line argument
    if interactiveconfig.runcommand is not None:

        def doit():
            main.run_string(interactiveconfig.runcommand, space=space)
    elif interactiveconfig.runmodule:

        def doit():
            main.run_module(interactiveconfig.runmodule, args, space=space)
    elif args:
        scriptdir = os.path.dirname(os.path.abspath(args[0]))
        space.call_method(space.sys.get('path'), 'insert', space.wrap(0),
                          space.wrap(scriptdir))

        def doit():
            main.run_file(args[0], space=space)
    else:

        def doit():
            pass

        space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
        go_interactive = 1
        banner = None

    try:

        def do_start():
            space.startup()
            pypy_init(space, space.wrap(not interactiveconfig.no_site_import))

        if main.run_toplevel(space,
                             do_start,
                             verbose=interactiveconfig.verbose):
            # compile and run it
            if not main.run_toplevel(
                    space, doit, verbose=interactiveconfig.verbose):
                exit_status = 1

            # start the interactive console
            if go_interactive or os.getenv('PYTHONINSPECT'):
                try:
                    import readline
                except:
                    pass
                con = interactive.PyPyConsole(
                    space,
                    verbose=interactiveconfig.verbose,
                    completer=interactiveconfig.completer)
                if banner == '':
                    banner = '%s / %s' % (con.__class__.__name__, repr(space))
                con.interact(banner)
                exit_status = 0
    finally:

        def doit():
            space.finish()

        main.run_toplevel(space, doit, verbose=interactiveconfig.verbose)

    return exit_status
Esempio n. 14
0
def parse_options_and_load_target():
    opt_parser = optparse.OptionParser(usage="%prog [options] [target] [target-specific-options]",
                                       prog="translate",
                                       formatter=OptHelpFormatter(),
                                       add_help_option=False)

    opt_parser.disable_interspersed_args()

    config = get_combined_translation_config(
                overrides=OVERRIDES, translating=True)
    to_optparse(config, parser=opt_parser, useoptions=['translation.*'])
    translateconfig = Config(translate_optiondescr)
    to_optparse(translateconfig, parser=opt_parser)

    options, args = opt_parser.parse_args()

    # set goals and skipped_goals
    reset = False
    for name, _, _, _ in GOALS:
        if name.startswith('?'):
            continue
        if getattr(translateconfig.goal_options, name):
            if name not in translateconfig.goals:
                translateconfig.goals.append(name)
        if getattr(translateconfig.goal_options, 'no_'+name):
            if name not in translateconfig.skipped_goals:
                if not reset:
                    translateconfig.skipped_goals[:] = []
                    reset = True
                translateconfig.skipped_goals.append(name)
        
    if args:
        arg = args[0]
        args = args[1:]
        if os.path.isfile(arg+'.py'):
            assert not os.path.isfile(arg), (
                "ambiguous file naming, please rename %s" % arg)
            translateconfig.targetspec = arg
        elif os.path.isfile(arg) and arg.endswith('.py'):
            translateconfig.targetspec = arg[:-3]
        else:
            log.ERROR("Could not find target %r" % (arg, ))
            sys.exit(1)

    # apply the platform settings
    set_platform(config)

    targetspec = translateconfig.targetspec
    targetspec_dic = load_target(targetspec)

    if args and not targetspec_dic.get('take_options', False):
        log.WARNING("target specific arguments supplied but will be ignored: %s" % ' '.join(args))

    # give the target the possibility to get its own configuration options
    # into the config
    if 'get_additional_config_options' in targetspec_dic:
        optiondescr = targetspec_dic['get_additional_config_options']()
        config = get_combined_translation_config(
                optiondescr,
                existing_config=config,
                translating=True)

    # apply the optimization level settings
    set_opt_level(config, translateconfig.opt)

    # let the target modify or prepare itself
    # based on the config
    if 'handle_config' in targetspec_dic:
        targetspec_dic['handle_config'](config, translateconfig)

    # perform checks (if any) on the final config
    final_check_config(config)

    if translateconfig.help:
        opt_parser.print_help()
        if 'print_help' in targetspec_dic:
            print "\n\nTarget specific help:\n\n"
            targetspec_dic['print_help'](config)
        print "\n\nFor detailed descriptions of the command line options see"
        print "http://codespeak.net/pypy/dist/pypy/doc/config/commandline.html"
        sys.exit(0)
    
    return targetspec_dic, translateconfig, config, args
Esempio n. 15
0
File: py.py Progetto: chyyuu/pygirl
def main_(argv=None):
    starttime = time.time()
    config, parser = option.get_standard_options()
    interactiveconfig = Config(cmdline_optiondescr)
    to_optparse(interactiveconfig, parser=parser)
    args = option.process_options(parser, argv[1:])
    if interactiveconfig.verbose:
        error.RECORD_INTERPLEVEL_TRACEBACK = True

    # create the object space

    space = option.make_objspace(config)

    space._starttime = starttime
    space.setitem(space.sys.w_dict, space.wrap('executable'),
                  space.wrap(argv[0]))

    # store the command-line arguments into sys.argv
    go_interactive = interactiveconfig.interactive
    banner = ''
    exit_status = 0
    if interactiveconfig.runcommand is not None:
        args = ['-c'] + args
    for arg in args:
        space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))

    # load the source of the program given as command-line argument
    if interactiveconfig.runcommand is not None:

        def doit():
            main.run_string(interactiveconfig.runcommand, space=space)
    elif interactiveconfig.runmodule:

        def doit():
            main.run_module(interactiveconfig.runmodule, args, space=space)
    elif args:
        scriptdir = os.path.dirname(os.path.abspath(args[0]))
        space.call_method(space.sys.get('path'), 'insert', space.wrap(0),
                          space.wrap(scriptdir))

        def doit():
            main.run_file(args[0], space=space)
    else:

        def doit():
            pass

        space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
        go_interactive = 1
        banner = None

    try:

        def do_start():
            space.startup()
            pypy_init(space, space.wrap(not interactiveconfig.no_site_import))

        if main.run_toplevel(space,
                             do_start,
                             verbose=interactiveconfig.verbose):
            # compile and run it
            if not main.run_toplevel(
                    space, doit, verbose=interactiveconfig.verbose):
                exit_status = 1

            # start the interactive console
            if go_interactive or getenv_w(space, 'PYTHONINSPECT'):
                try:
                    import readline
                except:
                    pass
                con = interactive.PyPyConsole(
                    space,
                    verbose=interactiveconfig.verbose,
                    completer=interactiveconfig.completer)
                if banner == '':
                    banner = '%s / %s' % (con.__class__.__name__, repr(space))
                con.interact(banner)
                exit_status = 0
    finally:

        def doit():
            space.finish()

        main.run_toplevel(space, doit, verbose=interactiveconfig.verbose)

    return exit_status
Esempio n. 16
0
def setup_module(mod):
    mod.jsconfig = Config(js_optiondescr)
Esempio n. 17
0
def parse_options_and_load_target():
    opt_parser = optparse.OptionParser(
        usage="%prog [options] [target] [target-specific-options]",
        prog="translate",
        formatter=OptHelpFormatter(),
        add_help_option=False)

    opt_parser.disable_interspersed_args()

    config = get_combined_translation_config(overrides=OVERRIDES,
                                             translating=True)
    to_optparse(config, parser=opt_parser, useoptions=['translation.*'])
    translateconfig = Config(translate_optiondescr)
    to_optparse(translateconfig, parser=opt_parser)

    options, args = opt_parser.parse_args()

    # set goals and skipped_goals
    reset = False
    for name, _, _, _ in GOALS:
        if name.startswith('?'):
            continue
        if getattr(translateconfig.goal_options, name):
            if name not in translateconfig.goals:
                translateconfig.goals.append(name)
        if getattr(translateconfig.goal_options, 'no_' + name):
            if name not in translateconfig.skipped_goals:
                if not reset:
                    translateconfig.skipped_goals[:] = []
                    reset = True
                translateconfig.skipped_goals.append(name)

    if args:
        arg = args[0]
        args = args[1:]
        if os.path.isfile(arg + '.py'):
            assert not os.path.isfile(arg), (
                "ambiguous file naming, please rename %s" % arg)
            translateconfig.targetspec = arg
        elif os.path.isfile(arg) and arg.endswith('.py'):
            translateconfig.targetspec = arg[:-3]
        else:
            log.ERROR("Could not find target %r" % (arg, ))
            sys.exit(1)

    # apply the platform settings
    set_platform(config)

    targetspec = translateconfig.targetspec
    targetspec_dic = load_target(targetspec)

    if args and not targetspec_dic.get('take_options', False):
        log.WARNING(
            "target specific arguments supplied but will be ignored: %s" %
            ' '.join(args))

    # give the target the possibility to get its own configuration options
    # into the config
    if 'get_additional_config_options' in targetspec_dic:
        optiondescr = targetspec_dic['get_additional_config_options']()
        config = get_combined_translation_config(optiondescr,
                                                 existing_config=config,
                                                 translating=True)

    # apply the optimization level settings
    set_opt_level(config, translateconfig.opt)

    # let the target modify or prepare itself
    # based on the config
    if 'handle_config' in targetspec_dic:
        targetspec_dic['handle_config'](config, translateconfig)

    # perform checks (if any) on the final config
    final_check_config(config)

    if translateconfig.help:
        opt_parser.print_help()
        if 'print_help' in targetspec_dic:
            print "\n\nTarget specific help:\n\n"
            targetspec_dic['print_help'](config)
        print "\n\nFor detailed descriptions of the command line options see"
        print "http://pypy.readthedocs.org/en/latest/config/commandline.html"
        sys.exit(0)

    return targetspec_dic, translateconfig, config, args
Esempio n. 18
0
def process_options():
    jsconfig = Config(js_optiondescr)
    parser = to_optparse(jsconfig, parserkwargs={"usage": __doc__})
    parser.disable_interspersed_args()
    options, args = parser.parse_args()
    return args, jsconfig