hooks/post_gen_project.py
changeset 0 10812b2ba33a
child 2 01a55c704ea1
equal deleted inserted replaced
-1:000000000000 0:10812b2ba33a
       
     1 #!/usr/bin/env python3.5
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 import os
       
     5 
       
     6 from grp import getgrnam
       
     7 from pwd import getpwnam
       
     8 
       
     9 
       
    10 #
       
    11 # Replace "$((INSTALL))" by install directory for all files
       
    12 #
       
    13 
       
    14 TARGET = os.getcwd()
       
    15 
       
    16 for root, dirs, files in os.walk(TARGET):
       
    17     for filename in files:
       
    18         # read file content
       
    19         with open(os.path.join(root, filename)) as f:
       
    20             content = f.read()
       
    21         # replace tag by install path
       
    22         content = content.replace('$((INSTALL))', TARGET)
       
    23         # replace file content
       
    24         with open(os.path.join(root, filename), 'w') as f:
       
    25             f.write(content)
       
    26 
       
    27 
       
    28 #
       
    29 # Check for logs directory
       
    30 #
       
    31 
       
    32 user = '{{ cookiecutter.run_user }}'
       
    33 user_id = getpwnam(user).pw_uid
       
    34 
       
    35 group = '{{ cookiecutter.run_group }}'
       
    36 group_id = getgrnam(group).gr_gid
       
    37 
       
    38 
       
    39 LOGS_DIRECTORY = '{{ cookiecutter.logs_directory }}'
       
    40 
       
    41 if not os.path.exists(LOGS_DIRECTORY):
       
    42     try:
       
    43         os.makedirs(LOGS_DIRECTORY, mode=0o775, exist_ok=True)
       
    44     except PermissionError:
       
    45         print("WARNING: Can't create logs directory {0}".format(LOGS_DIRECTORY))
       
    46     else:
       
    47         os.chown(LOGS_DIRECTORY, user_id, group_id)
       
    48 
       
    49 
       
    50 #
       
    51 # Check for var directory
       
    52 #
       
    53 
       
    54 VAR_DIRECTORY = os.path.join(TARGET, 'var')
       
    55 
       
    56 os.chown(VAR_DIRECTORY, user_id, group_id)
       
    57 for root, dirs, files in os.walk(VAR_DIRECTORY):
       
    58     for dirname in dirs:
       
    59         try:
       
    60             target = os.path.join(VAR_DIRECTORY, root, dirname)
       
    61             os.chown(target, user_id, group_id)
       
    62         except PermissionError:
       
    63             print("WARNING: Can't set permissions for directory {0}".format(target))
       
    64     for filename in files:
       
    65         try:
       
    66             target = os.path.join(VAR_DIRECTORY, root, filename)
       
    67             os.chown(target, user_id, group_id)
       
    68         except PermissionError:
       
    69             print("WARNING: Can't set permissions for file {0}".format(target))
       
    70 
       
    71 
       
    72 #
       
    73 # Make binary scripts executable
       
    74 #
       
    75 
       
    76 BIN_DIRECTORY = os.path.join(TARGET, 'bin')
       
    77 
       
    78 for root, dirs, files in os.walk(VAR_DIRECTORY):
       
    79     for filename in files:
       
    80         try:
       
    81             target = os.path.join(BIN_DIRECTORY, root, filename)
       
    82             os.chmod(target, 0o775)
       
    83         except PermissionError:
       
    84             print("WARNING: Can't set permissions for file {0}".format(target))
       
    85 
       
    86 
       
    87 print("\nYour ZEO environment is initialized.")
       
    88 print("To finalize it's creation, just type:")
       
    89 print("- cd {{ cookiecutter.project_slug }}")
       
    90 print("- python3.5 bootstrap.py")
       
    91 print("- ./bin/buildout")
       
    92 
       
    93 {%- if cookiecutter.use_zeo_auth %}
       
    94 #
       
    95 # Initialize ZEO authentication file
       
    96 #
       
    97 
       
    98 cmd = os.path.join(TARGET, 'bin', 'zeopasswd')
       
    99 target = os.path.join(TARGET, 'etc', 'auth.db')
       
   100 print("\nTo initialize authentication database, please run following command after buildout:")
       
   101 print('{cmd} -f {target} -p digest -r "{{ cookiecutter.project_name }}" {{ cookiecutter.zeo_auth_user }} '
       
   102       '{{ cookiecutter.zeo_auth_password }}'.format(cmd=cmd, target=target))
       
   103 {%- endif %}