|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2021 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +from urllib.request import urlopen |
| 7 | +import os |
| 8 | +import json |
| 9 | +import traceback |
| 10 | + |
| 11 | + |
| 12 | +def create_frequentlicenses(): |
| 13 | + success = True |
| 14 | + error_msg = '' |
| 15 | + licenses = '' |
| 16 | + licenses_file = 'licenses.json' |
| 17 | + frequentLicenses = {} |
| 18 | + _frequentLicenses_key = 'frequentLicenses' |
| 19 | + |
| 20 | + # spdx_txt_url = _spdx_txt_base_url + version + /text/ + licenseId + '.txt' |
| 21 | + _spdx_txt_base_url = 'https://raw.githubusercontent.com/spdx/license-list-data/' |
| 22 | + |
| 23 | + base_dir = os.path.dirname(__file__) |
| 24 | + # https://github.com/spdx/license-list-data/blob/v3.14/json/licenses.json |
| 25 | + file_withpath = os.path.join(base_dir, licenses_file) |
| 26 | + |
| 27 | + try: |
| 28 | + with open(file_withpath, 'r') as f: |
| 29 | + licenses = json.load(f) |
| 30 | + version = licenses['licenseListVersion'] |
| 31 | + frequentLicenses[_frequentLicenses_key] = [] |
| 32 | + for lic in licenses['licenses']: |
| 33 | + tmp_lic = {} |
| 34 | + tmp_lic["fullName"] = lic['name'] |
| 35 | + tmp_lic["shortName"] = lic['licenseId'] |
| 36 | + |
| 37 | + deprecated = lic['isDeprecatedLicenseId'] |
| 38 | + if deprecated: |
| 39 | + spdx_txt_url = _spdx_txt_base_url + 'v' + version + '/text/' + 'deprecated_' + lic['licenseId'] + '.txt' |
| 40 | + else: |
| 41 | + spdx_txt_url = _spdx_txt_base_url + 'v' + version + '/text/' + lic['licenseId'] + '.txt' |
| 42 | + |
| 43 | + with urlopen(spdx_txt_url) as url: |
| 44 | + licenseText = url.read().decode('utf-8') |
| 45 | + tmp_lic["defaultText"] = licenseText |
| 46 | + |
| 47 | + frequentLicenses[_frequentLicenses_key].append(tmp_lic) |
| 48 | + |
| 49 | + except Exception as e: |
| 50 | + success = False |
| 51 | + error_msg = 'Failed to open and parse ' + licenses_file |
| 52 | + print(error_msg, ": ", e) |
| 53 | + print(traceback.format_exc()) |
| 54 | + |
| 55 | + return frequentLicenses, success, error_msg |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + _frequencyLicense = 'frequentLicenselist.json' |
| 60 | + frequentLicenses, success, error_msg = create_frequentlicenses() |
| 61 | + if success: |
| 62 | + with open(_frequencyLicense, 'w', encoding='utf-8') as f: |
| 63 | + json.dump(frequentLicenses, f, sort_keys=True, indent=4) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + main() |
0 commit comments