-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fae5f75
commit fcfbcfd
Showing
5 changed files
with
515 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
from enum import Enum | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class AgentConfig(BaseModel): | ||
id: str | ||
secret: str | ||
|
||
|
||
class AgentPythonPackageManager(str, Enum): | ||
poetry = "poetry" | ||
pip = "pip" | ||
|
||
|
||
class AgentUsageType(str, Enum): | ||
fastapi = "fastapi" | ||
python = "python" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import re | ||
import tomllib | ||
|
||
import requests | ||
from poetry.core.constraints.version import Version | ||
from poetry.core.constraints.version.parser import parse_constraint | ||
|
||
from libertai_client.interfaces.agent import AgentPythonPackageManager | ||
from libertai_client.utils.system import get_full_path | ||
|
||
|
||
def __fetch_real_python_versions() -> list[str]: | ||
response = requests.get( | ||
"https://api.github.com/repos/python/cpython/tags?per_page=100" | ||
) | ||
if response.status_code == 200: | ||
releases = response.json() | ||
versions = [str(release["name"]).removeprefix("v") for release in releases] | ||
exact_versions = [v for v in versions if re.match(r"^\d+\.\d+\.\d+$", v)] | ||
return exact_versions | ||
else: | ||
return [] | ||
|
||
|
||
def detect_python_project_version( | ||
project_path: str, | ||
package_manager: AgentPythonPackageManager, | ||
) -> str | None: | ||
if package_manager == AgentPythonPackageManager.poetry: | ||
pyproject_path = get_full_path(project_path, "pyproject.toml") | ||
with open(pyproject_path, "rb") as file: | ||
pyproject_data = tomllib.load(file) | ||
|
||
# The version might be a range, let's try to find an exact version that is in this range | ||
version_range = pyproject_data["tool"]["poetry"]["dependencies"]["python"] | ||
real_python_versions = __fetch_real_python_versions() | ||
|
||
constraint = parse_constraint(version_range) | ||
for version in real_python_versions: | ||
if constraint.allows(Version.parse(version)): | ||
return version | ||
|
||
# Checking common venv folders config | ||
for venv_folder in ["venv", ".venv"]: | ||
try: | ||
venv_config_path = get_full_path(project_path, f"{venv_folder}/pyvenv.cfg") | ||
with open(venv_config_path, "r") as file: | ||
for line in file: | ||
if line.startswith("version"): | ||
return line.split("=")[1].strip() | ||
except FileNotFoundError: | ||
pass | ||
# | ||
# # Checking if we have a .python-version file, for example created by pyenv | ||
try: | ||
version_file_path = get_full_path(project_path, ".python-version") | ||
with open(version_file_path, "r") as file: | ||
return file.readline().strip() | ||
except FileNotFoundError: | ||
pass | ||
|
||
# TODO: if pyproject, look in pyproject.toml | ||
# TODO: if pip, look in requirements.txt | ||
return None |
Oops, something went wrong.