Esempio n. 1
0
def load_nox_module(global_config: Namespace) -> Union[types.ModuleType, int]:
    """Load the user's noxfile and return the module object for it.

    .. note::

        This task has two side effects; it makes ``global_config.noxfile``
        an absolute path, and changes the working directory of the process.

    Args:
        global_config (.nox.main.GlobalConfig): The global config.

    Returns:
        module: The module designated by the Noxfile path.
    """
    try:
        # Save the absolute path to the Noxfile.
        # This will inoculate it if nox changes paths because of an implicit
        # or explicit chdir (like the one below).
        global_config.noxfile = os.path.realpath(
            # Be sure to expand variables
            os.path.expandvars(global_config.noxfile))

        # Move to the path where the Noxfile is.
        # This will ensure that the Noxfile's path is on sys.path, and that
        # import-time path resolutions work the way the Noxfile author would
        # guess.
        os.chdir(os.path.realpath(os.path.dirname(global_config.noxfile)))
        return importlib.machinery.SourceFileLoader(
            "user_nox_module",
            global_config.noxfile).load_module()  # type: ignore

    except (IOError, OSError):
        logger.exception("Failed to load Noxfile {}".format(
            global_config.noxfile))
        return 2
Esempio n. 2
0
 def _run_func(self, func, args, kwargs):
     """Legacy support for running a function through :func`run`."""
     self.log("{}(args={!r}, kwargs={!r})".format(func, args, kwargs))
     try:
         return func(*args, **kwargs)
     except Exception as e:
         logger.exception("Function {!r} raised {!r}.".format(func, e))
         raise nox.command.CommandFailed()
Esempio n. 3
0
 def _run_func(self, func, args, kwargs):
     """Legacy support for running a function through :func`run`."""
     self.log("{}(args={!r}, kwargs={!r})".format(func, args, kwargs))
     try:
         return func(*args, **kwargs)
     except Exception as e:
         logger.exception("Function {!r} raised {!r}.".format(func, e))
         raise nox.command.CommandFailed()
Esempio n. 4
0
 def _run_func(self, func: Callable[..., Any], args: Iterable[Any],
               kwargs: Mapping[str, Any]) -> Any:
     """Legacy support for running a function through :func`run`."""
     self.log(f"{func}(args={args!r}, kwargs={kwargs!r})")
     try:
         return func(*args, **kwargs)
     except Exception as e:
         logger.exception(f"Function {func!r} raised {e!r}.")
         raise nox.command.CommandFailed()
Esempio n. 5
0
    def run(self):
        func_name = self.func.__name__
        logger.info('{}()'.format(func_name))

        try:
            self.func()
            return True
        except Exception as e:
            logger.exception('Function {} raised {}.'.format(func_name, e))

            raise CommandFailed(e)
Esempio n. 6
0
    def run(self):
        logger.info(str(self))

        try:
            self.func(*self.args, **self.kwargs)
            return True
        except Exception as e:
            logger.exception('Function {} raised {}.'.format(
                self._func_name, e))

            raise CommandFailed(e)
Esempio n. 7
0
def load_nox_module(global_config: Namespace) -> types.ModuleType | int:
    """Load the user's Noxfile and return the module object for it.

    .. note::

        This task has two side effects; it makes ``global_config.noxfile``
        an absolute path, and changes the working directory of the process.

    Args:
        global_config (.nox.main.GlobalConfig): The global config.

    Returns:
        module: The module designated by the Noxfile path.
    """
    try:
        # Save the absolute path to the Noxfile.
        # This will inoculate it if Nox changes paths because of an implicit
        # or explicit chdir (like the one below).
        global_config.noxfile = os.path.realpath(
            # Be sure to expand variables
            os.path.expandvars(global_config.noxfile))
        noxfile_parent_dir = os.path.realpath(
            os.path.dirname(global_config.noxfile))

        # Check ``nox.needs_version`` by parsing the AST.
        check_nox_version(global_config.noxfile)

        # Move to the path where the Noxfile is.
        # This will ensure that the Noxfile's path is on sys.path, and that
        # import-time path resolutions work the way the Noxfile author would
        # guess. The original working directory (the directory that Nox was
        # invoked from) gets stored by the .invoke_from "option" in _options.
        os.chdir(noxfile_parent_dir)

    except (VersionCheckFailed, InvalidVersionSpecifier) as error:
        logger.error(str(error))
        return 2
    except FileNotFoundError:
        logger.error(
            f"Failed to load Noxfile {global_config.noxfile}, no such file exists."
        )
        return 2
    except OSError:
        logger.exception(f"Failed to load Noxfile {global_config.noxfile}")
        return 2
    else:
        return _load_and_exec_nox_module(global_config)
