Skip to content

Commit 2bf1bc3

Browse files
Merge pull request #41 from HalaAli198/fix/setup-py-split-dependencies
Fix: Parse unquoted dependencies in setup.py with .split() pattern
2 parents 1fcc88f + adb9ddf commit 2bf1bc3

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)