From dbfea3149feb041f841c693885552aa5a134a663 Mon Sep 17 00:00:00 2001
From: James Curtin <jameswcurtin@gmail.com>
Date: Sun, 3 Nov 2019 08:24:11 -0500
Subject: [PATCH] Update cookiecutter hooks and python versions

---
 hooks/post_gen_project.py            |  9 +++++---
 hooks/pre_gen_project.py             | 27 ++++++++++++++++-------
 tasks.py                             | 33 ++++++++++------------------
 {{cookiecutter.app_name}}/Dockerfile |  3 ++-
 4 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py
index 08be14f9..f6df3bd2 100644
--- a/hooks/post_gen_project.py
+++ b/hooks/post_gen_project.py
@@ -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__":
diff --git a/hooks/pre_gen_project.py b/hooks/pre_gen_project.py
index cb782f2d..26c1340a 100644
--- a/hooks/pre_gen_project.py
+++ b/hooks/pre_gen_project.py
@@ -1,6 +1,8 @@
+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__":
diff --git a/tasks.py b/tasks.py
index 5503af6c..0e77b881 100644
--- a/tasks.py
+++ b/tasks.py
@@ -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")
diff --git a/{{cookiecutter.app_name}}/Dockerfile b/{{cookiecutter.app_name}}/Dockerfile
index c0fe84ca..610b684d 100644
--- a/{{cookiecutter.app_name}}/Dockerfile
+++ b/{{cookiecutter.app_name}}/Dockerfile
@@ -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 -
-- 
GitLab