Пример #1
0
    def __init__(self, lexer=None):
        if lexer is None:
            self.lexer = MyLexer(stage="post_processed",
                                 optimize=_OPTIMIZE_LEX)
        else:
            self.lexer = lexer

        picklefile = _PICKLED_PARSETAB
        if not op.exists(picklefile):
            try:
                fid = open(picklefile, "wb")
                fid.close()
            except IOError:
                e = extract_exception()
                raise BentoError(
                    "Could not write pickle file %r (original error was %r)" %
                    (picklefile, e))
        else:
            try:
                fid = open(_PICKLED_PARSETAB, "wb")
                fid.close()
            except IOError:
                # In case of read-only support (no write access, zip import,
                # etc...)
                e = extract_exception()
                if e.errno == errno.EACCES:
                    if _has_parser_changed(_PICKLED_PARSETAB):
                        raise BentoError(
                            "Cannot write new updated grammar to file %r" %
                            _PICKLED_PARSETAB)
                else:
                    raise
        self.parser = ply.yacc.yacc(start="stmt_list",
                                    picklefile=picklefile,
                                    debug=_DEBUG_YACC)
Пример #2
0
    def build_compiled_library(self, library):
        import distutils.errors

        bld_cmd = self.clib_bld_cmd
        compiler = bld_cmd.compiler
        base, filename = os.path.split(self._compiled_library_filename(library.name, compiler))
        old_build_clib = bld_cmd.build_clib
        if base:
            # workaround for a distutils issue: distutils put all C libraries
            # in the same directory, and we cannot control the output directory
            # from the name - we need to hack build_clib directory
            bld_cmd.build_clib = os.path.join(old_build_clib, base)
        try:
            try:
                # workaround for yet another bug in distutils: distutils f***s up when
                # building a static library if the target alread exists on at least mac
                # os x.
                target = os.path.join(old_build_clib, base, filename)
                try:
                    os.remove(target)
                except OSError:
                    e = extract_exception()
                    if e.errno != errno.ENOENT:
                        raise
                build_info = {"sources": library.sources,
                        "include_dirs": library.include_dirs}
                bld_cmd.build_libraries([(library.name, build_info)])

                return [relpath(target, self._build_base)]
            except distutils.errors.DistutilsError:
                e = extract_exception()
                raise bento.errors.BuildError(str(e))
        finally:
            bld_cmd.build_clib = old_build_clib
Пример #3
0
def upload(dist_filename, dist_type, package, config, sign=False):
    schema, netloc, url, params, query, fragments = urlparse(config.repository)
    if params or query or fragments:
        raise InvalidRepository("Incompatible url %s" % config.repository)

    if schema not in ('http', 'https'):
        raise InvalidRepository("unsupported schema " + schema)

    if sign:
        raise NotImplementedError()

    data = build_upload_post_data(dist_filename, dist_type, package)
    userpass = (config.username + ":" + config.password).encode("ascii")
    auth = six.b("Basic ") + base64.standard_b64encode(userpass)
    request = build_request(config.repository, data, auth)

    try:
        result = urlopen(request)
        status = result.getcode()
        reason = result.msg
    except HTTPError:
        e = extract_exception()
        status = e.code
        reason = e.msg
    except URLError:
        e = extract_exception()
        reason = e.reason
        raise PyPIError(
                "Could not upload to repository %r - error %s" \
                % (config.repository, reason))

    if status != 200:
        raise PyPIError(
                "Could not upload to repository %r - error %s (server answered '%s')" \
                % (config.repository, status, reason))
Пример #4
0
    def __init__(self, lexer=None):
        if lexer is None:
            self.lexer = MyLexer(stage="post_processed", optimize=_OPTIMIZE_LEX)
        else:
            self.lexer = lexer

        picklefile = _PICKLED_PARSETAB
        if not op.exists(picklefile):
            try:
                fid = open(picklefile, "wb")
                fid.close()
            except IOError:
                e = extract_exception()
                raise BentoError("Could not write pickle file %r (original error was %r)" % (picklefile, e))
        else:
            try:
                fid = open(_PICKLED_PARSETAB, "wb")
                fid.close()
            except IOError:
                # In case of read-only support (no write access, zip import,
                # etc...)
                e = extract_exception()
                if e.errno == errno.EACCES:
                    if _has_parser_changed(_PICKLED_PARSETAB):
                        raise BentoError("Cannot write new updated grammar to file %r" % _PICKLED_PARSETAB)
                else:
                    raise
        self.parser = ply.yacc.yacc(start="stmt_list",
                                picklefile=picklefile,
                                debug=_DEBUG_YACC)
Пример #5
0
def noexc_main(argv=None):
    def _print_debug():
        if BENTOMAKER_DEBUG:
            tb = sys.exc_info()[2]
            traceback.print_tb(tb)
    def _print_error(msg):
        pprint('RED', msg)
        if not BENTOMAKER_DEBUG:
            pprint('RED', "(You can see the traceback by setting the " \
                          "BENTOMAKER_DEBUG=1 environment variable)")

    try:
        main(argv)
    except bento.errors.BentoError:
        _print_debug()
        e = extract_exception()
        _print_error(str(e))
        sys.exit(2)
    except Exception:
        msg = """\
%s: Error: %s crashed (uncaught exception %s: %s).
Please report this on bento issue tracker:
    http://github.com/cournape/bento/issues"""
        if not BENTOMAKER_DEBUG:
            msg += "\nYou can get a full traceback by setting BENTOMAKER_DEBUG=1"
        else:
            _print_debug()
        e = extract_exception()
        pprint('RED',  msg % (SCRIPT_NAME, SCRIPT_NAME, e.__class__, str(e)))
        sys.exit(1)
