Openembedded Core Discussions
 help / color / mirror / Atom feed
From: Joshua Watt <jpewhacker@gmail.com>
To: openembedded-core@lists.openembedded.org
Cc: Joshua Watt <JPEWhacker@gmail.com>
Subject: [OE-core][PATCH 1/2] scripts/pull-spdx-licenses.py: Add script
Date: Thu, 27 Jun 2024 16:16:55 -0600	[thread overview]
Message-ID: <20240627221804.599573-2-JPEWhacker@gmail.com> (raw)
In-Reply-To: <20240627221804.599573-1-JPEWhacker@gmail.com>

Adds a script to pull the SPDX license data and update the license list
JSON data, as well as update the license directory.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
---
 scripts/pull-sdpx-licenses.py | 101 ++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
 create mode 100755 scripts/pull-sdpx-licenses.py

diff --git a/scripts/pull-sdpx-licenses.py b/scripts/pull-sdpx-licenses.py
new file mode 100755
index 00000000000..597a62133fb
--- /dev/null
+++ b/scripts/pull-sdpx-licenses.py
@@ -0,0 +1,101 @@
+#! /usr/bin/env python3
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: GPL-2.0-only
+
+import argparse
+import json
+import sys
+import urllib.request
+from pathlib import Path
+
+TOP_DIR = Path(__file__).parent.parent
+
+
+def main():
+    parser = argparse.ArgumentParser(
+        description="Update SPDX License files from upstream"
+    )
+    parser.add_argument(
+        "-v",
+        "--version",
+        metavar="MAJOR.MINOR[.MICRO]",
+        help="Pull specific version of License list instead of latest",
+    )
+    parser.add_argument(
+        "--overwrite",
+        action="store_true",
+        help="Update existing license file text with upstream text",
+    )
+    parser.add_argument(
+        "--deprecated",
+        action="store_true",
+        help="Update deprecated licenses",
+    )
+    parser.add_argument(
+        "--dest",
+        type=Path,
+        default=TOP_DIR / "meta" / "files" / "common-licenses",
+        help="Write licenses to directory DEST. Default is %(default)s",
+    )
+
+    args = parser.parse_args()
+
+    if args.version:
+        version = f"v{args.version}"
+    else:
+        # Fetch the latest release
+        req = urllib.request.Request(
+            "https://api.github.com/repos/spdx/license-list-data/releases/latest"
+        )
+        req.add_header("X-GitHub-Api-Version", "2022-11-28")
+        req.add_header("Accept", "application/vnd.github+json")
+        with urllib.request.urlopen(req) as response:
+            data = json.load(response)
+            version = data["tag_name"]
+
+    print(f"Pulling SPDX license list version {version}")
+    req = urllib.request.Request(
+        f"https://raw.githubusercontent.com/spdx/license-list-data/{version}/json/licenses.json"
+    )
+    with urllib.request.urlopen(req) as response:
+        spdx_licenses = json.load(response)
+
+    with (TOP_DIR / "meta" / "files" / "spdx-licenses.json").open("w") as f:
+        json.dump(spdx_licenses, f, sort_keys=True, indent=2)
+
+    total_count = len(spdx_licenses["licenses"])
+    updated = 0
+    for idx, lic in enumerate(spdx_licenses["licenses"]):
+        lic_id = lic["licenseId"]
+
+        print(f"[{idx + 1} of {total_count}] ", end="")
+
+        dest_license_file = args.dest / lic_id
+        if dest_license_file.is_file() and not args.overwrite:
+            print(f"Skipping {lic_id} since it already exists")
+            continue
+
+        print(f"Fetching {lic_id}... ", end="", flush=True)
+
+        req = urllib.request.Request(lic["detailsUrl"])
+        with urllib.request.urlopen(req) as response:
+            lic_data = json.load(response)
+
+        if lic_data["isDeprecatedLicenseId"] and not args.deprecated:
+            print("Skipping (deprecated)")
+            continue
+
+        with dest_license_file.open("w") as f:
+            f.write(lic_data["licenseText"])
+        updated += 1
+        print("done")
+
+    print(f"Updated {updated} licenses")
+
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())
-- 
2.45.2



  reply	other threads:[~2024-06-27 22:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-27 22:16 [OE-core][PATCH 0/2] Update to latest version of SPDX licenses Joshua Watt
2024-06-27 22:16 ` Joshua Watt [this message]
2024-06-27 22:16 ` [OE-core][PATCH 2/2] licenses: Update to SPDX license version 3.24.0 Joshua Watt
2024-07-01  6:56 ` [OE-core][PATCH 0/2] Update to latest version of SPDX licenses Richard Purdie
2024-07-01 14:32   ` Joshua Watt
2024-07-08  8:17     ` Martin Jansa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240627221804.599573-2-JPEWhacker@gmail.com \
    --to=jpewhacker@gmail.com \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox