Skip to content

Instantly share code, notes, and snippets.

@rtabassum
Created March 24, 2023 18:38
Show Gist options
  • Save rtabassum/13ee821dacf444577e8802dd59228c5c to your computer and use it in GitHub Desktop.
Save rtabassum/13ee821dacf444577e8802dd59228c5c to your computer and use it in GitHub Desktop.
def _generate_osl_impl(ctx):
"""Implements `_generate_osl`.
# Gather all licenses and write information to OSL.
"""
# Declare the output OSL file.
osl_file = ctx.actions.declare_file("osl.txt")
target = ctx.attr.src
if not TransitiveLicensesInfo in target:
return [OutputGroupInfo(licenses = depset())]
target_trans_license_info = target[TransitiveLicensesInfo]
# If the result doesn't contain licenses, we simply return the provider
if not hasattr(target_trans_license_info, "target_under_license"):
return [OutputGroupInfo(licenses = depset())]
# Write the output file for the target
lic_info, lic_files = licenses_info_to_json(target_trans_license_info)
content = "[\n%s\n]\n" % ",\n".join(lic_info)
licenseinfo_json = ctx.actions.declare_file("_%s_licenses_info.json" % ctx.label.name)
ctx.actions.write(
output = licenseinfo_json,
content = content,
)
# generate osl file
args = ctx.actions.args()
args.add_all([
licenseinfo_json,
osl_file,
])
ctx.actions.run(
outputs = [osl_file],
executable = ctx.executable._osl_generator,
inputs = [licenseinfo_json] + lic_files,
arguments = [args],
progress_message = "Generating OSL file {} by parsing the json \
containing license information".format(ctx.label),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment