Skip to content

Commit af6928c

Browse files
committed
Create script for generating sbom
1 parent d8546f0 commit af6928c

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ doc/_build
1212
doc/landing-page/_build
1313
example/Tests/
1414
venv*
15+
*.cdx.json

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ build = [
105105
'nuitka==2.8.9',
106106
"tomli; python_version < '3.11'", # Tomllib is default in 3.11, required for letting codespell read the pyproject.toml]
107107
]
108+
sbom = ["cyclonedx-bom==7.2.1"]
108109

109110
[project.scripts]
110111
dfetch = "dfetch.__main__:main"

script/create_sbom.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
"""Generate an sbom of the tool."""
3+
import contextlib
4+
import logging
5+
import subprocess
6+
import sys
7+
import tempfile
8+
import venv
9+
from pathlib import Path
10+
11+
from dfetch import __version__
12+
13+
logging.basicConfig(level=logging.INFO)
14+
15+
PROJECT_DIR = Path(__file__).parent.parent.resolve()
16+
OUTPUT_FILE = PROJECT_DIR / f"dfetch-{__version__}.{sys.platform}.cdx.json"
17+
18+
DEPS = f"{PROJECT_DIR}[sbom]"
19+
20+
21+
@contextlib.contextmanager
22+
def temporary_venv():
23+
"""Create a temporary virtual environment and clean it up on exit."""
24+
with tempfile.TemporaryDirectory(prefix="venv_sbom_") as tmpdir:
25+
venv_dir = Path(tmpdir)
26+
logging.info(f"Creating temporary virtual environment at {venv_dir}")
27+
venv.create(venv_dir, with_pip=True, upgrade_deps=True)
28+
29+
if sys.platform.startswith("win"):
30+
python_bin = venv_dir / "Scripts" / "python.exe"
31+
else:
32+
python_bin = venv_dir / "bin" / "python"
33+
34+
yield str(python_bin)
35+
36+
37+
with temporary_venv() as python:
38+
subprocess.check_call([python, "-m", "pip", "install", DEPS])
39+
subprocess.check_call(
40+
[python, "-m", "cyclonedx_py", "environment", "-o", str(OUTPUT_FILE)]
41+
)
42+
43+
logging.info(f"SBOM generated at {OUTPUT_FILE}")

0 commit comments

Comments
 (0)