Пример #6
0
def upload(dist_filename, dist_type, package, config, sign=False):
    schema, netloc, url, params, query, fragments = urlparse(config.repository)
    if params or query or fragments:
        raise InvalidRepository("Incompatible url %s" % config.repository)

    if schema not in ('http', 'https'):
        raise InvalidRepository("unsupported schema " + schema)

    if sign:
        raise NotImplementedError()

    data = build_upload_post_data(dist_filename, dist_type, package)
    userpass = (config.username + ":" + config.password).encode("ascii")
    auth = six.b("Basic ") + base64.standard_b64encode(userpass)
    request = build_request(config.repository, data, auth)

    try:
        result = urlopen(request)
        status = result.getcode()
        reason = result.msg
    except HTTPError:
        e = extract_exception()
        status = e.code
        reason = e.msg
    except URLError:
        e = extract_exception()
        reason = e.reason
        raise PyPIError(
                "Could not upload to repository %r - error %s" \
                % (config.repository, reason))

    if status != 200:
        raise PyPIError(
                "Could not upload to repository %r - error %s (server answered '%s')" \
                % (config.repository, status, reason))
Пример #7
0
    def build_compiled_library(self, library):
        import distutils.errors

        bld_cmd = self.clib_bld_cmd
        compiler = bld_cmd.compiler
        base, filename = os.path.split(self._compiled_library_filename(library.name, compiler))
        old_build_clib = bld_cmd.build_clib
        if base:
            # workaround for a distutils issue: distutils put all C libraries
            # in the same directory, and we cannot control the output directory
            # from the name - we need to hack build_clib directory
            bld_cmd.build_clib = os.path.join(old_build_clib, base)
        try:
            try:
                # workaround for yet another bug in distutils: distutils f***s up when
                # building a static library if the target alread exists on at least mac
                # os x.
                target = os.path.join(old_build_clib, base, filename)
                try:
                    os.remove(target)
                except OSError:
                    e = extract_exception()
                    if e.errno != errno.ENOENT:
                        raise
                build_info = {"sources": library.sources, "include_dirs": library.include_dirs}
                bld_cmd.build_libraries([(library.name, build_info)])

                return [relpath(target, self._build_base)]
            except distutils.errors.DistutilsError:
                e = extract_exception()
                raise bento.errors.BuildError(str(e))
        finally:
            bld_cmd.build_clib = old_build_clib
Пример #8
0
def noexc_main(argv=None):
    def _print_debug():
        if BENTOMAKER_DEBUG:
            tb = sys.exc_info()[2]
            traceback.print_tb(tb)

    def _print_error(msg):
        pprint('RED', msg)
        if not BENTOMAKER_DEBUG:
            pprint('RED', "(You can see the traceback by setting the " \
                          "BENTOMAKER_DEBUG=1 environment variable)")

    try:
        main(argv)
    except bento.errors.BentoError:
        _print_debug()
        e = extract_exception()
        _print_error(str(e))
        sys.exit(2)
    except Exception:
        msg = """\
%s: Error: %s crashed (uncaught exception %s: %s).
Please report this on bento issue tracker:
    http://github.com/cournape/bento/issues"""
        if not BENTOMAKER_DEBUG:
            msg += "\nYou can get a full traceback by setting BENTOMAKER_DEBUG=1"
        else:
            _print_debug()
        e = extract_exception()
        pprint('RED', msg % (SCRIPT_NAME, SCRIPT_NAME, e.__class__, str(e)))
        sys.exit(1)
Пример #9
0
    def compile(self):
        super(BuildWafContext, self).compile()
        reg = self.builder_registry

        for category in ("extensions", "compiled_libraries"):
            for name, extension in self._node_pkg.iter_category(category):
                builder = reg.builder(category, name)
                self.pre_recurse(extension.ref_node)
                try:
                    extension = extension.extension_from(extension.ref_node)
                    task_gen = builder(extension)
                finally:
                    self.post_recurse()

        if self.progress_bar:
            sys.stderr.write(Logs.colors.cursor_off)
        try:
            self.waf_context.compile()
        except waflib.Errors.BuildError:
            e = extract_exception()
            if len(e.tasks) > 0:
                task0 = e.tasks[0]
                raise bento.errors.BuildError("Error while compiling %r"  % task0.inputs[0].abspath())
            else:
                raise
        finally:
            if self.progress_bar:
                c = len(self.waf_context.returned_tasks) or 1
                self.waf_context.to_log(self.waf_context.progress_line(c, c, Logs.colors.BLUE, Logs.colors.NORMAL))
                print('')
                sys.stdout.flush()
                sys.stderr.write(Logs.colors.cursor_on)
Пример #10
0
def create_hook_module(target):
    safe_name = SAFE_MODULE_NAME.sub("_", target, len(target))
    module_name = "bento_hook_%s" % safe_name
    main_file = os.path.abspath(target)
    module = imp.new_module(module_name)
    module.__file__ = main_file
    code = open(main_file).read()

    sys.path.insert(0, os.path.dirname(main_file))
    try:
        exec(compile(code, main_file, 'exec'), module.__dict__)
        sys.modules[module_name] = module
    except Exception:
        sys.path.pop(0)
        e = extract_exception()
        tb = sys.exc_info()[2]
        s = StringIO()
        traceback.print_tb(tb, file=s)
        msg = """\
Could not import hook file %r: caught exception %r
Original traceback (most recent call last)
%s\
""" % (main_file, e, s.getvalue())
        raise InvalidHook(msg)

    module.root_path = main_file
    return module
Пример #11
0
def create_hook_module(target):
    safe_name = SAFE_MODULE_NAME.sub("_", target, len(target))
    module_name = "bento_hook_%s" % safe_name
    main_file = os.path.abspath(target)
    module = imp.new_module(module_name)
    module.__file__ = main_file
    code = open(main_file).read()

    sys.path.insert(0, os.path.dirname(main_file))
    try:
        exec(compile(code, main_file, 'exec'), module.__dict__)
        sys.modules[module_name] = module
    except Exception:
        sys.path.pop(0)
        e = extract_exception()
        tb = sys.exc_info()[2]
        s = StringIO()
        traceback.print_tb(tb, file=s)
        msg = """\
Could not import hook file %r: caught exception %r
Original traceback (most recent call last)
%s\
""" % (main_file, e, s.getvalue())
        raise InvalidHook(msg)

    module.root_path = main_file
    return module
