Skip to content

Commit

Permalink
Modity update_optional_dependencies to update requirements.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
arjxn-py committed Jun 2, 2024
1 parent 855025d commit f042769
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions scripts/update_optional_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
from functools import reduce
from pathlib import Path

import tomlkit
import tomlkit.items
from packaging.requirements import Requirement

import ragna
from ragna.core import Assistant, SourceStorage

HERE = Path(__file__).parent
PYPROJECT_TOML = HERE / ".." / "pyproject.toml"
REQUIREMENTS_TXT = HERE / ".." / "requirements.txt"


def main():
optional_dependencies = make_optional_dependencies(
extract_builtin_document_handler_requirements(),
extract_builtin_component_requirements(),
)
update_pyproject_toml(optional_dependencies)
update_requirements_txt(optional_dependencies)


def make_optional_dependencies(*optional_requirements):
Expand Down Expand Up @@ -57,18 +55,27 @@ def extract_builtin_component_requirements():
return dict(requirements)


def update_pyproject_toml(optional_dependencies):
with open(PYPROJECT_TOML) as file:
document = tomlkit.load(file)
def update_requirements_txt(optional_dependencies):
existing_dependencies = set()

document["project"]["optional-dependencies"]["all"] = tomlkit.items.Array(
list(map(tomlkit.items.String.from_raw, optional_dependencies)),
trivia=tomlkit.items.Trivia(),
multiline=True,
)
# Read existing dependencies from requirements.txt
if REQUIREMENTS_TXT.exists():
with open(REQUIREMENTS_TXT, "r") as file:
existing_dependencies = set(line.strip() for line in file)

new_dependencies = []

# Check if each optional dependency already exists
for dependency in optional_dependencies:
if dependency not in existing_dependencies:
new_dependencies.append(dependency)
existing_dependencies.add(dependency)

with open(PYPROJECT_TOML, "w") as file:
tomlkit.dump(document, file)
# Append new dependencies to requirements.txt
if new_dependencies:
with open(REQUIREMENTS_TXT, "a") as file:
file.write("\n".join(new_dependencies))
file.write("\n")


def append_version_specifiers(version_specifiers, obj):
Expand Down

0 comments on commit f042769

Please sign in to comment.