Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
Get current crewai version from poetry.lock
Thiago Moretto345f1eacde61
diff --git a/src/crewai/cli/deploy/utils.py b/src/crewai/cli/deploy/utils.pyindex 5a701e675..a7534fc48 100644--- a/src/crewai/cli/deploy/utils.py+++ b/src/crewai/cli/deploy/utils.py@@ -1,6 +1,7 @@import sysimport reimport subprocess+import astfrom ..authentication.utils import TokenManager@@ -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"diff --git a/tests/cli/deploy/test_deploy_main.py b/tests/cli/deploy/test_deploy_main.pyindex 52171459a..a63ea7110 100644--- a/tests/cli/deploy/test_deploy_main.py+++ b/tests/cli/deploy/test_deploy_main.py@@ -153,21 +153,6 @@ class TestDeployCommand(unittest.TestCase):"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 = """@@ -177,11 +162,10 @@ class TestDeployCommand(unittest.TestCase):[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]@@ -190,7 +174,7 @@ class TestDeployCommand(unittest.TestCase):[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@@ -205,9 +189,31 @@ class TestDeployCommand(unittest.TestCase):[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_nameproject_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())