blob: 59e5ffd7879865de5e3377d8a605b386ffc4a8fc [file] [log] [blame]
#!/usr/bin/env python3
#
# Copyright 2022 Google LLC
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# This script is used to automatically generate package
# information for SBOM of COS image bundled dependencies.
import os
import sys
from sbom_info_lib import download_url
from sbom_info_lib import go_dep
from sbom_info_lib import licenses
SBOM_INFO_FILE_NAME = "sbom-pkg-info"
class SbomPackageInfo:
def __init__(self, url, license, go_dep):
self.download_url = url
self.licenses = license
self.go_dep = go_dep
def write_to_build_info(self, build_info_dir):
with open(f"{build_info_dir}/{SBOM_INFO_FILE_NAME}", "w") as f:
f.write(f"download-url:{self.download_url}\n")
f.write(f"licenses:{self.licenses}\n")
f.write(f"go-dep:{self.go_dep}\n")
class SBOMPkgInfoError(Exception):
def __init__(self, msg):
super().__init__(msg)
def main():
package_dir = os.getenv("PORTAGE_BUILDDIR")
build_info_dir = os.path.join(package_dir, "build-info")
package_name = os.path.basename(package_dir)
ebuild = os.path.join(build_info_dir, package_name + ".ebuild")
url = download_url.get_download_url(build_info_dir, ebuild)
sbom_pkg_info = SbomPackageInfo(
url,
licenses.get_licenses(build_info_dir),
go_dep.get_go_dep(url, build_info_dir),
)
if not sbom_pkg_info.download_url and "private-overlays" not in ebuild:
raise SBOMPkgInfoError(f"download url not found")
if not sbom_pkg_info.licenses:
raise SBOMPkgInfoError(f"license not found")
sbom_pkg_info.write_to_build_info(build_info_dir)
if __name__ == "__main__":
sys.exit(main())