-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathvirtual_environment.py
executable file
·90 lines (64 loc) · 2.37 KB
/
virtual_environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
TODO(T132414938) Add a module-level docstring
"""
from __future__ import annotations
import argparse
import logging
import os
import subprocess
import sys
import venv
from pathlib import Path
from typing import NoReturn
LOG = logging.getLogger(__name__)
PATH = Path(os.path.expanduser("~/.venvs/pyre"))
ACTIVATE = PATH / "bin" / "activate"
def _setup(arguments: argparse._Namespace) -> NoReturn:
LOG.info(f"Attempting to setup virtual environment in `{PATH}`...")
venv.create(str(PATH), with_pip=True)
LOG.info("Installing requirements...")
requirements = Path(__file__).parent.parent / "requirements.txt"
_run_in_environment(f"pip install -r {requirements}")
def _enter(arguments: argparse._Namespace) -> None:
print(f"source {ACTIVATE}")
def _leave(arguments: argparse._Namespace) -> None:
print("deactivate")
def _run_in_environment(command: str) -> None:
subprocess.check_call(["sh", "-c", f"source {ACTIVATE}; {command}"])
def _run(arguments: argparse._Namespace) -> None:
_run_in_environment(arguments.to_run)
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(message)s", level=logging.DEBUG
)
parser = argparse.ArgumentParser(
description="Run inside the Pyre virtual environment"
)
commands = parser.add_subparsers()
setup = commands.add_parser("setup")
setup.set_defaults(command=_setup)
enter = commands.add_parser(
"enter",
help="Enter the virtual environment."
"Run with `eval $(./scripts/virtual_environment.py enter)`",
)
enter.set_defaults(command=_enter)
leave = commands.add_parser(
"leave",
help="Leave the virtual environment."
"Run with `eval $(./scripts/virtual_environment.py leave)`",
)
leave.set_defaults(command=_leave)
run = commands.add_parser("run", help="Run a command in the virtual environment")
run.add_argument("to_run")
run.set_defaults(command=_run)
arguments = parser.parse_args()
if not hasattr(arguments, "command"):
LOG.warning("No command provided, see `--help` for more information.")
sys.exit(1)
arguments.command(arguments)