Commit 57f75b39 authored by Boris Baldassari's avatar Boris Baldassari
Browse files

Add scritp to create curations from clearlydefined.io.

parent c39a51e1
###########################################################################
# Copyright (c) 2022 The Eclipse Foundation.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License 2.0 which accompanies this
# distribution, and is available at https://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
###########################################################################
# This script takes a ORT evaluator JSON output file and looks for
# the packages on clearlydefined.io. If the package is defined there, then
# build a set of curations with the proper concluded license.
import json
import requests
import getopt
import sys
# Parse command line arguments
try:
arguments, values = getopt.getopt(sys.argv[1:], "i:o:", ["input=", "output="])
except getopt.error as err:
# Output error, and return with an error code
print(str(err))
sys.exit(2)
file_in = ''
file_out = ''
if len(arguments) < 2:
exit("Usage: curate clearlydefined.py -i evaluation-result.json -o mycurations.yml")
# Evaluate given options
for current_argument, current_value in arguments:
if current_argument in ("-i", "--input"):
file_in = current_value
elif current_argument in ("-o", "--output"):
file_out = current_value
# Read JSON result file from ORT
print(f"Reading JSON file from {file_in}.")
with open(file_in, 'r') as f:
try:
doc = json.load(f)
except json.JSONDecodeError as e:
print(f"Could not parse JSON from {file_in}: error {e}")
exit()
packages = doc['evaluator']['violations']
licenses = {}
urls={}
# Loop through all violations IDs
for package in packages:
pkg = package['pkg']
print(f"- Working on '{pkg}'..", end=" ")
# Transform coordinates
parts = pkg.split(':')
if pkg.startswith('NPM:'):
coordinates = f"npm/npmjs/-/{parts[-2]}/{parts[-1]}"
url = 'https://api.clearlydefined.io/definitions/' + coordinates
# Do a get request to clearlydefined, as described in
# https://docs.clearlydefined.io/using-data
r = requests.get(url)
if r.status_code == 200:
answer = r.json()
if 'licensed' in answer and 'declared' in answer['licensed']:
print(f"Found license: {answer['licensed']['declared']}")
licenses[pkg] = answer['licensed']['declared']
urls[pkg] = 'https://clearlydefined.io/definitions/' + coordinates
else:
print("Not found on clearlydefined.")
else:
print("Not found on clearlydefined.")
print(f"\nFound {len(licenses)} curations.")
# Now write the yaml curation file
curations = []
for p in licenses:
curations.append((
f'- id: "{p}"\n'
f' curations:\n'
f' comment: "Curated from clearlydefined, see {urls[p]}"\n'
f' concluded_license: "{licenses[p]}"'))
print(f"Write curation file to {file_out}.")
with open(file_out, 'w') as f:
f.write('\n'.join(curations))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment