Skip to content

Commit

Permalink
Fix getting license in post_gen hook
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Andrew-Chen-Wang committed Feb 27, 2021
1 parent a33be2a commit 8666291
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
11 changes: 8 additions & 3 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 2 additions & 0 deletions tests/test_cookiecutter_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ cookiecutter.open_source_license }}

0 comments on commit 8666291

Please sign in to comment.