Пример #12
0
def convert(ctx, filename, setup_args, monkey_patch_mode, verbose, output, log, show_output=True):
    if monkey_patch_mode == "automatic":
        try:
            if verbose:
                pprint("PINK",
                       "Catching monkey (this may take a while) ...")
            monkey_patch_mode = detect_monkeys(filename, show_output, log)
            if verbose:
                pprint("PINK", "Detected mode: %s" % monkey_patch_mode)
        except ValueError:
            e = extract_exception()
            raise UsageException("Error while detecting setup.py type " \
                                 "(original error: %s)" % str(e))

    monkey_patch(ctx.top_node, monkey_patch_mode, filename)
    dist, package_objects = analyse_setup_py(filename, setup_args)
    pkg, options = build_pkg(dist, package_objects, ctx.top_node)

    out = static_representation(pkg, options)
    if output == '-':
        for line in out.splitlines():
            pprint("YELLOW", line)
    else:
        fid = open(output, "w")
        try:
            fid.write(out)
        finally:
            fid.close()
Пример #13
0
    def compile(self):
        super(BuildWafContext, self).compile()
        reg = self.builder_registry

        for category in ("extensions", "compiled_libraries"):
            for name, extension in self._node_pkg.iter_category(category):
                builder = reg.builder(category, name)
                self.pre_recurse(extension.ref_node)
                try:
                    extension = extension.extension_from(extension.ref_node)
                    task_gen = builder(extension)
                finally:
                    self.post_recurse()

        if self.progress_bar:
            sys.stderr.write(Logs.colors.cursor_off)
        try:
            self.waf_context.compile()
        except waflib.Errors.BuildError:
            e = extract_exception()
            if len(e.tasks) > 0:
                task0 = e.tasks[0]
                raise bento.errors.BuildError("Error while compiling %r" %
                                              task0.inputs[0].abspath())
            else:
                raise
        finally:
            if self.progress_bar:
                c = len(self.waf_context.returned_tasks) or 1
                self.waf_context.to_log(
                    self.waf_context.progress_line(c, c, Logs.colors.BLUE,
                                                   Logs.colors.NORMAL))
                print('')
                sys.stdout.flush()
                sys.stderr.write(Logs.colors.cursor_on)
Пример #14
0
def convert(ctx,
            filename,
            setup_args,
            monkey_patch_mode,
            verbose,
            output,
            log,
            show_output=True):
    if monkey_patch_mode == "automatic":
        try:
            if verbose:
                pprint("PINK", "Catching monkey (this may take a while) ...")
            monkey_patch_mode = detect_monkeys(filename, show_output, log)
            if verbose:
                pprint("PINK", "Detected mode: %s" % monkey_patch_mode)
        except ValueError:
            e = extract_exception()
            raise UsageException("Error while detecting setup.py type " \
                                 "(original error: %s)" % str(e))

    monkey_patch(ctx.top_node, monkey_patch_mode, filename)
    dist, package_objects = analyse_setup_py(filename, setup_args)
    pkg, options = build_pkg(dist, package_objects, ctx.top_node)

    out = static_representation(pkg, options)
    if output == '-':
        for line in out.splitlines():
            pprint("YELLOW", line)
    else:
        fid = open(output, "w")
        try:
            fid.write(out)
        finally:
            fid.close()
Пример #15
0
 def get_options(self, bento_info):
     try:
         return self._get_options(bento_info)
     except Exception:
         e = extract_exception()
         warnings.warn("Resetting invalid cache (error was %r)" % e)
         self._reset()
         return self._get_options(bento_info)
Пример #16
0
def raw_parse(data, filename=None):
    try:
        ret = _parse(data)
        return ret
    except ParseError:
        e = extract_exception()
        e.filename = filename
        raise
Пример #17
0
 def get_package(self, bento_info, user_flags=None):
     try:
         return self._get_package(bento_info, user_flags)
     except Exception:
         e = extract_exception()
         warnings.warn("Resetting invalid cache (error was %r)" % e)
         self._reset()
         return self._get_package(bento_info, user_flags)
Пример #18
0
def raw_parse(data, filename=None):
    try:
        ret = _parse(data)
        return ret
    except ParseError:
        e = extract_exception()
        e.filename = filename
        raise
Пример #19
0
def rename(source, target):
    try:
        _rename(source, target)
    except OSError:
        e = extract_exception()
        if e.errno == errno.EXDEV:
            shutil.move(source, target)
        else:
            raise
Пример #20
0
def rename(source, target):
    try:
        _rename(source, target)
    except OSError:
        e = extract_exception()
        if e.errno == errno.EXDEV:
            shutil.move(source, target)
        else:
            raise
Пример #21
0
def analyse_setup_py(filename, setup_args, verbose=False):
    # This is the dirty part: we run setup.py inside this process, and pass
    # data back through global variables. Not sure if there is a better way to
    # do this
    if verbose:
        pprint('PINK',
               "======================================================")
        pprint('PINK',
               " Analysing %s (running %s) .... " % (filename, filename))

    # exec_globals contains the globals used to execute the setup.py
    exec_globals = {}
    exec_globals.update(globals())
    # Some setup.py files call setup from their main, so execute them as if
    # they were the main script
    exec_globals["__name__"] = "__main__"
    exec_globals["__file__"] = op.abspath(filename)

    _saved_argv = sys.argv[:]
    _saved_sys_path = sys.path
    try:
        try:
            sys.argv = [filename] + setup_args + ["build_py"]
            # XXX: many packages import themselves to get version at build
            # time, and setuptools screw this up by inserting stuff first. Is
            # there a better way ?
            sys.path.insert(0, op.dirname(filename))
            fid = open(filename, "r")
            try:
                exec(fid.read(), exec_globals)
                if type == "distutils" and "setuptools" in sys.modules and verbose:
                    pprint("YELLOW",
                           "Setuptools detected in distutils mode !!!")
            finally:
                fid.close()
        except ConvertionError:
            raise
        except Exception:
            e = extract_exception()
            pprint('RED', "Got exception: %s" % e)
            raise
    finally:
        sys.argv = _saved_argv
        sys.path = _saved_sys_path

    live_objects = PACKAGE_OBJECTS
    dist = DIST_GLOBAL
    if dist is None:
        raise ValueError("setup monkey-patching failed")
    else:
        if verbose:
            pprint('PINK', " %s analyse done " % filename)
            pprint('PINK',
                   "======================================================")
        return dist, live_objects
