hooks/post_gen_project.py
changeset 0 e4f314a62bbf
child 6 ad4d84ea212e
equal deleted inserted replaced
-1:000000000000 0:e4f314a62bbf
       
     1 #!/usr/bin/env python3.5
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 import os
       
     5 from grp import getgrnam
       
     6 from pwd import getpwnam
       
     7 
       
     8 
       
     9 #
       
    10 # Replace "$((INSTALL))" by install directory for all files in '/etc'
       
    11 #
       
    12 
       
    13 target = os.getcwd()
       
    14 
       
    15 for root, dirs, files in os.walk(os.path.join(target, 'etc')):
       
    16     # read file content
       
    17     for filename in files:
       
    18         with open(os.path.join(root, filename)) as f:
       
    19             content = f.read()
       
    20         content = content.replace('$((INSTALL))', target)
       
    21         with open(os.path.join(root, filename), 'w') as f:
       
    22             f.write(content)
       
    23 
       
    24 
       
    25 #
       
    26 # Check for logs directory
       
    27 #
       
    28 
       
    29 LOGS_DIRECTORY = '{{ cookiecutter.logs_directory }}'
       
    30 
       
    31 if not os.path.exists(LOGS_DIRECTORY):
       
    32     user = '{{ cookiecutter.run_user }}'
       
    33     group = '{{ cookiecutter.run_group }}'
       
    34     try:
       
    35         os.mkdir(LOGS_DIRECTORY, mode=0o775)
       
    36     except PermissionError:
       
    37         print("WARNING: Can't create logs directory {0}".format(LOGS_DIRECTORY))
       
    38     else:
       
    39         os.chown(LOGS_DIRECTORY, getpwnam(user).pw_uid, getgrnam(group).gr_gid)