8000 gh-112302: Add Software Bill-of-Materials (SBOM) tracking for dependencies by sethmlarson · Pull Request #112303 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112302: Add Software Bill-of-Materials (SBOM) tracking for dependencies #112303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Filter out ignored files using gitignore
  • Loading branch information
sethmlarson committed Dec 6, 2023
commit acdd91bcf5e8c6482ad1f98788c9e90ddf576fcc
33 changes: 32 additions & 1 deletion Tools/build/generate_sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import glob
import pathlib
import subprocess
import typing

# Before adding a new entry to this list, double check that
Expand Down Expand Up @@ -78,6 +79,33 @@ def spdx_id(value: str) -> str:
return re.sub(r"[^a-zA-Z0-9.\-]+", "-", value)


def filter_gitignored_paths(paths: list[str]) -> list[str]:
"""
Filter out paths excluded by the gitignore file.
The output of 'git check-ignore --non-matching --verbose' looks
like this for non-matching (included) files:

'::<whitespace><path>'

And looks like this for matching (excluded) files:

'.gitignore:9:*.a Tools/lib.a'
"""
# Filter out files in gitignore.
# Non-matching files show up as '::<whitespace><path>'
git_check_ignore_proc = subprocess.run(
["git", "check-ignore", "--verbose", "--non-matching", *paths],
check=False,
stdout=subprocess.PIPE
)
# 1 means matches, 0 means no matches.
assert git_check_ignore_proc.returncode in (0, 1)

# Return the list of paths sorted
git_check_ignore_lines = git_check_ignore_proc.stdout.decode().splitlines()
return sorted([line.split()[-1] for line in git_check_ignore_lines if line.startswith("::")])


def main() -> None:
root_dir = pathlib.Path(__file__).parent.parent.parent
sbom_path = root_dir / "Misc/sbom.spdx.json"
Expand Down Expand Up @@ -108,7 +136,10 @@ def main() -> None:
package_spdx_id = spdx_id(f"SPDXRef-PACKAGE-{name}")
exclude = files.exclude or ()
for include in sorted(files.include):
paths = sorted(glob.glob(include, root_dir=root_dir, recursive=True))

# Find all the paths and then filter them through .gitignore.
paths = glob.glob(include, root_dir=root_dir, recursive=True)
paths = filter_gitignored_paths(paths)
assert paths, include # Make sure that every value returns something!

for path in paths:
Expand Down
0