forked from aws/amazon-mwaa-docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_venvs.py
108 lines (83 loc) · 3.31 KB
/
create_venvs.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Create the virtual environments required to develop with this package.
This module should be executed after cloning the repository to create the following
virtual environments:
- One virtual environment at the root package.
- One per each Docker image
Those environments are used for many tasks, most importantly allow the IDE to use the
right Python environment for the different folders in this repository. This is necessary
since the Python packages required to develop the different Airflow versions are
different from the packages that we need for the various scripts in this repository.
"""
import argparse
import os
import shutil
import subprocess
import sys
import venv
from pathlib import Path
def verify_python_version():
"""Check if the current Python version is at least 3.9."""
major, minor, *_ = sys.version_info
if major != 3 or minor < 11:
print("Python 3.11 or higher is required.")
sys.exit(1)
def create_venv(path: Path, recreate: bool = False):
"""
Create a venv in the given directory and optionally recreate it if it already exists.
:param path: The path to create the venv in.
:param recreate: Whether to recreate the venv if it already exists.
"""
venv_path = path / ".venv"
print(f">>> Creating a virtual environment under the path {venv_path}...")
if recreate and venv_path.exists():
print(f"> Deleting existing virtualenv in {venv_path}")
shutil.rmtree(venv_path) # Delete the existing environment
if not venv_path.exists():
print(f"> Creating virtualenv in directory: {venv_path}")
venv.create(venv_path, with_pip=True)
else:
print(f"> Virtualenv already exists in {venv_path}")
print("> Upgrade pip...")
pip_install(venv_path, "-U", "pip")
print("")
requirements_path = str(path / "requirements.txt")
print(f"> Install dependencies from {requirements_path}...")
pip_install(venv_path, "-r", requirements_path)
print("")
print("> Install/Upgrade development tools: pydocstyle, pyright, ruff...")
pip_install(venv_path, "-U", "pydocstyle", "pyright", "ruff")
print("")
print(f">>> Finished creating a virtual environment under the path {venv_path}.")
print("")
print("")
def pip_install(venv_dir: Path, *args: str):
"""
Install dependencies from requirements.txt if it exists.
:param venv_dir: The path to the venv directory.
:param venv_dir: The path to the requirements.txt file.
"""
subprocess.run(
[os.path.join(venv_dir, "bin", "python"), "-m", "pip", "install", *args],
check=True,
)
def main():
"""Start execution of the script."""
# Create the parser
parser = argparse.ArgumentParser(description="Create virtual environments.")
# Add the 'recreate' optional argument
parser.add_argument(
"--recreate", action="store_true", help="Recreate the venv if it exists"
)
# Parse the arguments
args = parser.parse_args()
verify_python_version()
project_dirs = [
Path("."),
*Path("./images").glob("airflow/*"),
] # Include main project dir and each image dir
for dir_path in project_dirs:
if dir_path.is_dir() and (dir_path / "requirements.txt").exists():
create_venv(dir_path, recreate=args.recreate)
if __name__ == "__main__":
main()