From 86662917c93fc394bc94325ca070d20575aa0f8a Mon Sep 17 00:00:00 2001 From: Andrew-Chen-Wang Date: Sat, 27 Feb 2021 01:41:26 -0500 Subject: [PATCH] Fix getting license in post_gen hook * The solution? Put it in some random txt file in the license folder and read it in. You can't use """""" or "" or '' without some kind of error. By reading in the text instead, we completely avoid this. Signed-off-by: Andrew-Chen-Wang --- hooks/post_gen_project.py | 11 ++++++++--- tests/test_cookiecutter_generation.py | 2 ++ .../licenses/-temporary-placeholder.txt | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 04cbeab379..90ae2b74a6 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -319,18 +319,23 @@ 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() for filename in os.listdir("licenses"): + if filename == "-temporary-placeholder.txt": + continue # You'll always see: '---\n' marking beginning + end of Jekyll format with open(os.path.join("licenses", filename)) as f: contents = f.readlines() - title = contents[1].replace("title: ", "").replace("\n", "") - if title != "{{ cookiecutter.open_source_license }}": + title = contents[1].replace("title: ", "").rstrip() + if title != selected_title: continue with open(special_license_files.get(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 :]) break - if "{{ cookiecutter.open_source_license }}" == "Not open source": + + if selected_title == "Not open source": os.remove("CONTRIBUTORS.txt") shutil.rmtree("licenses") diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 96c6aced60..3fdaa2e24d 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -29,6 +29,8 @@ def generate_license_file_titles(): directory = os.path.join("{{cookiecutter.project_slug}}", "licenses") titles = [] for file in os.listdir(directory): + if file == "-temporary-placeholder.txt": + continue with open(os.path.join(directory, file)) as f: titles.append(f.readlines()[1].replace("title: ", "").replace("\n", "")) return titles diff --git a/{{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt b/{{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt new file mode 100644 index 0000000000..f5be03a8c4 --- /dev/null +++ b/{{cookiecutter.project_slug}}/licenses/-temporary-placeholder.txt @@ -0,0 +1 @@ +{{ cookiecutter.open_source_license }}