From 6cc921139012969c3ec6c69fe29d802c76f212fe Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Tue, 12 Dec 2017 15:31:37 +0100 Subject: [PATCH] added pre_gen hook with additional checks --- cookiecutter.json | 2 +- hooks/pre_gen_project.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 hooks/pre_gen_project.py diff --git a/cookiecutter.json b/cookiecutter.json index fe8f086..7787efd 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -1,6 +1,6 @@ { "new_wiki": "", - "wiki_name": "{{ cookiecutter.new_wiki|title|replace(' ', '')|replace('Wiki', '') }}", + "wiki_name": "{{ cookiecutter.new_wiki|title|replace(' ', '') }}", "directory_name": "{{ cookiecutter.wiki_name|lower }}", "url": "https://{{cookiecutter.directory_name}}.cpi.imtek.uni-freiburg.de/" } diff --git a/hooks/pre_gen_project.py b/hooks/pre_gen_project.py new file mode 100644 index 0000000..d398a9f --- /dev/null +++ b/hooks/pre_gen_project.py @@ -0,0 +1,25 @@ +import re +import sys +from pathlib import Path + +MOIN_PATH = '/var/www/moin/wikis' +MODULE_REGEX = r'^[_a-zA-Z][_a-zA-Z0-9]+$' + +module_name = '{{ cookiecutter.directory_name }}' + +if not re.match(MODULE_REGEX, module_name): + print('ERROR: %s is not a valid Python module name!' % module_name) + sys.exit(1) + +try: + import {{ cookiecutter.directory_name }} + print('ERROR: %s is an existing Python module!' % module_name) + sys.exit(2) +except ModuleNotFoundError: + pass + + +wiki_path = Path(MOIN_PATH) / module_name +if wiki_path.exists(): + print('ERROR: wiki %s already exists!' % module_name) + sys.exit(1)