mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-07-25 23:39:53 +03:00
commit
7fa6b94fa6
|
@ -10,11 +10,11 @@ TODO: restrict Cookiecutter Django project initialization to
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
import json
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Inspired by
|
# Inspired by
|
||||||
|
@ -328,22 +328,28 @@ def handle_licenses():
|
||||||
"GNU Lesser General Public License v3.0": "COPYING.LESSER",
|
"GNU Lesser General Public License v3.0": "COPYING.LESSER",
|
||||||
"The Unlicense": "UNLICENSE",
|
"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)
|
titles_dict = json.load(f)
|
||||||
# access the title to filename dictionary to find the correct file
|
# access the title to filename dictionary to find the correct file
|
||||||
# using a dictionary instead of looping reduces time complexity
|
# using a dictionary instead of looping reduces time complexity
|
||||||
with open(os.path.join("licenses", titles_dict[selected_title])) as f:
|
with open(os.path.join("licenses", titles_dict[selected_title])) as f:
|
||||||
contents = f.readlines()
|
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
|
# +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")
|
shutil.rmtree("licenses")
|
||||||
|
|
||||||
|
|
||||||
|
|
41
scripts/check_licenses.py
Normal file
41
scripts/check_licenses.py
Normal 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()
|
|
@ -3,12 +3,14 @@ import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from github import Github
|
from github import Github
|
||||||
|
|
||||||
CURRENT_FILE = Path(__file__)
|
CURRENT_FILE = Path(__file__)
|
||||||
ROOT = CURRENT_FILE.parents[1]
|
ROOT = CURRENT_FILE.parents[1]
|
||||||
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None)
|
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""
|
"""
|
||||||
Script entry point.
|
Script entry point.
|
||||||
|
@ -20,15 +22,18 @@ def main() -> None:
|
||||||
for file in repo.get_contents("_licenses", "gh-pages"):
|
for file in repo.get_contents("_licenses", "gh-pages"):
|
||||||
content = codecs.decode(file.decoded_content)
|
content = codecs.decode(file.decoded_content)
|
||||||
# make below line into a dictionary mapping to filename
|
# make below line into a dictionary mapping to filename
|
||||||
titles_dict[content.split("\n", maxsplit=2)[1].replace("title: ", "")] = file.name
|
titles_dict[
|
||||||
#titles.append(content.split("\n", maxsplit=2)[1].replace("title: ", ""))
|
content.split("\n", maxsplit=2)[1].replace("title: ", "")
|
||||||
|
] = file.name
|
||||||
path = license_dir / file.name
|
path = license_dir / file.name
|
||||||
if not path.is_file():
|
if not path.is_file():
|
||||||
path.touch()
|
path.touch()
|
||||||
(license_dir / file.name).write_text(replace_content_options(content))
|
(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
|
# write the titles dictionary to a json file so it can be accessed by other files
|
||||||
with open('licenses.json', 'w') as licenses_dict:
|
with open(
|
||||||
|
"{{cookiecutter.project_slug}}/licenses/licenses.json", "w"
|
||||||
|
) as licenses_dict:
|
||||||
json.dump(titles_dict, licenses_dict, indent=2)
|
json.dump(titles_dict, licenses_dict, indent=2)
|
||||||
# Put "Not open source" at front so people know it's an option
|
# Put "Not open source" at front so people know it's an option
|
||||||
front_options = [
|
front_options = [
|
||||||
|
@ -60,7 +65,7 @@ def update_cookiecutter(titles: list):
|
||||||
with open("cookiecutter.json") as f:
|
with open("cookiecutter.json") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
data["open_source_license"] = titles
|
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)
|
json.dump(data, f, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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)
|
|
|
@ -0,0 +1 @@
|
||||||
|
{{ cookiecutter.open_source_license }}
|
Loading…
Reference in New Issue
Block a user