Skip to content

Commit

Permalink
Get current crewai version from poetry.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomoretto committed Aug 29, 2024
1 parent fa937bf commit 345f1ea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
38 changes: 17 additions & 21 deletions src/crewai/cli/deploy/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import re
import subprocess
import ast

from ..authentication.utils import TokenManager

Expand Down Expand Up @@ -92,32 +93,27 @@ def get_project_name(pyproject_path: str = "pyproject.toml"):
return None


def get_crewai_version(pyproject_path: str = "pyproject.toml") -> str:
"""Get the version number of crewai from the pyproject.toml file."""
def get_crewai_version(poetry_lock_path: str = "poetry.lock") -> str:
"""Get the version number of crewai from the poetry.lock file."""
try:
# Read the pyproject.toml file
with open(pyproject_path, "rb") as f:
pyproject_content = parse_toml(f.read())

# Extract the version number of crewai
crewai_version = pyproject_content["tool"]["poetry"]["dependencies"]["crewai"][
"version"
]
with open(poetry_lock_path, "r") as f:
lock_content = f.read()

return crewai_version
match = re.search(
r'\[\[package\]\]\s*name\s*=\s*"crewai"\s*version\s*=\s*"([^"]+)"',
lock_content,
re.DOTALL,
)
if match:
return match.group(1)
else:
print("crewai package not found in poetry.lock")
return "no-version-found"

except FileNotFoundError:
print(f"Error: {pyproject_path} not found.")
except KeyError:
print(f"Error: {pyproject_path} is not a valid pyproject.toml file.")
except tomllib.TOMLDecodeError if sys.version_info >= (3, 11) else Exception as e:
print(
f"Error: {pyproject_path} is not a valid TOML file."
if sys.version_info >= (3, 11)
else f"Error reading the pyproject.toml file: {e}"
)
print(f"Error: {poetry_lock_path} not found.")
except Exception as e:
print(f"Error reading the pyproject.toml file: {e}")
print(f"Error reading the poetry.lock file: {e}")

return "no-version-found"

Expand Down
44 changes: 25 additions & 19 deletions tests/cli/deploy/test_deploy_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,6 @@ def test_remove_crew(self):
"Crew 'test_project' removed successfully", fake_out.getvalue()
)

@patch('crewai.cli.deploy.utils.sys.version_info', (3, 10))
def test_parse_toml_python_310(self):
toml_content = """
[tool.poetry]
name = "test_project"
version = "0.1.0"
[tool.poetry.dependencies]
python = "^3.10"
crewai = "^0.1.0"
"""
parsed = parse_toml(toml_content)
self.assertEqual(parsed['tool']['poetry']['name'], 'test_project')
self.assertEqual(parsed['tool']['poetry']['dependencies']['crewai'], '^0.1.0')

@unittest.skipIf(sys.version_info < (3, 11), "Requires Python 3.11+")
def test_parse_toml_python_311_plus(self):
toml_content = """
Expand All @@ -177,11 +162,10 @@ def test_parse_toml_python_311_plus(self):
[tool.poetry.dependencies]
python = "^3.11"
crewai = "^0.1.0"
crewai = { extras = ["tools"], version = ">=0.51.0,<1.0.0" }
"""
parsed = parse_toml(toml_content)
self.assertEqual(parsed['tool']['poetry']['name'], 'test_project')
self.assertEqual(parsed['tool']['poetry']['dependencies']['crewai'], '^0.1.0')

@patch('builtins.open', new_callable=unittest.mock.mock_open, read_data="""
[tool.poetry]
Expand All @@ -190,7 +174,7 @@ def test_parse_toml_python_311_plus(self):
[tool.poetry.dependencies]
python = "^3.10"
crewai = "^0.1.0"
crewai = { extras = ["tools"], version = ">=0.51.0,<1.0.0" }
""")
def test_get_project_name_python_310(self, mock_open):
from crewai.cli.deploy.utils import get_project_name
Expand All @@ -205,9 +189,31 @@ def test_get_project_name_python_310(self, mock_open):
[tool.poetry.dependencies]
python = "^3.11"
crewai = "^0.1.0"
crewai = { extras = ["tools"], version = ">=0.51.0,<1.0.0" }
""")
def test_get_project_name_python_311_plus(self, mock_open):
from crewai.cli.deploy.utils import get_project_name
project_name = get_project_name()
self.assertEqual(project_name, 'test_project')

@patch('builtins.open', new_callable=unittest.mock.mock_open, read_data="""
[[package]]
name = "crewai"
version = "0.51.1"
description = "Some description"
category = "main"
optional = false
python-versions = ">=3.10,<4.0"
""")
def test_get_crewai_version(self, mock_open):
from crewai.cli.deploy.utils import get_crewai_version
version = get_crewai_version()
self.assertEqual(version, '0.51.1')

@patch('builtins.open', side_effect=FileNotFoundError)
def test_get_crewai_version_file_not_found(self, mock_open):
from crewai.cli.deploy.utils import get_crewai_version
with patch('sys.stdout', new=StringIO()) as fake_out:
version = get_crewai_version()
self.assertEqual(version, 'no-version-found')
self.assertIn("Error: poetry.lock not found.", fake_out.getvalue())

0 comments on commit 345f1ea

Please sign in to comment.