Пример #22
0
def post_to_server(post_data, config, auth=None):
    """Send the given post_data to the pypi server.

    Parameters
    ----------
    post_data: dict
        Usually the dict returned by build_post_data
    config: object
        A PyPIConfig instance
    auth: object or None
        HTTP authentification object.

    Returns
    -------
    code: int
        HTTP status code
    msg: str
        Message received back from the server
    """
    content_type, body = encode_multipart(post_data.items(), [])

    # build the Request
    headers = {
        'Content-type': content_type,
        'Content-length': str(len(body))
    }
    req = Request(config.repository, body, headers)

    # handle HTTP and include the Basic Auth handler
    opener = build_opener(HTTPBasicAuthHandler(password_mgr=auth))
    try:
        opener.open(req)
    except HTTPError:
        e = extract_exception()
        code, msg = e.code, e.msg
    except URLError:
        e = extract_exception()
        code, msg = 500, str(e)
    else:
        code, msg = 200, 'OK'

    return code, msg
Пример #23
0
def build_compiled_library(bld, clib, env=None):
    builder = bld.builders["ctasks"]
    try:
        for p in clib.include_dirs:
            builder.env["CPPPATH"].insert(0, p)
        outputs = builder.static_library(clib.name, clib.sources, env)
        return [n.bldpath() for n in outputs]
    except RuntimeError:
        e = extract_exception()
        msg = "Building library %s failed: %s" % (clib.name, str(e))
        raise CommandExecutionFailure(msg)
Пример #24
0
def build_compiled_library(bld, clib, env=None):
    builder = bld.builders["ctasks"]
    try:
        for p in clib.include_dirs:
            builder.env["CPPPATH"].insert(0, p)
        outputs = builder.static_library(clib.name, clib.sources, env)
        return [n.bldpath() for n in outputs]
    except RuntimeError:
        e = extract_exception()
        msg = "Building library %s failed: %s" % (clib.name, str(e))
        raise CommandExecutionFailure(msg)
Пример #25
0
    def configure(self):
        extensions = get_extensions(self.pkg, self.run_node)
        libraries = get_compiled_libraries(self.pkg, self.run_node)

        yaku_ctx = self.yaku_context
        if extensions or libraries:
            for t in ["ctasks", "pyext"]:
                try:
                    yaku_ctx.use_tools([t])
                except yaku.errors.ConfigurationFailure:
                    e = extract_exception()
                    raise ConfigurationError(str(e))
Пример #26
0
    def configure(self):
        extensions = get_extensions(self.pkg, self.run_node)
        libraries = get_compiled_libraries(self.pkg, self.run_node)

        yaku_ctx = self.yaku_context
        if extensions or libraries:
            for t in ["ctasks", "pyext"]:
                try:
                    yaku_ctx.use_tools([t])
                except yaku.errors.ConfigurationFailure:
                    e = extract_exception()
                    raise ConfigurationError(str(e))
Пример #27
0
    def run(self, context):
        if not self.can_run():
            return bento.errors.CommandExecutionFailure("sphinx not available")
        import sphinx.application

        p = context.options_context.parser
        o, a = p.parse_args(context.command_argv)
        if o.output_format != "html":
            raise ValueError("Only html output supported for now")
        else:
            builder = "html"

        if o.source_dir is None:
            source_dir = guess_source_dir()
        else:
            source_dir = o.source_dir
        if source_dir is None:
            raise bento.errors.UsageException(
                """\
Doc source dir could not be found: please specify the directory where your
sphinx conf.py is located with the --source-dir option"""
            )
        if not op.isabs(source_dir):
            source_dir = op.join(context.top_node.abspath(), source_dir)

        sphinx_build = context.build_node.make_node("sphinx")
        html_build = sphinx_build.make_node(o.output_format)
        doctrees_build = sphinx_build.make_node("doctrees")

        doc_html_build = html_build.abspath()
        doc_doctrees_build = doctrees_build.abspath()

        confoverrides = {}
        status_stream = sys.stdout
        fresh_env = False
        force_all = False

        app = sphinx.application.Sphinx(
            source_dir,
            source_dir,
            doc_html_build,
            doc_doctrees_build,
            builder,
            confoverrides,
            status_stream,
            freshenv=fresh_env,
        )
        try:
            app.build(force_all=force_all)
        except Exception:
            err = extract_exception()
            raise bento.errors.CommandExecutionFailure("error while building doc: %r" % str(err))
Пример #28
0
 def __init__(self, db_location):
     self._location = db_location
     self._first_time = False
     if not os.path.exists(db_location):
         bento.utils.path.ensure_dir(db_location)
         self._reset()
     else:
         try:
             self.db = self._load_existing_cache(db_location)
         except Exception:
             e = extract_exception()
             warnings.warn("Resetting invalid cached db: (reason: %r)" % e)
             self._reset()
Пример #29
0
def post_to_server(post_data, config, auth=None):
    """Send the given post_data to the pypi server.

    Parameters
    ----------
    post_data: dict
        Usually the dict returned by build_post_data
    config: object
        A PyPIConfig instance
    auth: object or None
        HTTP authentification object.

    Returns
    -------
    code: int
        HTTP status code
    msg: str
        Message received back from the server
    """
    content_type, body = encode_multipart(post_data.items(), [])

    # build the Request
    headers = {'Content-type': content_type, 'Content-length': str(len(body))}
    req = Request(config.repository, body, headers)

    # handle HTTP and include the Basic Auth handler
    opener = build_opener(HTTPBasicAuthHandler(password_mgr=auth))
    try:
        opener.open(req)
    except HTTPError:
        e = extract_exception()
        code, msg = e.code, e.msg
    except URLError:
        e = extract_exception()
        code, msg = 500, str(e)
    else:
        code, msg = 200, 'OK'

    return code, msg
