pull-sdpx-licenses.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #! /usr/bin/env python3
  2. #
  3. # Copyright OpenEmbedded Contributors
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. import argparse
  7. import json
  8. import sys
  9. import urllib.request
  10. from pathlib import Path
  11. TOP_DIR = Path(__file__).parent.parent
  12. def main():
  13. parser = argparse.ArgumentParser(
  14. description="Update SPDX License files from upstream"
  15. )
  16. parser.add_argument(
  17. "-v",
  18. "--version",
  19. metavar="MAJOR.MINOR[.MICRO]",
  20. help="Pull specific version of License list instead of latest",
  21. )
  22. parser.add_argument(
  23. "--overwrite",
  24. action="store_true",
  25. help="Update existing license file text with upstream text",
  26. )
  27. parser.add_argument(
  28. "--deprecated",
  29. action="store_true",
  30. help="Update deprecated licenses",
  31. )
  32. parser.add_argument(
  33. "--dest",
  34. type=Path,
  35. default=TOP_DIR / "meta" / "files" / "common-licenses",
  36. help="Write licenses to directory DEST. Default is %(default)s",
  37. )
  38. args = parser.parse_args()
  39. if args.version:
  40. version = f"v{args.version}"
  41. else:
  42. # Fetch the latest release
  43. req = urllib.request.Request(
  44. "https://api.github.com/repos/spdx/license-list-data/releases/latest"
  45. )
  46. req.add_header("X-GitHub-Api-Version", "2022-11-28")
  47. req.add_header("Accept", "application/vnd.github+json")
  48. with urllib.request.urlopen(req) as response:
  49. data = json.load(response)
  50. version = data["tag_name"]
  51. print(f"Pulling SPDX license list version {version}")
  52. req = urllib.request.Request(
  53. f"https://raw.githubusercontent.com/spdx/license-list-data/{version}/json/licenses.json"
  54. )
  55. with urllib.request.urlopen(req) as response:
  56. spdx_licenses = json.load(response)
  57. with (TOP_DIR / "meta" / "files" / "spdx-licenses.json").open("w") as f:
  58. json.dump(spdx_licenses, f, sort_keys=True, indent=2)
  59. total_count = len(spdx_licenses["licenses"])
  60. updated = 0
  61. for idx, lic in enumerate(spdx_licenses["licenses"]):
  62. lic_id = lic["licenseId"]
  63. print(f"[{idx + 1} of {total_count}] ", end="")
  64. dest_license_file = args.dest / lic_id
  65. if dest_license_file.is_file() and not args.overwrite:
  66. print(f"Skipping {lic_id} since it already exists")
  67. continue
  68. print(f"Fetching {lic_id}... ", end="", flush=True)
  69. req = urllib.request.Request(lic["detailsUrl"])
  70. with urllib.request.urlopen(req) as response:
  71. lic_data = json.load(response)
  72. if lic_data["isDeprecatedLicenseId"] and not args.deprecated:
  73. print("Skipping (deprecated)")
  74. continue
  75. with dest_license_file.open("w") as f:
  76. f.write(lic_data["licenseText"])
  77. updated += 1
  78. print("done")
  79. print(f"Updated {updated} licenses")
  80. return 0
  81. if __name__ == "__main__":
  82. sys.exit(main())