-
Notifications
You must be signed in to change notification settings - Fork 0
/
freeze.py
39 lines (35 loc) · 1.08 KB
/
freeze.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
"""
Pin the dependencies given in pyproject.toml.
"""
from venv import EnvBuilder
from shutil import rmtree
from subprocess import check_call
from tomllib import load
from os import mkdir, path, remove
BUILD = "build"
REQUIREMENTS = "build/requirements.txt"
VENV = "build/.venv"
try:
try:
mkdir(BUILD, mode=0o750)
except FileExistsError:
pass
try:
mkdir(VENV, mode=0o750)
except FileExistsError:
pass
EnvBuilder().create(VENV)
with open("pyproject.toml", "rb") as project:
pyproject = load(project)
with open(REQUIREMENTS, "w") as requirements:
requirements.write("\n".join(pyproject["project"]["dependencies"]))
check_call([VENV + "/bin/python", "-m", "ensurepip"])
check_call([VENV + "/bin/pip3", "install", "-r", REQUIREMENTS])
with open("requirements.txt", "w") as requirements:
check_call([VENV + "/bin/pip3", "freeze", "-r", REQUIREMENTS],
stdout=requirements)
finally:
if path.isdir(VENV):
rmtree(VENV)
if path.exists(REQUIREMENTS):
remove(REQUIREMENTS)