hooks/post_gen_project.py
changeset 0 e4f314a62bbf
child 6 ad4d84ea212e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hooks/post_gen_project.py	Fri Jun 02 15:07:36 2017 +0200
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3.5
+# -*- coding: utf-8 -*-
+
+import os
+from grp import getgrnam
+from pwd import getpwnam
+
+
+#
+# Replace "$((INSTALL))" by install directory for all files in '/etc'
+#
+
+target = os.getcwd()
+
+for root, dirs, files in os.walk(os.path.join(target, 'etc')):
+    # read file content
+    for filename in files:
+        with open(os.path.join(root, filename)) as f:
+            content = f.read()
+        content = content.replace('$((INSTALL))', target)
+        with open(os.path.join(root, filename), 'w') as f:
+            f.write(content)
+
+
+#
+# Check for logs directory
+#
+
+LOGS_DIRECTORY = '{{ cookiecutter.logs_directory }}'
+
+if not os.path.exists(LOGS_DIRECTORY):
+    user = '{{ cookiecutter.run_user }}'
+    group = '{{ cookiecutter.run_group }}'
+    try:
+        os.mkdir(LOGS_DIRECTORY, mode=0o775)
+    except PermissionError:
+        print("WARNING: Can't create logs directory {0}".format(LOGS_DIRECTORY))
+    else:
+        os.chown(LOGS_DIRECTORY, getpwnam(user).pw_uid, getgrnam(group).gr_gid)