Пример #30
0
def analyse_setup_py(filename, setup_args, verbose=False):
    # This is the dirty part: we run setup.py inside this process, and pass
    # data back through global variables. Not sure if there is a better way to
    # do this
    if verbose:
        pprint('PINK', "======================================================")
        pprint('PINK', " Analysing %s (running %s) .... " % (filename, filename))

    # exec_globals contains the globals used to execute the setup.py
    exec_globals = {}
    exec_globals.update(globals())
    # Some setup.py files call setup from their main, so execute them as if
    # they were the main script
    exec_globals["__name__"] = "__main__"
    exec_globals["__file__"] = op.abspath(filename)

    _saved_argv = sys.argv[:]
    _saved_sys_path = sys.path
    try:
        try:
            sys.argv = [filename] + setup_args + ["build_py"]
            # XXX: many packages import themselves to get version at build
            # time, and setuptools screw this up by inserting stuff first. Is
            # there a better way ?
            sys.path.insert(0, op.dirname(filename))
            fid = open(filename, "r")
            try:
                exec(fid.read(), exec_globals)
                if type == "distutils" and "setuptools" in sys.modules and verbose:
                    pprint("YELLOW", "Setuptools detected in distutils mode !!!")
            finally:
                fid.close()
        except ConvertionError:
            raise
        except Exception:
            e = extract_exception()
            pprint('RED', "Got exception: %s" % e)
            raise
    finally:
        sys.argv = _saved_argv
        sys.path = _saved_sys_path

    live_objects = PACKAGE_OBJECTS
    dist = DIST_GLOBAL
    if dist is None:
        raise ValueError("setup monkey-patching failed")
    else:
        if verbose:
            pprint('PINK', " %s analyse done " % filename)
            pprint('PINK', "======================================================")
        return dist, live_objects
Пример #31
0
    def run(self, ctx):
        argv = ctx.command_argv
        p = ctx.options_context.parser
        o, a = p.parse_args(argv)
        if o.help:
            p.print_help()
            return

        if len(a) < 1:
            filename = "bento.info"
        else:
            filename = a[0]

        if not os.path.exists(filename):
            raise UsageException("%s: error: file %s does not exist" %
                                 "bentomaker")

        f = open(filename, "r")
        try:
            data = f.read()
            try:
                parsed = build_ast_from_data(data)
            except ParseError:
                e = extract_exception()
                msg = "Error while parsing file %s\n" % filename
                e.args = (msg, ) + e.args
                raise e
            if o.flags:
                try:
                    flags = parsed["flags"]
                    for flag in flags:
                        print((flags[flag]))
                except KeyError:
                    pass
            elif o.path:
                try:
                    paths = parsed["paths"]
                    for path in paths:
                        print((paths[path]))
                except KeyError:
                    pass
            elif o.meta_field:
                try:
                    print((parsed[o.meta_field]))
                except KeyError:
                    raise ValueError("Field %s not found in metadata" %
                                     o.meta_field)
            else:
                pprint(parsed)
        finally:
            f.close()
Пример #32
0
 def makedirs(self, name, mode=MODE_777):
     head, tail = os.path.split(name)
     if not tail:
         head, tail = os.path.split(head)
     if head and tail and not os.path.exists(head):
         try:
             self.makedirs(head, mode)
         except OSError:
             e = extract_exception()
             if e.errno != errno.EEXIST:
                 raise
         if tail == os.curdir:
             return
     self.mkdir(name, mode)
Пример #33
0
    def run(self, ctx):
        argv = ctx.command_argv
        p = ctx.options_context.parser
        o, a = p.parse_args(argv)
        if o.help:
            p.print_help()
            return
        if len(a) < 1:
            filename = "setup.py"
        else:
            filename = a[0]
        if not op.exists(filename):
            raise ValueError("file %s not found" % filename)

        output = o.output_filename
        if op.exists(output):
            raise UsageException("file %s exists, not overwritten" % output)

        if o.verbose:
            show_output = True
        else:
            show_output = False

        if o.setup_args:
            setup_args = comma_list_split(o.setup_args)
        else:
            setup_args = ["-q", "-n"]

        monkey_patch_mode = o.type

        convert_log = "convert.log"
        log = open(convert_log, "w")
        try:
            try:
                convert(ctx, filename, setup_args, monkey_patch_mode,
                        o.verbose, output, log, show_output)
            except ConvertionError:
                raise
            except Exception:
                e = extract_exception()
                log.write("Error while converting - traceback:\n")
                tb = sys.exc_info()[2]
                traceback.print_tb(tb, file=log)
                msg = "Error while converting %s - you may look at %s for " \
                      "details (Original exception: %s %s)"
                raise ConvertionError(msg %
                                      (filename, convert_log, type(e), str(e)))
        finally:
            log.flush()
            log.close()
Пример #34
0
    def test_error_string(self):
        f = self.f
        f.write("NName: foo")
        f.flush()
        try:
            PackageDescription.from_file(f.name)
            raise AssertionError("Should raise here !")
        except ParseError:
            e = extract_exception()
            self.assertEqual(str(e), """\
  File "%s", line 1
NName: foo
^
Syntax error""" % f.name)
Пример #35
0
 def makedirs(self, name, mode=MODE_777):
     head, tail = os.path.split(name)
     if not tail:
         head, tail = os.path.split(head)
     if head and tail and not os.path.exists(head):
         try:
             self.makedirs(head, mode)
         except OSError:
             e = extract_exception()
             if e.errno != errno.EEXIST:
                 raise
         if tail == os.curdir:
             return
     self.mkdir(name, mode)
