Exemplo n.º 1
0
 def handle(self, project: Project, options: argparse.Namespace) -> None:
     if options.env:
         venv = next(
             (venv
              for key, venv in iter_venvs(project) if key == options.env),
             None)
         if not venv:
             project.core.ui.echo(
                 termui.yellow(
                     f"No virtualenv with key {options.env} is found"),
                 err=True,
             )
             raise SystemExit(1)
     else:
         # Use what is saved in .pdm.toml
         interpreter = project.python_executable
         if is_venv_python(interpreter):
             venv = Path(interpreter).parent.parent
         else:
             project.core.ui.echo(
                 termui.yellow(
                     f"Can't activate a non-venv Python{interpreter}, "
                     "you can specify one with pdm venv activate <env_name>"
                 ))
             raise SystemExit(1)
     project.core.ui.echo(self.get_activate_command(venv))
Exemplo n.º 2
0
 def environment(self) -> Environment:
     if self.is_global:
         env = GlobalEnvironment(self)
         # Rewrite global project's python requires to be
         # compatible with the exact version
         env.python_requires = PySpecSet(
             "==" + get_python_version(self.python_executable, True)[0])
         return env
     if self.config["use_venv"] and is_venv_python(self.python_executable):
         # Only recognize venv created by python -m venv and virtualenv>20
         return GlobalEnvironment(self)
     return Environment(self)
Exemplo n.º 3
0
 def get_environment(self) -> Environment:
     """Get the environment selected by this project"""
     if self.is_global:
         env = GlobalEnvironment(self)
         # Rewrite global project's python requires to be
         # compatible with the exact version
         env.python_requires = PySpecSet(f"=={self.python.version}")
         return env
     if self.config["use_venv"] and is_venv_python(self.python.executable):
         # Only recognize venv created by python -m venv and virtualenv>20
         return GlobalEnvironment(self)
     return Environment(self)
Exemplo n.º 4
0
 def get_paths(self) -> dict[str, str]:
     is_venv = is_venv_python(self.interpreter.executable)
     paths = get_sys_config_paths(
         str(self.interpreter.executable),
         kind="user"
         if not is_venv and self.project.global_config["global_project.user_site"]
         else "default",
     )
     if is_venv:
         python_xy = f"python{self.interpreter.identifier}"
         paths["include"] = os.path.join(paths["data"], "include", "site", python_xy)
     paths["prefix"] = paths["data"]
     paths["headers"] = paths["include"]
     return paths
Exemplo n.º 5
0
 def get_environment(self) -> Environment:
     if self.is_global:
         env = GlobalEnvironment(self)
         # Rewrite global project's python requires to be
         # compatible with the exact version
         env.python_requires = PySpecSet(f"=={self.python.version}")
         return env
     if self.config["python.use_venv"]:
         if self.project_config.get("python.path") and not os.getenv(
                 "PDM_IGNORE_SAVED_PYTHON"):
             return (GlobalEnvironment(self) if is_venv_python(
                 self.python.executable) else Environment(self))
         if os.getenv("VIRTUAL_ENV"):
             venv = os.getenv("VIRTUAL_ENV")
             self.core.ui.echo(
                 f"Detected inside an active virtualenv {termui.green(venv)}, "
                 "reuse it.")
             # Temporary usage, do not save in .pdm.toml
             self._python = PythonInfo.from_path(get_venv_python(
                 Path(venv)))
             return GlobalEnvironment(self)
         existing_venv = next((venv for _, venv in iter_venvs(self)), None)
         if existing_venv:
             self.core.ui.echo(
                 f"Virtualenv {termui.green(str(existing_venv))} is reused.",
                 err=True,
             )
             path = existing_venv
         else:
             # Create a virtualenv using the selected Python interpreter
             self.core.ui.echo(
                 "python.use_venv is on, creating a virtualenv for this project...",
                 fg="yellow",
                 err=True,
             )
             backend: str = self.config["venv.backend"]
             venv_backend = BACKENDS[backend](self, None)
             path = venv_backend.create(None, (), False,
                                        self.config["venv.in_project"])
             self.core.ui.echo(f"Virtualenv {path} is created successfully")
         self.python = PythonInfo.from_path(get_venv_python(path))
         return GlobalEnvironment(self)
     else:
         return Environment(self)