Esempio n. 8
0
    def execute(self) -> Result:
        logger.warning(f"Running session {self.friendly_name}")

        try:
            # By default, Nox should quietly change to the directory where
            # the noxfile.py file is located.
            cwd = py.path.local(
                os.path.realpath(os.path.dirname(
                    self.global_config.noxfile))).as_cwd()

            with cwd:
                self._create_venv()
                session = Session(self)
                self.func(session)

            # Nothing went wrong; return a success.
            return Result(self, Status.SUCCESS)

        except nox.virtualenv.InterpreterNotFound as exc:
            if self.global_config.error_on_missing_interpreters:
                return Result(self, Status.FAILED, reason=str(exc))
            else:
                logger.warning(
                    "Missing interpreters will error by default on CI systems."
                )
                return Result(self, Status.SKIPPED, reason=str(exc))

        except _SessionQuit as exc:
            return Result(self, Status.ABORTED, reason=str(exc))

        except _SessionSkip as exc:
            return Result(self, Status.SKIPPED, reason=str(exc))

        except nox.command.CommandFailed:
            return Result(self, Status.FAILED)

        except KeyboardInterrupt:
            logger.error(f"Session {self.friendly_name} interrupted.")
            raise

        except Exception as exc:
            logger.exception(
                f"Session {self.friendly_name} raised exception {exc!r}")
            return Result(self, Status.FAILED)
Esempio n. 9
0
    def execute(self):
        logger.warning("Running session {}".format(self.friendly_name))

        try:
            # By default, nox should quietly change to the directory where
            # the noxfile.py file is located.
            cwd = py.path.local(
                os.path.realpath(os.path.dirname(self.global_config.noxfile))
            ).as_cwd()

            with cwd:
                self._create_venv()
                session = Session(self)
                self.func(session)

            # Nothing went wrong; return a success.
            return Result(self, Status.SUCCESS)

        except nox.virtualenv.InterpreterNotFound as exc:
            if self.global_config.error_on_missing_interpreters:
                return Result(self, Status.FAILED, reason=str(exc))
            else:
                return Result(self, Status.SKIPPED, reason=str(exc))

        except _SessionQuit as exc:
            return Result(self, Status.ABORTED, reason=str(exc))

        except _SessionSkip as exc:
            return Result(self, Status.SKIPPED, reason=str(exc))

        except nox.command.CommandFailed:
            return Result(self, Status.FAILED)

        except KeyboardInterrupt:
            logger.error("Session {} interrupted.".format(self.friendly_name))
            raise

        except Exception as exc:
            logger.exception(
                "Session {} raised exception {!r}".format(self.friendly_name, exc)
            )
            return Result(self, Status.FAILED)
Esempio n. 10
0
    def execute(self):
        session_friendly_name = self.signature or self.name
        logger.warning("Running session {}".format(session_friendly_name))

        try:
            # By default, nox should quietly change to the directory where
            # the noxfile.py file is located.
            cwd = py.path.local(
                os.path.realpath(os.path.dirname(
                    self.global_config.noxfile))).as_cwd()

            with cwd:
                self._create_venv()
                session = Session(self)
                self.func(session)

            # Nothing went wrong; return a success.
            return Result(self, Status.SUCCESS)

        except _SessionQuit:
            return Result(self, Status.ABORTED)

        except _SessionSkip:
            return Result(self, Status.SKIPPED)

        except nox.command.CommandFailed:
            return Result(self, Status.FAILED)

        except KeyboardInterrupt:
            logger.error("Session {} interrupted.".format(self))
            raise

        except Exception as exc:
            logger.exception("Session {} raised exception {!r}".format(
                self, exc))
            return Result(self, Status.FAILED)