forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
microvenv
support for non-windows platforms (#20985)
This PR contains: 1. `microvenv` fallback if `venv` is not available (implemented in python with tests) 2. Updates to telemetry to include microvenv. Closes #20905
- Loading branch information
1 parent
f59b23e
commit e36b27c
Showing
11 changed files
with
570 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import argparse | ||
import os | ||
import pathlib | ||
import subprocess | ||
import sys | ||
import urllib.request as url_lib | ||
from typing import Optional, Sequence | ||
|
||
VENV_NAME = ".venv" | ||
LIB_ROOT = pathlib.Path(__file__).parent / "lib" / "python" | ||
CWD = pathlib.Path.cwd() | ||
|
||
|
||
class MicroVenvError(Exception): | ||
pass | ||
|
||
|
||
def run_process(args: Sequence[str], error_message: str) -> None: | ||
try: | ||
print("Running: " + " ".join(args)) | ||
subprocess.run(args, cwd=os.getcwd(), check=True) | ||
except subprocess.CalledProcessError: | ||
raise MicroVenvError(error_message) | ||
|
||
|
||
def parse_args(argv: Sequence[str]) -> argparse.Namespace: | ||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument( | ||
"--install-pip", | ||
action="store_true", | ||
default=False, | ||
help="Install pip into the virtual environment.", | ||
) | ||
|
||
parser.add_argument( | ||
"--name", | ||
default=VENV_NAME, | ||
type=str, | ||
help="Name of the virtual environment.", | ||
metavar="NAME", | ||
action="store", | ||
) | ||
return parser.parse_args(argv) | ||
|
||
|
||
def create_microvenv(name: str): | ||
run_process( | ||
[sys.executable, os.fspath(LIB_ROOT / "microvenv.py"), name], | ||
"CREATE_MICROVENV.MICROVENV_FAILED_CREATION", | ||
) | ||
|
||
|
||
def download_pip_pyz(name: str): | ||
url = "https://bootstrap.pypa.io/pip/pip.pyz" | ||
print("CREATE_MICROVENV.DOWNLOADING_PIP") | ||
|
||
try: | ||
with url_lib.urlopen(url) as response: | ||
pip_pyz_path = os.fspath(CWD / name / "pip.pyz") | ||
with open(pip_pyz_path, "wb") as out_file: | ||
data = response.read() | ||
out_file.write(data) | ||
out_file.flush() | ||
except Exception: | ||
raise MicroVenvError("CREATE_MICROVENV.DOWNLOAD_PIP_FAILED") | ||
|
||
|
||
def install_pip(name: str): | ||
pip_pyz_path = os.fspath(CWD / name / "pip.pyz") | ||
executable = os.fspath(CWD / name / "bin" / "python") | ||
print("CREATE_MICROVENV.INSTALLING_PIP") | ||
run_process( | ||
[executable, pip_pyz_path, "install", "pip"], | ||
"CREATE_MICROVENV.INSTALL_PIP_FAILED", | ||
) | ||
|
||
|
||
def main(argv: Optional[Sequence[str]] = None) -> None: | ||
if argv is None: | ||
argv = [] | ||
args = parse_args(argv) | ||
|
||
print("CREATE_MICROVENV.CREATING_MICROVENV") | ||
create_microvenv(args.name) | ||
print("CREATE_MICROVENV.CREATED_MICROVENV") | ||
|
||
if args.install_pip: | ||
download_pip_pyz(args.name) | ||
install_pip(args.name) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import importlib | ||
import os | ||
import sys | ||
|
||
import create_microvenv | ||
import pytest | ||
|
||
|
||
def test_create_microvenv(): | ||
importlib.reload(create_microvenv) | ||
run_process_called = False | ||
|
||
def run_process(args, error_message): | ||
nonlocal run_process_called | ||
run_process_called = True | ||
assert args == [ | ||
sys.executable, | ||
os.fspath(create_microvenv.LIB_ROOT / "microvenv.py"), | ||
create_microvenv.VENV_NAME, | ||
] | ||
assert error_message == "CREATE_MICROVENV.MICROVENV_FAILED_CREATION" | ||
|
||
create_microvenv.run_process = run_process | ||
|
||
create_microvenv.main() | ||
assert run_process_called == True | ||
|
||
|
||
def test_create_microvenv_with_pip(): | ||
importlib.reload(create_microvenv) | ||
|
||
download_pip_pyz_called = False | ||
|
||
def download_pip_pyz(name): | ||
nonlocal download_pip_pyz_called | ||
download_pip_pyz_called = True | ||
assert name == create_microvenv.VENV_NAME | ||
|
||
create_microvenv.download_pip_pyz = download_pip_pyz | ||
|
||
run_process_called = False | ||
|
||
def run_process(args, error_message): | ||
if "install" in args and "pip" in args: | ||
nonlocal run_process_called | ||
run_process_called = True | ||
pip_pyz_path = os.fspath( | ||
create_microvenv.CWD / create_microvenv.VENV_NAME / "pip.pyz" | ||
) | ||
executable = os.fspath( | ||
create_microvenv.CWD / create_microvenv.VENV_NAME / "bin" / "python" | ||
) | ||
assert args == [executable, pip_pyz_path, "install", "pip"] | ||
assert error_message == "CREATE_MICROVENV.INSTALL_PIP_FAILED" | ||
|
||
create_microvenv.run_process = run_process | ||
create_microvenv.main(["--install-pip"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ | |
|
||
# Unittest test adapter | ||
typing-extensions==4.5.0 | ||
|
||
# Fallback env creator for debian | ||
microvenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.