feat: add uv to tools

This commit is contained in:
Eduardo Chiarotti
2024-10-10 12:57:17 -03:00
parent 46bc0a89cf
commit 12a7cee5dd

View File

@@ -1,5 +1,6 @@
import base64 import base64
import os import os
import platform
import subprocess import subprocess
import tempfile import tempfile
from pathlib import Path from pathlib import Path
@@ -25,6 +26,8 @@ class ToolCommand(BaseCommand, PlusAPIMixin):
A class to handle tool repository related operations for CrewAI projects. A class to handle tool repository related operations for CrewAI projects.
""" """
BASE_URL = "https://app.crewai.com/pypi/"
def __init__(self): def __init__(self):
BaseCommand.__init__(self) BaseCommand.__init__(self)
PlusAPIMixin.__init__(self, telemetry=self._telemetry) PlusAPIMixin.__init__(self, telemetry=self._telemetry)
@@ -150,57 +153,39 @@ class ToolCommand(BaseCommand, PlusAPIMixin):
raise SystemExit raise SystemExit
login_response_json = login_response.json() login_response_json = login_response.json()
for repository in login_response_json["repositories"]: self._set_netrc_credentials(login_response_json["credentials"])
self._add_repository_to_uv(repository, login_response_json["credential"])
console.print( console.print(
"Succesfully authenticated to the tool repository.", style="bold green" "Successfully authenticated to the tool repository.", style="bold green"
) )
def _add_repository_to_uv(self, repository, credentials): def _set_netrc_credentials(self, credentials):
repository_handle = f"crewai-{repository['handle']}" # Create .netrc or _netrc file
netrc_filename = "_netrc" if platform.system() == "Windows" else ".netrc"
netrc_path = Path.home() / netrc_filename
add_repository_command = [ netrc_content = f"""machine app.crewai.com
"uv", login {credentials['username']}
"add", password {credentials['password']}
repository_handle, """
repository["url"],
]
add_repository_result = subprocess.run(
add_repository_command, text=True, check=True
)
if add_repository_result.stderr: with open(netrc_path, "a") as netrc_file:
click.echo(add_repository_result.stderr, err=True) netrc_file.write(netrc_content)
raise SystemExit
add_repository_credentials_command = [ # Set appropriate permissions for Unix-like systems
"uv", if platform.system() != "Windows":
"config", os.chmod(netrc_path, 0o600)
f"http-basic.{repository_handle}", console.print(f"Added credentials to {netrc_filename}", style="bold green")
credentials["username"],
credentials["password"],
]
add_repository_credentials_result = subprocess.run(
add_repository_credentials_command,
capture_output=False,
text=True,
check=True,
)
if add_repository_credentials_result.stderr:
click.echo(add_repository_credentials_result.stderr, err=True)
raise SystemExit
def _add_package(self, tool_details): def _add_package(self, tool_details):
tool_handle = tool_details["handle"] tool_handle = tool_details["handle"]
repository_handle = tool_details["repository"]["handle"] repository_handle = tool_details["repository"]["handle"]
pypi_index_handle = f"crewai-{repository_handle}"
add_package_command = [ add_package_command = [
"uv", "uv",
"add", "add",
pypi_index_handle, "--extra-index-url",
self.BASE_URL + repository_handle,
tool_handle, tool_handle,
] ]
add_package_result = subprocess.run( add_package_result = subprocess.run(