Merge pull request #2 from alexzvk/2941

2941
This commit is contained in:
alexzvk 2022-12-05 17:48:17 -05:00 committed by GitHub
commit 7fa6b94fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 50 deletions

View File

@ -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")

41
scripts/check_licenses.py Normal file
View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1 @@
{{ cookiecutter.open_source_license }}