Updated hook to set permissions in "var" directory 0.1.2
authorThierry Florac <thierry.florac@onf.fr>
Fri, 02 Jun 2017 16:01:13 +0200
changeset 6 ad4d84ea212e
parent 5 08b2ca72b082
child 7 f35ba3000688
Updated hook to set permissions in "var" directory
hooks/post_gen_project.py
--- a/hooks/post_gen_project.py	Fri Jun 02 15:58:39 2017 +0200
+++ b/hooks/post_gen_project.py	Fri Jun 02 16:01:13 2017 +0200
@@ -10,14 +10,14 @@
 # Replace "$((INSTALL))" by install directory for all files in '/etc'
 #
 
-target = os.getcwd()
+TARGET = os.getcwd()
 
-for root, dirs, files in os.walk(os.path.join(target, 'etc')):
+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)
+        content = content.replace('$((INSTALL))', TARGET)
         with open(os.path.join(root, filename), 'w') as f:
             f.write(content)
 
@@ -26,14 +26,41 @@
 # 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):
-    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)
+        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 dirs:
+        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))