Skip to content
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

[WIP] Uses python 3.5.x+ compatible command to invoke fossa-cli #8

Draft
wants to merge 2 commits into
base: dunfell
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions classes/fossa.bbclass
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ python do_fossa_pkg() {
# this way it is able to capture the output of `do_fossa_pkg` for every
# package that was processed.
python do_fossa() {
from oe.rootfs import image_list_installed_packages

if not is_fossa_enabled(d):
bb.debug(1, "Since FOSSA_ENABLED is 0, skipping: creating fossa-deps.json")
return
Expand All @@ -93,13 +95,14 @@ python do_fossa() {

metadata_dir = d.getVar('FOSSA_METADATA_RECIPES')
pkg_metadata = all_pkg_metadata(d, metadata_dir)
pkgs = image_list_installed_packages(d)

installed_pkgs = []
for pkg in pkg_metadata:
for pkg in pkgs:
try:
installed_pkgs.append(mk_user_dependencies(pkg_metadata[pkg]))
except Exception as err:
bb.error(f'failed to retrieve pkg metadata for {pkg} because: {err}')
except Exception:
bb.debug(f'failed to retrieve pkg metadata for {pkg} because: {err}')

# Ensure path exists
fossa_deps_dir = d.getVar("FOSSA_STAGING_DIR")
Expand Down
16 changes: 10 additions & 6 deletions classes/fossa_upload.bbclass
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ inherit fossa_utils
addtask do_fossa_analyze before do_build after do_rootfs
do_fossa_analyze[doc] = "Analyze via fossa-cli"
do_fossa_analyze[nostamp] = "1"
do_fossa_analyze[depends] = "fossa-cli:do_populate_sysroot"
do_fossa_analyze[depends] = "fossa-cli-native:do_populate_sysroot"

addtask do_fossa_test before do_build after do_fossa_analyze
do_fossa_test[doc] = "Test via fossa-cli"
do_fossa_test[nostamp] = "1"
do_fossa_test[deptask] += "fossa-cli:do_populate_sysroot"
do_fossa_test[deptask] += "fossa-cli-native:do_populate_sysroot"

# This task runs `fossa-cli` against the `fossa-deps` file generated by `fossa:do_fossa`,
# This task runs `fossa-cli-native` against the `fossa-deps` file generated by `fossa:do_fossa`,
# analyzing the file and storing its results in the FOSSA backend.
#
# This task is run after `do_rootfs` is finalized (`fossa:do_fossa` runs as a post-processing
Expand Down Expand Up @@ -44,6 +44,8 @@ python do_fossa_test() {
bb.debug(1, "Since FOSSA_TEST_ENABLED is 0, skipping: fossa test")
return

bb.plain("Running fossa test command. It will fail build if FOSSA finds unresolved licensing issues.")
bb.plain("To not fail fatally, fossa test command can be disabled by, setting: FOSSA_TEST_ENABLED to 0.")
run_fossa_cli(d, mk_fossa_cmd(d, 'test'))
}

Expand All @@ -54,11 +56,13 @@ def run_fossa_cli(d, cli_args):
BINDIR = d.getVar("bindir")
WORKDIR = d.getVar("WORKDIR")

cli_path = (f"{WORKDIR}/recipe-sysroot{BINDIR}/fossa")
cmds = [cli_path] + cli_args
# We don't need to specify the whole path here. The sysroot-native
# directory is already in our PATH.
fossa_cli = ("fossa")
cmds = [fossa_cli] + cli_args
bb.plain(f"running: {' '.join(cmds)}")

out = subprocess.run(cmds, cwd=d.getVar("FOSSA_STAGING_DIR"), capture_output=True, text=True, shell=False)
out = subprocess.run(cmds, cwd=d.getVar("FOSSA_STAGING_DIR"), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=False)
if out.returncode != 0:
bb.fatal(out.stderr)
else:
Expand Down
79 changes: 79 additions & 0 deletions recipes-extended/fossa/fossa-cli-native.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

# These special variables control how BitBake builds the project.
#
# Reference: https://docs.yoctoproject.org/bitbake/2.2/bitbake-user-manual/bitbake-user-manual-ref-variables.html

SUMMARY = "Flexible, performant dependency analysis"
HOMEPAGE = "https://fossa.com"
LICENSE = "MPL-2.0"
DEPENDS += "unzip-native"
INHIBIT_DEFAULT_DEPS = "1"
LIC_FILES_CHKSUM = "file://LICENSE;md5=815ca599c9df247a0c7f619bab123dad"
S = "${WORKDIR}/fossa-cli-${PV}"

inherit native

# Every BitBake recipe implicitly inherits `base.bbclass`: https://docs.yoctoproject.org/ref-manual/classes.html#base-bbclass
# This means that in addition to the tasks defined in this file, implicit tasks are defined.
# Refer to `meta/classes/base.bbclass` in Poky for the implementation of these implicit tasks
# and dependencies.
#
# In Dunfell, the following is the implicit task ordering:
# - addtask fetch
# - addtask unpack after do_fetch
# - addtask configure after do_patch
# - addtask compile after do_configure
# - addtask install after do_compile
# - addtask build after do_populate_sysroot
# - addtask cleansstate after do_clean
# - addtask cleanall after do_cleansstate
#
# An observant reader may have noticed that the `fossa` class is inherited in the quickstart,
# but nothing is explicitly added to depend on this `fossa-cli` recipe.
#
# The connection is in the `fossa_upload` class:
#
# ```
# do_fossa_analyze[depends] = "fossa-cli:do_populate_sysroot"
# do_fossa_test[deptask] += "fossa-cli:do_populate_sysroot"
# ```
#
# From the Yocto docs (https://docs.yoctoproject.org/ref-manual/tasks.html#do-populate-sysroot),
# `do_populate_sysroot` depends on the `do_install` task since it populates files from that task
# into the sysroot.
#
# In turn as we can see in the base class, `do_install` depends on `do_configure`,
# which depends on `fetch` which starts things off by pulling the `fossa-cli` bundle
# as part of the configuration step. Note: we delete the do_compile task here as
# there is nothing to compile for this.
#
# While we specify populate_sysroot as the step to perform this, since we are a native
# recipe installing a binary to be run on the build host, the install phase is applied
# to sysroot-native.

# https://docs.yoctoproject.org/ref-manual/tasks.html#do-compile
#
# Implicitly, before this step is run, the `fetch` and `unpack` tasks are run.
# These ensure that the source at `SRC_URI` is present in `${S}`.
#
# This task downloads `fossa-cli` at the specified version to `${D}`.
# The version provided to the install script should match the version specified by `${PV}`.
do_configure() {
chmod a+x ${S}/install-latest.sh
${S}/install-latest.sh -b ${S} -d v${PV}
}

do_compile[noexec] = "1"

# https://docs.yoctoproject.org/ref-manual/tasks.html#do-install
#
# Implicitly, before this step is run the `compile` task is run.
#
# This task installs the downloaded `fossa` binary to the BitBake binary directory
# for invocation.
do_install() {
install -d ${D}${bindir}/
install -m 0755 ${S}/fossa ${D}${bindir}/fossa
}

INSANE_SKIP_${PN} += "already-stripped"
4 changes: 4 additions & 0 deletions recipes-extended/fossa/fossa-cli-native_3.6.0.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SRC_URI = "https://github.com/fossas/fossa-cli/archive/refs/tags/v${PV}.tar.gz"
SRC_URI[sha256sum] = "31eac60f057b191734f5b4cc9ffedc25f9a2726828ddad99e6392dc10d638e1c"

require fossa-cli-native.inc
Loading