Пример #36
0
def _rollback_operation(line):
    operation, arg = line.split()
    if operation == "MKDIR":
        try:
            os.rmdir(arg)
        except OSError:
            e = extract_exception()
            # FIXME: hardcoded errno !!
            if e.errno != 66:
                raise
    elif operation == "COPY":
        os.remove(arg)
    else:
        raise ValueError("Unknown operation: %s" % operation)
Пример #37
0
def _rollback_operation(line):
    operation, arg = line.split()
    if operation == "MKDIR":
        try:
            os.rmdir(arg)
        except OSError:
            e = extract_exception()
            # FIXME: hardcoded errno !!
            if e.errno != 66:
                raise
    elif operation == "COPY":
        os.remove(arg)
    else:
        raise ValueError("Unknown operation: %s" % operation)
Пример #38
0
    def run(self, ctx):
        argv = ctx.command_argv
        p = ctx.options_context.parser
        o, a = p.parse_args(argv)
        if o.help:
            p.print_help()
            return

        if len(a) < 1:
            filename = "bento.info"
        else:
            filename = a[0]

        if not os.path.exists(filename):
            raise UsageException("%s: error: file %s does not exist" % "bentomaker")

        f = open(filename, "r")
        try:
            data = f.read()
            try:
                parsed = build_ast_from_data(data)
            except ParseError:
                e = extract_exception()
                msg = "Error while parsing file %s\n" % filename
                e.args = (msg,) +  e.args
                raise e
            if o.flags:
                try:
                    flags = parsed["flags"]
                    for flag in flags:
                        print(flags[flag])
                except KeyError:
                    pass
            elif o.path:
                try:
                    paths = parsed["paths"]
                    for path in paths:
                        print(paths[path])
                except KeyError:
                    pass
            elif o.meta_field:
                try:
                    print(parsed[o.meta_field])
                except KeyError:
                    raise ValueError("Field %s not found in metadata" % o.meta_field)
            else:
                pprint(parsed)
        finally:
            f.close()
def build_egg(build_manifest,
              build_node,
              source_root,
              output_dir=None,
              output_file=None):
    meta = PackageMetadata.from_build_manifest(build_manifest)
    egg_info = EggInfo.from_build_manifest(build_manifest, build_node)

    # FIXME: fix egg name
    if output_dir is None:
        if output_file is None:
            egg = egg_filename(os.path.join("dist", meta.fullname))
        else:
            egg = os.path.join("dist", output_file)
    else:
        if output_file is None:
            egg = egg_filename(os.path.join(output_dir, meta.fullname))
        else:
            egg = os.path.join(output_dir, output_file)
    bento.utils.path.ensure_dir(egg)

    egg_scheme = {
        "prefix": source_root.abspath(),
        "eprefix": source_root.abspath(),
        "sitedir": source_root.abspath()
    }

    zid = compat.ZipFile(egg, "w", compat.ZIP_DEFLATED)
    try:
        for filename, cnt in egg_info.iter_meta(build_node):
            zid.writestr(os.path.join("EGG-INFO", filename), cnt)

        for kind, source, target in build_manifest.iter_built_files(
                source_root, egg_scheme):
            if not kind in ["executables"]:
                zid.write(source.abspath(), target.path_from(source_root))
            if kind == "pythonfiles":
                try:
                    bytecode = bcompile(source.abspath())
                except PyCompileError:
                    e = extract_exception()
                    warnings.warn("Error byte-compiling %r" % source.abspath())
                else:
                    zid.writestr("%sc" % target.path_from(source_root),
                                 bcompile(source.abspath()))
    finally:
        zid.close()

    return
Пример #40
0
    def run(self, context):
        if not self.can_run():
            return bento.errors.CommandExecutionFailure("sphinx not available")
        import sphinx.application

        p = context.options_context.parser
        o, a = p.parse_args(context.command_argv)
        if o.output_format != "html":
            raise ValueError("Only html output supported for now")
        else:
            builder = "html"

        if o.source_dir is None:
            source_dir = guess_source_dir()
        else:
            source_dir = o.source_dir
        if source_dir is None:
            raise bento.errors.UsageException("""\
Doc source dir could not be found: please specify the directory where your
sphinx conf.py is located with the --source-dir option""")
        if not op.isabs(source_dir):
            source_dir = op.join(context.top_node.abspath(), source_dir)

        sphinx_build = context.build_node.make_node("sphinx")
        html_build = sphinx_build.make_node(o.output_format)
        doctrees_build = sphinx_build.make_node("doctrees")

        doc_html_build = html_build.abspath()
        doc_doctrees_build = doctrees_build.abspath()

        confoverrides = {}
        status_stream = sys.stdout
        fresh_env = False
        force_all = False

        app = sphinx.application.Sphinx(source_dir,
                                        source_dir,
                                        doc_html_build,
                                        doc_doctrees_build,
                                        builder,
                                        confoverrides,
                                        status_stream,
                                        freshenv=fresh_env)
        try:
            app.build(force_all=force_all)
        except Exception:
            err = extract_exception()
            raise bento.errors.CommandExecutionFailure(
                "error while building doc: %r" % str(err))
Пример #41
0
    def test_error_string(self):
        f = self.f
        f.write("NName: foo")
        f.flush()
        try:
            PackageDescription.from_file(f.name)
            raise AssertionError("Should raise here !")
        except ParseError:
            e = extract_exception()
            self.assertEqual(
                str(e), """\
  File "%s", line 1
NName: foo
^
Syntax error""" % f.name)
Пример #42
0
    def build_extension(self, extension):
        import distutils.errors

        dist_extension = toyext_to_distext(extension)

        bld_cmd = self.ext_bld_cmd
        try:
            bld_cmd.build_extension(dist_extension)

            base, filename = os.path.split(self._extension_filename(dist_extension.name, bld_cmd))
            fullname = os.path.join(bld_cmd.build_lib, base, filename)
            return [relpath(fullname, self._build_base)]
        except distutils.errors.DistutilsError:
            e = extract_exception()
            raise BuildError(str(e))
