-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_pyproject.py
59 lines (46 loc) · 1.62 KB
/
update_pyproject.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
import os
import toml
import argparse
import subprocess
from dotenv import dotenv_values
# Make sure to use absolute path when calling the script from different places
ABS_PATH = os.path.dirname(__file__)
CONFIG_FILE = "pyproject.toml"
# Load .env config
config = {
**dotenv_values(".env.shared"),
**os.environ
}
# Create the parser to use the script with ou without a flag
parser = argparse.ArgumentParser(description="Update pyproject.toml")
parser.add_argument(
"-c",
"--config",
help="pyproject.toml config file",
required=False,
default=os.path.join(ABS_PATH, CONFIG_FILE),
)
args = parser.parse_args()
def update_pyproject_dependencies(path: str) -> None:
"""
Reads the dependencies from a `pyproject.toml` file, updates them
with the output of `pip freeze`, and writes the updated data back to the file.
:param path: Path to the `pyproject.toml` file.
:return: None
"""
with open(path, "r") as file:
pyproject_data = toml.load(file)
dependencies = pyproject_data["project"]["dependencies"]
# Get updated dependencies from `pip freeze`
frozen_dependencies = subprocess.check_output(["pip", "freeze"]).decode("utf-8")
# Update dependencies in pyproject.toml
dependencies.clear()
for line in frozen_dependencies.splitlines():
package, version = line.split("==")
print(f"{package}=={version}")
dependencies.append(f"{package}=={version}")
# Write updated data back to the file
with open(path, "w") as file:
toml.dump(pyproject_data, file)
if __name__ == "__main__":
update_pyproject_dependencies(args.config)