def _run_subprocess(cls, exe): resolved_path = Path(os.path.abspath(__file__)).parent / "py_info.py" with ensure_file_on_disk(resolved_path) as resolved_path: cmd = [exe, "-s", str(resolved_path)] # prevent sys.prefix from leaking into the child process - see https://bugs.python.org/issue22490 env = os.environ.copy() env.pop("__PYVENV_LAUNCHER__", None) logging.debug("get interpreter info via cmd: %s", LogCmd(cmd)) try: process = Popen( cmd, universal_newlines=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env, ) out, err = process.communicate() code = process.returncode except OSError as os_error: out, err, code = "", os_error.strerror, os_error.errno result, failure = None, None if code == 0: result = cls._from_json(out) result.executable = exe # keep original executable as this may contain initialization code else: msg = "failed to query {} with code {}{}{}".format( exe, code, " out: {!r}".format(out) if out else "", " err: {!r}".format(err) if err else "" ) failure = RuntimeError(msg) return failure, result
def get_env_debug_info(env_exe, debug_script, app_data): env = os.environ.copy() env.pop(str("PYTHONPATH"), None) with ensure_file_on_disk(debug_script, app_data) as debug_script: cmd = [str(env_exe), str(debug_script)] if WIN_CPYTHON_2: cmd = [ensure_text(i) for i in cmd] logging.debug(str("debug via %r"), LogCmd(cmd)) code, out, err = run_cmd(cmd) # noinspection PyBroadException try: if code != 0: result = literal_eval(out) else: result = json.loads(out) if err: result["err"] = err except Exception as exception: return { "out": out, "err": err, "returncode": code, "exception": repr(exception) } if "sys" in result and "path" in result["sys"]: del result["sys"]["path"][0] return result
def env_patch_text(self): """Patch the distutils package to not be derailed by its configuration files""" with ensure_file_on_disk( Path(__file__).parent / "_virtualenv.py", self.app_data) as resolved_path: text = resolved_path.read_text() return text.replace( '"__SCRIPT_DIR__"', repr(os.path.relpath(str(self.script_dir), str(self.purelib))))
def pip_wheel_env_run(version): env = os.environ.copy() env.update( { six.ensure_str(k): str(v) # python 2 requires these to be string only (non-unicode) for k, v in {"PIP_USE_WHEEL": "1", "PIP_USER": "******", "PIP_NO_INPUT": "1"}.items() } ) with ensure_file_on_disk(get_bundled_wheel("pip", version)) as pip_wheel_path: # put the bundled wheel onto the path, and use it to do the bootstrap operation env[str("PYTHONPATH")] = str(pip_wheel_path) yield env
def patch_distutils_via_pth(self): """Patch the distutils package to not be derailed by its configuration files""" patch_file = Path(__file__).parent / "_distutils_patch_virtualenv.py" with ensure_file_on_disk(patch_file, self.app_data) as resolved_path: text = resolved_path.read_text() text = text.replace('"__SCRIPT_DIR__"', repr(os.path.relpath(str(self.script_dir), str(self.purelib)))) patch_path = self.purelib / "_distutils_patch_virtualenv.py" logging.debug("add distutils patch file %s", patch_path) patch_path.write_text(text) pth = self.purelib / "_distutils_patch_virtualenv.pth" logging.debug("add distutils patch file %s", pth) pth.write_text("import _distutils_patch_virtualenv")
def patch_distutils_via_pth(self): """Patch the distutils package to not be derailed by its configuration files""" if self.interpreter.version_info.major == 3: return # TODO: remove this, for her to bypass: https://github.com/pypa/pip/issues/7778 patch_file = Path(__file__).parent / "_distutils_patch_virtualenv.py" with ensure_file_on_disk(patch_file, self.app_data) as resolved_path: text = resolved_path.read_text() text = text.replace('"__SCRIPT_DIR__"', repr(os.path.relpath(str(self.script_dir), str(self.purelib)))) patch_path = self.purelib / "_distutils_patch_virtualenv.py" logging.debug("add distutils patch file %s", patch_path) patch_path.write_text(text) pth = self.purelib / "_distutils_patch_virtualenv.pth" logging.debug("add distutils patch file %s", pth) pth.write_text("import _distutils_patch_virtualenv")
def get_pip_install_cmd(self, exe, version): cmd = [ str(exe), "-m", "pip", "-q", "install", "--only-binary", ":all:" ] if not self.download: cmd.append("--no-index") for key, ver in self.package_version().items(): cmd.append("{}{}".format( key, "=={}".format(ver) if ver is not None else "")) with ExitStack() as stack: folders = set() for context in (ensure_file_on_disk(get_bundled_wheel(p, version)) for p in self.packages): folders.add(stack.enter_context(context).parent) for folder in folders: cmd.extend(["--find-links", str(folder)]) cmd.extend(self.extra_search_dir) yield cmd
def _run_subprocess(cls, exe): resolved_path = Path(__file__).parent.absolute().absolute() / "py_info.py" with ensure_file_on_disk(resolved_path) as resolved_path: cmd = [exe, "-s", str(resolved_path)] logging.debug("get interpreter info via cmd: %s", LogCmd(cmd)) try: process = Popen( cmd, universal_newlines=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE ) out, err = process.communicate() code = process.returncode except OSError as os_error: out, err, code = "", os_error.strerror, os_error.errno result, failure = None, None if code == 0: result = cls._from_json(out) result.executable = exe # keep original executable as this may contain initialization code else: msg = "failed to query {} with code {}{}{}".format( exe, code, " out: {!r}".format(out) if out else "", " err: {!r}".format(err) if err else "" ) failure = RuntimeError(msg) return failure, result