Skip to content

Commit 1623bb0

Browse files
author
HalaAli198
committed
Fix: Parse unquoted dependencies in setup.py with .split() pattern
- Fixed bug where file was read twice, causing second regex to fail - Added support for multiline string dependencies using .split() - Stores file content in variable and reuses it for both patterns - Maintains backward compatibility with list-style dependencies - Filters out empty lines and comments from multiline strings - Handles both triple double-quotes and triple single-quotes Tested with mayan-edms==4.9.2 which uses this pattern. Before fix: 0 dependencies detected After fix: 56 dependencies correctly parsed
1 parent 2ec02e5 commit 1623bb0

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

sbom4python/scanner.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,9 @@ def process_setup_py(self, filename):
689689
if filePath.exists() and filePath.is_file():
690690
dependencies = []
691691
with open(filename, "r") as setup_file:
692+
content = setup_file.read()
692693
# Read the file into a stream and search for list if dependencies specified by install_requires
693-
stream = setup_file.read().replace("\n", "")
694+
stream = content.replace("\n", "")
694695
match = re.search(r"install_requires\s*=\s*\[([^\]]+)\]", stream)
695696
if match:
696697
dependency_list = match.group(1).strip()
@@ -699,6 +700,23 @@ def process_setup_py(self, filename):
699700
for dep in dependency_list.split(",")
700701
if len(dep) > 0
701702
]
703+
# Method 2: Handle multiline string with .split()
704+
# Handles: install_requires = """package==1.0\npackage2>=2.0""".split()
705+
# Also handles single quotes: install_requires = '''...'''.split()
706+
if not dependencies:
707+
split_match = re.search(r'install_requires\s*=\s*["\'"]{3}([^"\']+)["\'"]{3}\.split\(\)', content, re.DOTALL)
708+
if split_match:
709+
# Extract dependencies from the multiline string
710+
deps_block = split_match.group(1).strip()
711+
# Split by newlines and filter out empty lines
712+
dependencies = [
713+
line.strip()
714+
for line in deps_block.split('\n')
715+
if line.strip() and not line.strip().startswith('#')
716+
]
717+
718+
719+
702720
if self.debug:
703721
print(dependencies)
704722
self.set_lifecycle("pre-build")

0 commit comments

Comments
 (0)