diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 90f9f7fba..6927f3804 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -10,11 +10,11 @@ TODO: restrict Cookiecutter Django project initialization to """ from __future__ import print_function +import json import os import random import shutil import string -import json try: # Inspired by @@ -328,22 +328,28 @@ def handle_licenses(): "GNU Lesser General Public License v3.0": "COPYING.LESSER", "The Unlicense": "UNLICENSE", } - with open(os.path.join("licenses", "-temporary-placeholder.txt")) as f: - selected_title = f.readline() - with open('../licenses.json', 'r') as f: + selected_title = """{{ cookiecutter.open_source_license }}""" + + if selected_title == "Not open source": + os.remove("CONTRIBUTORS.txt") + shutil.rmtree("licenses") + return + + with open(os.path.join("licenses", "licenses.json")) 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: contents = f.readlines() - with open(special_license_files.get(titles_dict[selected_title], "LICENSE"), "w") as f: + with open( + special_license_files.get(titles_dict[selected_title], "LICENSE"), "w" + ) as f: # +2 to get rid of the --- and and an extra new line - f.writelines(contents[contents.index("---\n", 1) + 2:]) + i = contents.index("---\n", 1) + 2 + f.writelines(contents[i:]) - if selected_title == "Not open source": - os.remove("CONTRIBUTORS.txt") shutil.rmtree("licenses") diff --git a/scripts/check_licenses.py b/scripts/check_licenses.py new file mode 100644 index 000000000..d14b84e4e --- /dev/null +++ b/scripts/check_licenses.py @@ -0,0 +1,41 @@ +import os +import re +from pprint import pprint + + +# script to check if licenses generated have placeholders not replaced +def check_scripts_for_placeholders(): + brackets = [] + for filename in os.listdir("../{{cookiecutter.project_slug}}/licenses"): + file = open( + "../{{cookiecutter.project_slug}}/licenses/" + filename, encoding="utf8" + ) + + # 'found' stores all found bracket instances + found = [] + + # dashes counts the '---\n' lines in the licenses. + # it skips instances of brackets until after 2 as to skip the jekyll header + dashes = 0 + for i, line in enumerate(file.readlines()): + if line == "---\n": + dashes += 1 + # skips any possible brackets until the jekyll header is skipped + if dashes < 2: + continue + line = re.findall(r"\[.*\]", line) + if line != []: + found += (i, line) + + # add any found instances of placeholders to the brackets array + # print it after the loop is executed + if found != []: + brackets += (filename, found) + if len(brackets) > 0: + print() + pprint(brackets) + assert len(brackets) == 0 + + +if __name__ == "__main__": + check_scripts_for_placeholders() diff --git a/scripts/update_licenses.py b/scripts/update_licenses.py index 66146e110..562c31300 100644 --- a/scripts/update_licenses.py +++ b/scripts/update_licenses.py @@ -3,12 +3,14 @@ import json import os import re from pathlib import Path + from github import Github CURRENT_FILE = Path(__file__) ROOT = CURRENT_FILE.parents[1] GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None) + def main() -> None: """ Script entry point. @@ -20,15 +22,18 @@ def main() -> None: for file in repo.get_contents("_licenses", "gh-pages"): content = codecs.decode(file.decoded_content) # make below line into a dictionary mapping to filename - titles_dict[content.split("\n", maxsplit=2)[1].replace("title: ", "")] = file.name - #titles.append(content.split("\n", maxsplit=2)[1].replace("title: ", "")) + titles_dict[ + content.split("\n", maxsplit=2)[1].replace("title: ", "") + ] = file.name path = license_dir / file.name 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: + # write the titles dictionary to a json file so it can be accessed by other files + with open( + "{{cookiecutter.project_slug}}/licenses/licenses.json", "w" + ) as licenses_dict: json.dump(titles_dict, licenses_dict, indent=2) # Put "Not open source" at front so people know it's an option front_options = [ @@ -60,7 +65,7 @@ def update_cookiecutter(titles: list): with open("cookiecutter.json") as f: data = json.load(f) data["open_source_license"] = titles - with open("cookiecutter.json", "wt") as f: + with open("cookiecutter.json", "w") as f: json.dump(data, f, indent=2) diff --git a/tests/test_licenses.py b/tests/test_licenses.py deleted file mode 100644 index a501de208..000000000 --- a/tests/test_licenses.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Unit tests for licenses""" -import os -import re -from pprint import pprint -from pathlib import Path - -import pytest - - -# script to check if licenses generated have placeholders not replaced with cookiecutter rendering -def test_scripts_for_placeholders(): - brackets = [] - for filename in os.listdir('../{{cookiecutter.project_slug}}/licenses'): - file = open('../{{cookiecutter.project_slug}}/licenses/' + filename, 'r', encoding="utf8") - - # 'found' stores all found bracket instances - found = [] - # dashes counts the '---\n' lines in the licenses. it skips instances of brackets until after the first 2 as to - # skip the jekyll header - dashes = 0 - for i, line in enumerate(file.readlines()): - if line == '---\n': - dashes += 1 - # skips any possible brackets until the jekyll header is skipped - if dashes < 2: - continue - line = re.findall(r'\[.*\]', line) - if line != []: - found += (i, line) - - # add any found instaces of bracket placeholders to the brackets array, and print it after the loop is executed - if found != []: - brackets += (filename, found) - if len(brackets) > 0: - print() - pprint(brackets) - assert(len(brackets) == 0) diff --git a/{{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt b/{{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt new file mode 100644 index 000000000..f5be03a8c --- /dev/null +++ b/{{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt @@ -0,0 +1 @@ +{{ cookiecutter.open_source_license }} diff --git a/licenses.json b/{{cookiecutter.project_slug}}/licenses/licenses.json similarity index 100% rename from licenses.json rename to {{cookiecutter.project_slug}}/licenses/licenses.json