hooks/post_gen_project.py
changeset 0 10812b2ba33a
child 2 01a55c704ea1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hooks/post_gen_project.py	Tue Jun 06 15:09:45 2017 +0200
@@ -0,0 +1,103 @@
+#!/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
+#
+
+TARGET = os.getcwd()
+
+for root, dirs, files in os.walk(TARGET):
+    for filename in files:
+        # read file content
+        with open(os.path.join(root, filename)) as f:
+            content = f.read()
+        # replace tag by install path
+        content = content.replace('$((INSTALL))', TARGET)
+        # replace file content
+        with open(os.path.join(root, filename), 'w') as f:
+            f.write(content)
+
+
+#
+# Check for logs directory
+#
+
+user = '{{ cookiecutter.run_user }}'
+user_id = getpwnam(user).pw_uid
+
+group = '{{ cookiecutter.run_group }}'
+group_id = getgrnam(group).gr_gid
+
+
+LOGS_DIRECTORY = '{{ cookiecutter.logs_directory }}'
+
+if not os.path.exists(LOGS_DIRECTORY):
+    try:
+        os.makedirs(LOGS_DIRECTORY, mode=0o775, exist_ok=True)
+    except PermissionError:
+        print("WARNING: Can't create logs directory {0}".format(LOGS_DIRECTORY))
+    else:
+        os.chown(LOGS_DIRECTORY, user_id, group_id)
+
+
+#
+# Check for var directory
+#
+
+VAR_DIRECTORY = os.path.join(TARGET, 'var')
+
+os.chown(VAR_DIRECTORY, user_id, group_id)
+for root, dirs, files in os.walk(VAR_DIRECTORY):
+    for dirname in dirs:
+        try:
+            target = os.path.join(VAR_DIRECTORY, root, dirname)
+            os.chown(target, user_id, group_id)
+        except PermissionError:
+            print("WARNING: Can't set permissions for directory {0}".format(target))
+    for filename in files:
+        try:
+            target = os.path.join(VAR_DIRECTORY, root, filename)
+            os.chown(target, user_id, group_id)
+        except PermissionError:
+            print("WARNING: Can't set permissions for file {0}".format(target))
+
+
+#
+# Make binary scripts executable
+#
+
+BIN_DIRECTORY = os.path.join(TARGET, 'bin')
+
+for root, dirs, files in os.walk(VAR_DIRECTORY):
+    for filename in files:
+        try:
+            target = os.path.join(BIN_DIRECTORY, root, filename)
+            os.chmod(target, 0o775)
+        except PermissionError:
+            print("WARNING: Can't set permissions for file {0}".format(target))
+
+
+print("\nYour ZEO environment is initialized.")
+print("To finalize it's creation, just type:")
+print("- cd {{ cookiecutter.project_slug }}")
+print("- python3.5 bootstrap.py")
+print("- ./bin/buildout")
+
+{%- if cookiecutter.use_zeo_auth %}
+#
+# Initialize ZEO authentication file
+#
+
+cmd = os.path.join(TARGET, 'bin', 'zeopasswd')
+target = os.path.join(TARGET, 'etc', 'auth.db')
+print("\nTo initialize authentication database, please run following command after buildout:")
+print('{cmd} -f {target} -p digest -r "{{ cookiecutter.project_name }}" {{ cookiecutter.zeo_auth_user }} '
+      '{{ cookiecutter.zeo_auth_password }}'.format(cmd=cmd, target=target))
+{%- endif %}