forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add microvenv
support for non-windows platforms
#20985
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed, sorry. Looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also does this:
which prints out the full command we run: