Update cookiecutter hooks and python versions

master
James Curtin 5 years ago committed by James Curtin
parent 524b8ada30
commit dbfea3149f
  1. 9
      hooks/post_gen_project.py
  2. 27
      hooks/pre_gen_project.py
  3. 33
      tasks.py
  4. 3
      {{cookiecutter.app_name}}/Dockerfile

@ -2,10 +2,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Post gen hook to ensure that the generated project """Post gen hook to ensure that the generated project
has only one package management, either pipenv or pip.""" has only one package management, either pipenv or pip."""
import logging
import os import os
import shutil import shutil
import sys import sys
LOGGER = logging.getLogger()
def clean_extra_package_management_files(): def clean_extra_package_management_files():
"""Removes either requirements files and folder or the Pipfile.""" """Removes either requirements files and folder or the Pipfile."""
@ -27,10 +30,10 @@ def clean_extra_package_management_files():
os.remove(file_or_dir) os.remove(file_or_dir)
else: else:
shutil.rmtree(file_or_dir) shutil.rmtree(file_or_dir)
sys.exit(0)
except OSError as e: except OSError as e:
sys.stdout.write("While attempting to remove file(s) an error occurred") LOGGER.warning("While attempting to remove file(s) an error occurred")
sys.stdout.write("Error: {}".format(e)) LOGGER.warning(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":

@ -1,6 +1,8 @@
import logging
import re import re
import sys import sys
LOGGER = logging.getLogger()
MODULE_REGEX = r"^[_a-zA-Z][_a-zA-Z0-9]+$" MODULE_REGEX = r"^[_a-zA-Z][_a-zA-Z0-9]+$"
@ -10,17 +12,26 @@ class bcolors:
BOLD = "\033[1m" 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(): def validate_python_module_name():
module_name = "{{ cookiecutter.app_name }}" module_name = "{{ cookiecutter.app_name }}"
if not re.match(MODULE_REGEX, module_name): if not re.match(MODULE_REGEX, module_name):
print( log_warning(module_name)
( raise ValueError
"\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)
if __name__ == "__main__": if __name__ == "__main__":

@ -19,16 +19,23 @@ REQUIREMENTS = os.path.join(COOKIE, "requirements", "dev.txt")
def _run_npm_command(ctx, command): def _run_npm_command(ctx, command):
os.chdir(COOKIE) os.chdir(COOKIE)
ctx.run("npm {0}".format(command), echo=True) ctx.run(f"npm {command}", echo=True)
os.chdir(HERE) 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 @task
def build(ctx): def build(ctx):
"""Build the cookiecutter.""" """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, "install")
_run_npm_command(ctx, "run build")
@task @task
@ -36,23 +43,12 @@ def clean(ctx):
"""Clean out generated cookiecutter.""" """Clean out generated cookiecutter."""
if os.path.exists(COOKIE): if os.path.exists(COOKIE):
shutil.rmtree(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]) @task(pre=[clean, build])
def test(ctx): def test(ctx):
"""Run lint commands and tests.""" """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") _run_npm_command(ctx, "run lint")
os.chdir(COOKIE) os.chdir(COOKIE)
shutil.copyfile(os.path.join(COOKIE, ".env.example"), os.path.join(COOKIE, ".env")) 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" os.environ["FLASK_DEBUG"] = "0"
_run_flask_command(ctx, "lint", "--check") _run_flask_command(ctx, "lint", "--check")
_run_flask_command(ctx, "test") _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 update
RUN apt-get install -y \ RUN apt-get install -y \
curl curl \
gcc
ARG INSTALL_NODE_VERSION=${INSTALL_NODE_VERSION:-12} ARG INSTALL_NODE_VERSION=${INSTALL_NODE_VERSION:-12}
RUN curl -sL https://deb.nodesource.com/setup_${INSTALL_NODE_VERSION}.x | bash - RUN curl -sL https://deb.nodesource.com/setup_${INSTALL_NODE_VERSION}.x | bash -

Loading…
Cancel
Save