Пример #43
0
    def build_extension(self, extension):
        import distutils.errors

        dist_extension = toyext_to_distext(extension)

        bld_cmd = self.ext_bld_cmd
        try:
            bld_cmd.build_extension(dist_extension)

            base, filename = os.path.split(self._extension_filename(dist_extension.name, bld_cmd))
            fullname = os.path.join(bld_cmd.build_lib, base, filename)
            return [relpath(fullname, self._build_base)]
        except distutils.errors.DistutilsError:
            e = extract_exception()
            raise BuildError(str(e))
Пример #44
0
    def test_invalid_keyword(self):
        data = """\
Library:
    Name: foo
"""

        try:
            parse(data)
            self.fail("parser did not raise expected ParseError")
        except ParseError:
            e = extract_exception()
            self.assertMultiLineEqual(e.msg, """\
yacc: Syntax error at line 2, Token(NAME_ID, 'Name')
    Name: foo
    ^""")
Пример #45
0
    def run(self, ctx):
        argv = ctx.command_argv
        p = ctx.options_context.parser
        o, a = p.parse_args(argv)
        if o.help:
            p.print_help()
            return
        if len(a) < 1:
            filename = "setup.py"
        else:
            filename = a[0]
        if not op.exists(filename):
            raise ValueError("file %s not found" % filename)

        output = o.output_filename
        if op.exists(output):
            raise UsageException("file %s exists, not overwritten" % output)

        if o.verbose:
            show_output = True
        else:
            show_output = False

        if o.setup_args:
            setup_args = comma_list_split(o.setup_args)
        else:
            setup_args = ["-q", "-n"]

        monkey_patch_mode = o.type

        convert_log = "convert.log"
        log = open(convert_log, "w")
        try:
            try:
                convert(ctx, filename, setup_args, monkey_patch_mode, o.verbose, output, log, show_output)
            except ConvertionError:
                raise
            except Exception:
                e = extract_exception()
                log.write("Error while converting - traceback:\n")
                tb = sys.exc_info()[2]
                traceback.print_tb(tb, file=log)
                msg = "Error while converting %s - you may look at %s for " \
                      "details (Original exception: %s %s)" 
                raise ConvertionError(msg % (filename, convert_log, type(e), str(e)))
        finally:
            log.flush()
            log.close()
Пример #46
0
    def test_invalid_keyword(self):
        data = """\
Library:
    Name: foo
"""

        try:
            parse(data)
            self.fail("parser did not raise expected ParseError")
        except ParseError:
            e = extract_exception()
            self.assertMultiLineEqual(
                e.msg, """\
yacc: Syntax error at line 2, Token(NAME_ID, 'Name')
    Name: foo
    ^""")
Пример #47
0
    def test_multiline_string_count(self):
        data = """\
Description: hey
    how are
        you
        doing ?
NName: foo
"""

        self.lexer.input(data)
        try:
            list(self.lexer)
            self.fail("lexer did not raise expected ParseError")
        except ParseError:
            e = extract_exception()
            self.assertEqual(e.token.lexer.lineno, 5, "Invalid line number: %d" % e.token.lexer.lineno)
Пример #48
0
def build_egg(ipkg, ctx, source_root, output_dir=None, output_file=None):
    meta = PackageMetadata.from_ipkg(ipkg)
    egg_info = EggInfo.from_ipkg(ipkg, ctx.build_node)

    # FIXME: fix egg name
    if output_dir is None:
        if output_file is None:
            egg = egg_filename(os.path.join("dist", meta.fullname))
        else:
            egg = os.path.join("dist", output_file)
    else:
        if output_file is None:
            egg = egg_filename(os.path.join(output_dir, meta.fullname))
        else:
            egg = os.path.join(output_dir, output_file)
    bento.utils.path.ensure_dir(egg)

    zid = compat.ZipFile(egg, "w", compat.ZIP_DEFLATED)
    try:
        ipkg.update_paths({
            "prefix": source_root.abspath(),
            "eprefix": source_root.abspath(),
            "sitedir": source_root.abspath()
        })
        for filename, cnt in egg_info.iter_meta(ctx.build_node):
            zid.writestr(os.path.join("EGG-INFO", filename), cnt)

        file_sections = ipkg.resolve_paths(source_root)
        for kind, source, target in iter_files(file_sections):
            if not kind in ["executables"]:
                zid.write(source.abspath(), target.path_from(source_root))

        pprint("PINK", "Byte-compiling ...")
        for kind, source, target in iter_files(file_sections):
            if kind in ["pythonfiles"]:
                try:
                    bytecode = bcompile(source.abspath())
                except PyCompileError:
                    e = extract_exception()
                    warnings.warn("Error byte-compiling %r" % source.abspath())
                else:
                    zid.writestr("%sc" % target.path_from(source_root),
                                 bcompile(source.abspath()))
    finally:
        zid.close()

    return
Пример #49
0
    def test_multiline_string_count(self):
        data = """\
Description: hey
    how are
        you
        doing ?
NName: foo
"""

        self.lexer.input(data)
        try:
            list(self.lexer)
            self.fail("lexer did not raise expected ParseError")
        except ParseError:
            e = extract_exception()
            self.assertEqual(e.token.lexer.lineno, 5,
                             "Invalid line number: %d" % e.token.lexer.lineno)
Пример #50
0
    def test_invalid_keyword_comment(self):
        """Check comments don't screw up line counting."""
        data = """\
# Some useless
# comments
Library:
    Name: foo
"""

        try:
            parse(data)
            self.fail("parser did not raise expected ParseError")
        except ParseError:
            e = extract_exception()
            self.assertMultiLineEqual(e.msg, """\
yacc: Syntax error at line 4, Token(NAME_ID, 'Name')
    Name: foo
    ^""")
