From 9a39937b0c3615a89b67e8dc4d86fdd35c488c24 Mon Sep 17 00:00:00 2001 From: alexzvk Date: Mon, 28 Nov 2022 16:22:03 -0500 Subject: [PATCH] stored licenses to filename mapping in json file --- hooks/post_gen_project.py | 5 +++-- licenses.json | 1 + scripts/update_licenses.py | 9 +++++---- 3 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 licenses.json diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index fbd61b82..90f9f7fb 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -14,8 +14,7 @@ import os import random import shutil import string -os.chdir('../scripts/') -from update_licenses import titles_dict +import json try: # Inspired by @@ -332,6 +331,8 @@ def handle_licenses(): with open(os.path.join("licenses", "-temporary-placeholder.txt")) as f: selected_title = f.readline() + with open('../licenses.json', 'r') as f: + titles_dict = json.load(f) # access the title to filename dictionary to find the correct file # using a dictionary instead of looping reduces time complexity with open(os.path.join("licenses", titles_dict[selected_title])) as f: diff --git a/licenses.json b/licenses.json new file mode 100644 index 00000000..aa50ecd2 --- /dev/null +++ b/licenses.json @@ -0,0 +1 @@ +{"BSD Zero Clause License": "0bsd.txt", "Academic Free License v3.0": "afl-3.0.txt", "GNU Affero General Public License v3.0": "agpl-3.0.txt", "Apache License 2.0": "apache-2.0.txt", "Artistic License 2.0": "artistic-2.0.txt", "BSD 2-Clause \"Simplified\" License": "bsd-2-clause.txt", "BSD 3-Clause Clear License": "bsd-3-clause-clear.txt", "BSD 3-Clause \"New\" or \"Revised\" License": "bsd-3-clause.txt", "BSD 4-Clause \"Original\" or \"Old\" License": "bsd-4-clause.txt", "Boost Software License 1.0": "bsl-1.0.txt", "Creative Commons Attribution 4.0 International": "cc-by-4.0.txt", "Creative Commons Attribution Share Alike 4.0 International": "cc-by-sa-4.0.txt", "Creative Commons Zero v1.0 Universal": "cc0-1.0.txt", "CeCILL Free Software License Agreement v2.1": "cecill-2.1.txt", "CERN Open Hardware Licence Version 2 - Permissive": "cern-ohl-p-2.0.txt", "CERN Open Hardware Licence Version 2 - Strongly Reciprocal": "cern-ohl-s-2.0.txt", "CERN Open Hardware Licence Version 2 - Weakly Reciprocal": "cern-ohl-w-2.0.txt", "Educational Community License v2.0": "ecl-2.0.txt", "Eclipse Public License 1.0": "epl-1.0.txt", "Eclipse Public License 2.0": "epl-2.0.txt", "European Union Public License 1.1": "eupl-1.1.txt", "European Union Public License 1.2": "eupl-1.2.txt", "GNU Free Documentation License v1.3": "gfdl-1.3.txt", "GNU General Public License v2.0": "gpl-2.0.txt", "GNU General Public License v3.0": "gpl-3.0.txt", "ISC License": "isc.txt", "GNU Lesser General Public License v2.1": "lgpl-2.1.txt", "GNU Lesser General Public License v3.0": "lgpl-3.0.txt", "LaTeX Project Public License v1.3c": "lppl-1.3c.txt", "MIT No Attribution": "mit-0.txt", "MIT License": "mit.txt", "Mozilla Public License 2.0": "mpl-2.0.txt", "Microsoft Public License": "ms-pl.txt", "Microsoft Reciprocal License": "ms-rl.txt", "Mulan Permissive Software License, Version 2": "mulanpsl-2.0.txt", "University of Illinois/NCSA Open Source License": "ncsa.txt", "Open Data Commons Open Database License v1.0": "odbl-1.0.txt", "SIL Open Font License 1.1": "ofl-1.1.txt", "Open Software License 3.0": "osl-3.0.txt", "PostgreSQL License": "postgresql.txt", "The Unlicense": "unlicense.txt", "Universal Permissive License v1.0": "upl-1.0.txt", "Vim License": "vim.txt", "\"Do What The F*ck You Want To Public License\"": "wtfpl.txt", "zlib License": "zlib.txt"} \ No newline at end of file diff --git a/scripts/update_licenses.py b/scripts/update_licenses.py index 282aa761..f22927f0 100644 --- a/scripts/update_licenses.py +++ b/scripts/update_licenses.py @@ -8,9 +8,7 @@ from github import Github CURRENT_FILE = Path(__file__) ROOT = CURRENT_FILE.parents[1] GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None) - -# dictionary declared here to be exported to post_gen_project, for faster license access -titles_dict = {} +GITHUB_TOKEN = "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" def main() -> None: """ @@ -19,7 +17,6 @@ def main() -> None: repo = Github(login_or_token=GITHUB_TOKEN).get_repo("github/choosealicense.com") license_dir = ROOT / "{{cookiecutter.project_slug}}" / "licenses" license_dir.mkdir(exist_ok=True) - global titles_dict for file in repo.get_contents("_licenses", "gh-pages"): content = codecs.decode(file.decoded_content) # make below line into a dictionary mapping to filename @@ -29,6 +26,10 @@ def main() -> None: if not path.is_file(): path.touch() (license_dir / file.name).write_text(replace_content_options(content)) + + # write the titles dictionary to a json file and put it in workflows so it can be accessed by other files + with open('licenses.json', 'w') as licenses_dict: + licenses_dict.write(json.dumps(titles_dict)) # Put "Not open source" at front so people know it's an option front_options = [ "Not open source",