22
33import re
44import subprocess
5+ import sys
56from pathlib import Path
67from typing import Optional
78
89import tomlkit
910from pydantic import (
1011 BaseModel ,
11- model_validator ,
1212)
1313from tomlkit import TOMLDocument
1414
@@ -84,19 +84,33 @@ class PoetryDependencies(BaseModel):
8484 working_directory : Path
8585
8686 @staticmethod
87- def _extract_from_line (line : str ) -> Package :
88- pattern = r"\s+(\d+(?:\.\d+)*)\s+"
89- match = re .split (pattern , line )
90- return Package (name = match [0 ], version = match [1 ]) #
87+ def _extract_from_line (line : str ) -> Optional [Package ]:
88+ # remove (!) from line as indicates not installed in environment,
89+ # which could occur for optional dependencies
90+ split_line = line .replace ("(!)" , "" ).strip ().split (maxsplit = 2 )
91+ if len (split_line ) < 2 :
92+ print (f"Unable to parse dependency={ line } " )
93+ return None
94+ return Package (name = split_line [0 ], version = split_line [1 ])
9195
9296 def _extract_from_poetry_show (self , output_text : str ) -> list [Package ]:
93- return [self ._extract_from_line (line ) for line in output_text .splitlines ()]
97+ return [
98+ package
99+ for line in output_text .splitlines ()
100+ if (package := self ._extract_from_line (line ))
101+ ]
94102
95103 @property
96104 def direct_dependencies (self ) -> dict [str , list [Package ]]:
97105 dependencies = {}
98106 for group in self .groups :
99- command = ("poetry" , "show" , "--top-level" , f"--only={ group .name } " )
107+ command = (
108+ "poetry" ,
109+ "show" ,
110+ "--top-level" ,
111+ f"--only={ group .name } " ,
112+ "--no-truncate" ,
113+ )
100114 output = subprocess .run (
101115 command ,
102116 capture_output = True ,
@@ -110,7 +124,7 @@ def direct_dependencies(self) -> dict[str, list[Package]]:
110124
111125 @property
112126 def all_dependencies (self ) -> dict [str , list [Package ]]:
113- command = ("poetry" , "show" )
127+ command = ("poetry" , "show" , "--no-truncate" )
114128 output = subprocess .run (
115129 command ,
116130 capture_output = True ,
@@ -128,7 +142,7 @@ def all_dependencies(self) -> dict[str, list[Package]]:
128142 }
129143 for line in output .stdout .splitlines ():
130144 dep = self ._extract_from_line (line = line )
131- if dep .name not in names_direct_dependencies :
145+ if dep and dep .name not in names_direct_dependencies :
132146 transitive_dependencies .append (dep )
133147
134148 return direct_dependencies | {TRANSITIVE_GROUP .name : transitive_dependencies }
0 commit comments