11from __future__ import annotations
22
3- import re
43import subprocess
4+ import sys
55from pathlib import Path
66from typing import Optional
77
88import tomlkit
99from pydantic import (
1010 BaseModel ,
11- model_validator ,
1211)
1312from tomlkit import TOMLDocument
1413
@@ -84,19 +83,36 @@ class PoetryDependencies(BaseModel):
8483 working_directory : Path
8584
8685 @staticmethod
87- def _extract_from_line (line : str ) -> Package :
88- pattern = r"\s+(\d+(?:\.\S+)*)"
89- match = re .split (pattern , line )
90- return Package (name = match [0 ], version = match [1 ])
86+ def _extract_from_line (line : str ) -> Optional [Package ]:
87+ # remove (!) from line as indicates not installed in environment,
88+ # which could occur for optional dependencies
89+ split_line = line .replace ("(!)" , "" ).strip ().split (maxsplit = 2 )
90+ if len (split_line ) < 2 :
91+ print (
92+ f"Unable to parse line={ line } correctly; result={ split_line } " ,
93+ file = sys .stderr ,
94+ )
95+ return None
96+ return Package (name = split_line [0 ], version = split_line [1 ])
9197
9298 def _extract_from_poetry_show (self , output_text : str ) -> list [Package ]:
93- return [self ._extract_from_line (line ) for line in output_text .splitlines ()]
99+ return [
100+ package
101+ for line in output_text .splitlines ()
102+ if (package := self ._extract_from_line (line ))
103+ ]
94104
95105 @property
96106 def direct_dependencies (self ) -> dict [str , list [Package ]]:
97107 dependencies = {}
98108 for group in self .groups :
99- command = ("poetry" , "show" , "--top-level" , f"--only={ group .name } " )
109+ command = (
110+ "poetry" ,
111+ "show" ,
112+ "--top-level" ,
113+ f"--only={ group .name } " ,
114+ "--no-truncate" ,
115+ )
100116 output = subprocess .run (
101117 command ,
102118 capture_output = True ,
@@ -110,7 +126,7 @@ def direct_dependencies(self) -> dict[str, list[Package]]:
110126
111127 @property
112128 def all_dependencies (self ) -> dict [str , list [Package ]]:
113- command = ("poetry" , "show" )
129+ command = ("poetry" , "show" , "--no-truncate" )
114130 output = subprocess .run (
115131 command ,
116132 capture_output = True ,
0 commit comments