Skip to content

Commit dcd4dc8

Browse files
Merge branch 'main' of github.com:anthonyharrison/sbom4python
2 parents e8b0789 + 77b4ec2 commit dcd4dc8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

sbom4python/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def main(argv=None):
7575
default=False,
7676
help="use pip for package management",
7777
)
78+
input_group.add_argument(
79+
"--python",
80+
action="store",
81+
help="use specified Python interpreter for pip",
82+
)
7883

7984
output_group = parser.add_argument_group("Output")
8085
output_group.add_argument(
@@ -130,6 +135,7 @@ def main(argv=None):
130135
"debug": False,
131136
"format": "tag",
132137
"graph": "",
138+
"python": "",
133139
}
134140

135141
raw_args = parser.parse_args(argv[1:])
@@ -167,6 +173,7 @@ def main(argv=None):
167173
args["exclude_license"],
168174
include_service=args["include_service"],
169175
use_pip=args["use_pip"],
176+
python_path=args["python"],
170177
)
171178

172179
if len(module_name) > 0:

sbom4python/scanner.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import subprocess
1111
import sys
1212
import unicodedata
13+
from typing import Iterable
1314

1415
if sys.version_info >= (3, 11):
1516
import tomllib as toml
@@ -42,6 +43,7 @@ def __init__(
4243
lifecycle="build",
4344
include_service=False,
4445
use_pip=False,
46+
python_path:str=None
4547
):
4648
self.record = []
4749
self.debug = debug
@@ -62,16 +64,21 @@ def __init__(
6264
self.set_lifecycle(lifecycle)
6365
self.metadata = {}
6466
self.use_pip = use_pip
67+
self.python_path = pathlib.Path(python_path)
6568

6669
def set_parent(self, module):
6770
self.parent = f"Python-{module}"
6871

69-
def run_program(self, command_line):
70-
# Remove any null bytes
71-
command_line = command_line.replace("\x00", "")
72-
# Split command line into individual elements
73-
params = command_line.split()
74-
res = subprocess.run(params, capture_output=True, text=True)
72+
def run_pip_cmd(self, params:Iterable[str]):
73+
cmd = ["pip"]
74+
if self.python_path.exists():
75+
cmd.extend(("--python", str(self.python_path)))
76+
77+
cmd.extend(params)
78+
return self.run_program(cmd)
79+
80+
def run_program(self, params:Iterable[str]):
81+
res = subprocess.run(list(params), capture_output=True, text=True)
7582
return res.stdout.splitlines()
7683

7784
def set_lifecycle(self, lifecycle):
@@ -369,7 +376,7 @@ def _extract_package_names(self, requirements_list):
369376
def _getpackage_metadata(self, module):
370377
metadata = {}
371378
if self.use_pip:
372-
out = self.run_program(f"pip show {module}")
379+
out = self.run_pip_cmd(("show", module))
373380
for line in out:
374381
entry = line.split(":")
375382
# If: this line contain an non-empty entry delimited by ':'
@@ -560,7 +567,7 @@ def process_python_module(self, module_name):
560567
def _get_installed_modules(self):
561568
modules = []
562569
if self.use_pip:
563-
out = self.run_program("pip list")
570+
out = self.run_pip_cmd(("list", ))
564571
if len(out) > 0:
565572
# Ignore headers in output stream
566573
for m in out[2:]:

0 commit comments

Comments
 (0)