codekingpro/portable-devtools
115k
1##########################################################################2#3# pgAdmin 4 - PostgreSQL Tools4#5# Copyright (C) 2013 - 2024, The pgAdmin Development Team6# This software is released under the PostgreSQL Licence7#8##########################################################################9 10import os11import sys12import getpass13from pgadmin.utils.constants import KERBEROS14 15FAILED_CREATE_DIR = \16 "ERROR : Failed to create the directory {}:\n {}"17 18 19def _create_directory_if_not_exists(_path):20 if _path and not os.path.exists(_path):21 os.mkdir(_path)22 return True23 24 return False25 26 27def create_app_data_directory(config):28 """29 Create the required directories (if not present).30 """31 # Create the directory containing the configuration file (if not present).32 is_directory_created = False33 try:34 is_directory_created = _create_directory_if_not_exists(35 os.path.dirname(config.SQLITE_PATH))36 except PermissionError as e:37 print(FAILED_CREATE_DIR.format(os.path.dirname(config.SQLITE_PATH), e))38 print(39 "HINT : Create the directory {}, ensure it is writeable by\n"40 " '{}', and try again, or, create a config_local.py file\n"41 " and override the SQLITE_PATH setting per\n"42 " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".43 format(44 os.path.dirname(config.SQLITE_PATH),45 getpass.getuser(),46 config.APP_VERSION))47 sys.exit(1)48 49 # Try to set the permissions on the directory, but don't complain50 # if we can't. This may be the case on a mounted directory, e.g. in51 # OpenShift. We'll still secure the config database anyway.52 if os.name != 'nt' and is_directory_created:53 try:54 os.chmod(os.path.dirname(config.SQLITE_PATH), 0o700)55 except Exception as e:56 # The flask app isn't setup yet, so we can't use the logger57 print('WARNING: Failed to set ACL on the directory containing the '58 'configuration database:\n {}'.format(e))59 print("HINT : You may need to manually set the permissions on\n"60 " {} to allow {} to write to it.".61 format(os.path.dirname(config.SQLITE_PATH),62 getpass.getuser()))63 64 # Create the directory containing the log file (if not present).65 try:66 _create_directory_if_not_exists(os.path.dirname(config.LOG_FILE))67 except PermissionError as e:68 print(FAILED_CREATE_DIR.format(os.path.dirname(config.LOG_FILE), e))69 print(70 "HINT : Create the directory {}, ensure it is writeable by\n"71 " '{}', and try again, or, create a config_local.py file\n"72 " and override the LOG_FILE setting per\n"73 " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".74 format(75 os.path.dirname(config.LOG_FILE),76 getpass.getuser(),77 config.APP_VERSION))78 sys.exit(1)79 80 # Create the session directory (if not present).81 try:82 is_directory_created = \83 _create_directory_if_not_exists(config.SESSION_DB_PATH)84 except PermissionError as e:85 print(FAILED_CREATE_DIR.format(config.SESSION_DB_PATH, e))86 print(87 "HINT : Create the directory {}, ensure it is writeable by\n"88 " '{}', and try again, or, create a config_local.py file\n"89 " and override the SESSION_DB_PATH setting per\n"90 " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".91 format(92 config.SESSION_DB_PATH,93 getpass.getuser(),94 config.APP_VERSION))95 sys.exit(1)96 97 if os.name != 'nt' and is_directory_created:98 os.chmod(config.SESSION_DB_PATH, 0o700)99 100 # Create the storage directory (if not present).101 try:102 _create_directory_if_not_exists(config.STORAGE_DIR)103 except PermissionError as e:104 print(FAILED_CREATE_DIR.format(config.STORAGE_DIR, e))105 print(106 "HINT : Create the directory {}, ensure it is writable by\n"107 " '{}', and try again, or, create a config_local.py file\n"108 " and override the STORAGE_DIR setting per\n"109 " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".110 format(111 config.STORAGE_DIR,112 getpass.getuser(),113 config.APP_VERSION))114 sys.exit(1)115 116 # Create Azure Credential Cache directory (if not present).117 try:118 _create_directory_if_not_exists(config.AZURE_CREDENTIAL_CACHE_DIR)119 except PermissionError as e:120 print(FAILED_CREATE_DIR.format(config.AZURE_CREDENTIAL_CACHE_DIR, e))121 print(122 "HINT : Create the directory {}, ensure it is writable by\n"123 "'{}', and try again, or, create a config_local.py file\n"124 " and override the AZURE_CREDENTIAL_CACHE_DIR setting per\n"125 " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".126 format(127 config.AZURE_CREDENTIAL_CACHE_DIR,128 getpass.getuser(),129 config.APP_VERSION))130 sys.exit(1)131 132 # Create Kerberos Credential Cache directory (if not present).133 if config.SERVER_MODE and KERBEROS in config.AUTHENTICATION_SOURCES:134 try:135 _create_directory_if_not_exists(config.KERBEROS_CCACHE_DIR)136 except PermissionError as e:137 print(FAILED_CREATE_DIR.format(config.KERBEROS_CCACHE_DIR, e))138 print(139 "HINT : Create the directory {}, ensure it is writable by\n"140 "'{}', and try again, or, create a config_local.py file\n"141 " and override the KERBEROS_CCACHE_DIR setting per\n"142 " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".143 format(144 config.KERBEROS_CCACHE_DIR,145 getpass.getuser(),146 config.APP_VERSION))147 sys.exit(1)148 