Пример #51
0
def build_egg(ipkg, ctx, source_root, output_dir=None, output_file=None):
    meta = PackageMetadata.from_ipkg(ipkg)
    egg_info = EggInfo.from_ipkg(ipkg, ctx.build_node)

    # FIXME: fix egg name
    if output_dir is None:
        if output_file is None:
            egg = egg_filename(os.path.join("dist", meta.fullname))
        else:
            egg = os.path.join("dist", output_file)
    else:
        if output_file is None:
            egg = egg_filename(os.path.join(output_dir, meta.fullname))
        else:
            egg = os.path.join(output_dir, output_file)
    bento.utils.path.ensure_dir(egg)

    zid = compat.ZipFile(egg, "w", compat.ZIP_DEFLATED)
    try:
        ipkg.update_paths({"prefix": source_root.abspath(),
                           "eprefix": source_root.abspath(),
                           "sitedir": source_root.abspath()})
        for filename, cnt in egg_info.iter_meta(ctx.build_node):
            zid.writestr(os.path.join("EGG-INFO", filename), cnt)

        file_sections = ipkg.resolve_paths(source_root)
        for kind, source, target in iter_files(file_sections):
            if not kind in ["executables"]:
                zid.write(source.abspath(), target.path_from(source_root))

        pprint("PINK", "Byte-compiling ...")
        for kind, source, target in iter_files(file_sections):
            if kind in ["pythonfiles"]:
                try:
                    bytecode = bcompile(source.abspath())
                except PyCompileError:
                    e = extract_exception()
                    warnings.warn("Error byte-compiling %r" % source.abspath())
                else:
                    zid.writestr("%sc" % target.path_from(source_root), bcompile(source.abspath()))
    finally:
        zid.close()

    return
Пример #52
0
def build_extension(bld, extension, env=None):
    builder = bld.builders["pyext"]
    try:
        if env is None:
            env = {"PYEXT_CPPPATH": extension.include_dirs}
        else:
            val = env.get("PYEXT_CPPPATH", [])
            val.extend(extension.include_dirs)
        tasks = builder.extension(extension.name, extension.sources, env)
        if len(tasks) > 1:
            outputs = tasks[0].gen.outputs
        else:
            outputs = []
        return [n.bldpath() for n in outputs]
    except RuntimeError:
        e = extract_exception()
        msg = "Building extension %s failed: %s" % \
              (extension.name, str(e))
        raise CommandExecutionFailure(msg)
Пример #53
0
    def test_invalid_keyword_comment(self):
        """Check comments don't screw up line counting."""
        data = """\
# Some useless
# comments
Library:
    Name: foo
"""

        try:
            parse(data)
            self.fail("parser did not raise expected ParseError")
        except ParseError:
            e = extract_exception()
            self.assertMultiLineEqual(
                e.msg, """\
yacc: Syntax error at line 4, Token(NAME_ID, 'Name')
    Name: foo
    ^""")
Пример #54
0
def build_extension(bld, extension, env=None):
    builder = bld.builders["pyext"]
    try:
        if env is None:
            env = {"PYEXT_CPPPATH": extension.include_dirs}
        else:
            val = env.get("PYEXT_CPPPATH", [])
            val.extend(extension.include_dirs)
        tasks = builder.extension(extension.name, extension.sources, env)
        if len(tasks) > 1:
            outputs = tasks[0].gen.outputs
        else:
            outputs = []
        return [n.bldpath() for n in outputs]
    except RuntimeError:
        e = extract_exception()
        msg = "Building extension %s failed: %s" % \
              (extension.name, str(e))
        raise CommandExecutionFailure(msg)
Пример #55
0
def build_egg(build_manifest, build_node, source_root, output_dir=None, output_file=None):
    meta = PackageMetadata.from_ipkg(build_manifest)
    egg_info = EggInfo.from_ipkg(build_manifest, build_node)

    # FIXME: fix egg name
    if output_dir is None:
        if output_file is None:
            egg = egg_filename(os.path.join("dist", meta.fullname))
        else:
            egg = os.path.join("dist", output_file)
    else:
        if output_file is None:
            egg = egg_filename(os.path.join(output_dir, meta.fullname))
        else:
            egg = os.path.join(output_dir, output_file)
    bento.utils.path.ensure_dir(egg)

    egg_scheme = {"prefix": source_root.abspath(),
                  "eprefix": source_root.abspath(),
                  "sitedir": source_root.abspath()}

    zid = compat.ZipFile(egg, "w", compat.ZIP_DEFLATED)
    try:
        for filename, cnt in egg_info.iter_meta(build_node):
            zid.writestr(os.path.join("EGG-INFO", filename), cnt)

        for kind, source, target in build_manifest.iter_built_files(source_root, egg_scheme):
            if not kind in ["executables"]:
                zid.write(source.abspath(), target.path_from(source_root))
            if kind == "pythonfiles":
                try:
                    bytecode = bcompile(source.abspath())
                except PyCompileError:
                    e = extract_exception()
                    warnings.warn("Error byte-compiling %r" % source.abspath())
                else:
                    zid.writestr("%sc" % target.path_from(source_root), bcompile(source.abspath()))
    finally:
        zid.close()

    return
Пример #56
0
    def _test_impl(self, data, ref):
        res = []
        while True:
            tok = self.lexer.token()
            if not tok:
                break
            res.append(tok.type)

        if is_string(ref):
            ref = split(ref)
        try:
            self.assertEqual(res, ref)
        except AssertionError:
            e = extract_exception()
            cnt = 0
            for i, j in zip(res, ref):
                if not i == j:
                    break
                cnt += 1
            print("Break at index %d" % cnt)
            raise e
Пример #57
0
    def _test_impl(self, data, ref):
        res = []
        while True:
            tok = self.lexer.token()
            if not tok:
                break
            res.append(tok.type)

        if is_string(ref):
            ref = split(ref)
        try:
            self.assertEqual(res, ref)
        except AssertionError:
            e = extract_exception()
            cnt = 0
            for i, j in zip(res, ref):
                if not i == j:
                    break
                cnt += 1
            print(("Break at index %d" % cnt))
            raise e