Skip to content
Snippets Groups Projects
Commit dbfea314 authored by James Curtin's avatar James Curtin Committed by James Curtin
Browse files

Update cookiecutter hooks and python versions

parent 524b8ada
No related branches found
No related tags found
No related merge requests found
......@@ -2,10 +2,13 @@
# -*- coding: utf-8 -*-
"""Post gen hook to ensure that the generated project
has only one package management, either pipenv or pip."""
import logging
import os
import shutil
import sys
LOGGER = logging.getLogger()
def clean_extra_package_management_files():
"""Removes either requirements files and folder or the Pipfile."""
......@@ -27,10 +30,10 @@ def clean_extra_package_management_files():
os.remove(file_or_dir)
else:
shutil.rmtree(file_or_dir)
sys.exit(0)
except OSError as e:
sys.stdout.write("While attempting to remove file(s) an error occurred")
sys.stdout.write("Error: {}".format(e))
LOGGER.warning("While attempting to remove file(s) an error occurred")
LOGGER.warning(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
......
import logging
import re
import sys
LOGGER = logging.getLogger()
MODULE_REGEX = r"^[_a-zA-Z][_a-zA-Z0-9]+$"
......@@ -10,17 +12,26 @@ class bcolors:
BOLD = "\033[1m"
def colorize(escape_code, text):
code = getattr(bcolors, escape_code)
return f"{code}{text}{bcolors.ENDC}"
def log_warning(module_name):
warning = (
f"\n{colorize('WARNING', 'WARNING:')} {colorize('BOLD', module_name)}"
" is not a valid Python module name!\n"
"See https://www.python.org/dev/peps/pep-0008/#package-and-module-names"
" for naming standards.\n"
)
LOGGER.warning(warning)
def validate_python_module_name():
module_name = "{{ cookiecutter.app_name }}"
if not re.match(MODULE_REGEX, module_name):
print(
(
"\n{0}ERROR:{1} "
+ "{2}{3}{1} is not a valid Python module name!\n"
+ "See https://www.python.org/dev/peps/pep-0008/#package-and-module-names for naming standards.\n"
).format(bcolors.WARNING, bcolors.ENDC, bcolors.BOLD, module_name)
)
sys.exit(1)
log_warning(module_name)
raise ValueError
if __name__ == "__main__":
......
......@@ -19,16 +19,23 @@ REQUIREMENTS = os.path.join(COOKIE, "requirements", "dev.txt")
def _run_npm_command(ctx, command):
os.chdir(COOKIE)
ctx.run("npm {0}".format(command), echo=True)
ctx.run(f"npm {command}", echo=True)
os.chdir(HERE)
def _run_flask_command(ctx, command, *args):
os.chdir(COOKIE)
flask_command = f"flask {command}"
if args:
flask_command += f" {' '.join(args)}"
ctx.run(flask_command, echo=True)
@task
def build(ctx):
"""Build the cookiecutter."""
ctx.run("cookiecutter {0} --no-input".format(HERE))
ctx.run(f"cookiecutter {HERE} --no-input")
_run_npm_command(ctx, "install")
_run_npm_command(ctx, "run build")
@task
......@@ -36,23 +43,12 @@ def clean(ctx):
"""Clean out generated cookiecutter."""
if os.path.exists(COOKIE):
shutil.rmtree(COOKIE)
print("Removed {0}".format(COOKIE))
else:
print("App directory does not exist. Skipping.")
def _run_flask_command(ctx, command, *args):
os.chdir(COOKIE)
flask_command = "flask {0}".format(command)
if args:
flask_command = "{0} {1}".format(flask_command, " ".join(args))
ctx.run(flask_command, echo=True)
@task(pre=[clean, build])
def test(ctx):
"""Run lint commands and tests."""
ctx.run("pip install -r {0} --ignore-installed".format(REQUIREMENTS), echo=True)
ctx.run(f"pip install -r {REQUIREMENTS} --ignore-installed", echo=True)
_run_npm_command(ctx, "run lint")
os.chdir(COOKIE)
shutil.copyfile(os.path.join(COOKIE, ".env.example"), os.path.join(COOKIE, ".env"))
......@@ -60,10 +56,3 @@ def test(ctx):
os.environ["FLASK_DEBUG"] = "0"
_run_flask_command(ctx, "lint", "--check")
_run_flask_command(ctx, "test")
@task
def readme(ctx, browse=False):
ctx.run("rst2html.py README.rst > README.html")
if browse:
webbrowser.open_new_tab("README.html")
......@@ -4,7 +4,8 @@ FROM python:${INSTALL_PYTHON_VERSION}-slim-buster AS base
RUN apt-get update
RUN apt-get install -y \
curl
curl \
gcc
ARG INSTALL_NODE_VERSION=${INSTALL_NODE_VERSION:-12}
RUN curl -sL https://deb.nodesource.com/setup_${INSTALL_NODE_VERSION}.x | bash -
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment