hooks/post_gen_project.py
changeset 6 ad4d84ea212e
parent 0 e4f314a62bbf
child 8 2025dd476ece
equal deleted inserted replaced
5:08b2ca72b082 6:ad4d84ea212e
     8 
     8 
     9 #
     9 #
    10 # Replace "$((INSTALL))" by install directory for all files in '/etc'
    10 # Replace "$((INSTALL))" by install directory for all files in '/etc'
    11 #
    11 #
    12 
    12 
    13 target = os.getcwd()
    13 TARGET = os.getcwd()
    14 
    14 
    15 for root, dirs, files in os.walk(os.path.join(target, 'etc')):
    15 for root, dirs, files in os.walk(os.path.join(TARGET, 'etc')):
    16     # read file content
    16     # read file content
    17     for filename in files:
    17     for filename in files:
    18         with open(os.path.join(root, filename)) as f:
    18         with open(os.path.join(root, filename)) as f:
    19             content = f.read()
    19             content = f.read()
    20         content = content.replace('$((INSTALL))', target)
    20         content = content.replace('$((INSTALL))', TARGET)
    21         with open(os.path.join(root, filename), 'w') as f:
    21         with open(os.path.join(root, filename), 'w') as f:
    22             f.write(content)
    22             f.write(content)
    23 
    23 
    24 
    24 
    25 #
    25 #
    26 # Check for logs directory
    26 # Check for logs directory
    27 #
    27 #
    28 
    28 
       
    29 user = '{{ cookiecutter.run_user }}'
       
    30 user_id = getpwnam(user).pw_uid
       
    31 
       
    32 group = '{{ cookiecutter.run_group }}'
       
    33 group_id = getgrnam(group).gr_gid
       
    34 
       
    35 
    29 LOGS_DIRECTORY = '{{ cookiecutter.logs_directory }}'
    36 LOGS_DIRECTORY = '{{ cookiecutter.logs_directory }}'
    30 
    37 
    31 if not os.path.exists(LOGS_DIRECTORY):
    38 if not os.path.exists(LOGS_DIRECTORY):
    32     user = '{{ cookiecutter.run_user }}'
       
    33     group = '{{ cookiecutter.run_group }}'
       
    34     try:
    39     try:
    35         os.mkdir(LOGS_DIRECTORY, mode=0o775)
    40         os.mkdir(LOGS_DIRECTORY, mode=0o775)
    36     except PermissionError:
    41     except PermissionError:
    37         print("WARNING: Can't create logs directory {0}".format(LOGS_DIRECTORY))
    42         print("WARNING: Can't create logs directory {0}".format(LOGS_DIRECTORY))
    38     else:
    43     else:
    39         os.chown(LOGS_DIRECTORY, getpwnam(user).pw_uid, getgrnam(group).gr_gid)
    44         os.chown(LOGS_DIRECTORY, user_id, group_id)
       
    45 
       
    46 
       
    47 #
       
    48 # Check for var directory
       
    49 #
       
    50 
       
    51 VAR_DIRECTORY = os.path.join(TARGET, 'var')
       
    52 
       
    53 os.chown(VAR_DIRECTORY, user_id, group_id)
       
    54 for root, dirs, files in os.walk(VAR_DIRECTORY):
       
    55     for dirname in dirs:
       
    56         try:
       
    57             target = os.path.join(VAR_DIRECTORY, root, dirname)
       
    58             os.chown(target, user_id, group_id)
       
    59         except PermissionError:
       
    60             print("WARNING: Can't set permissions for directory {0}".format(target))
       
    61     for filename in dirs:
       
    62         try:
       
    63             target = os.path.join(VAR_DIRECTORY, root, filename)
       
    64             os.chown(target, user_id, group_id)
       
    65         except PermissionError:
       
    66             print("WARNING: Can't set permissions for file {0}".format(target))