""" Module called at startup to setting-up some applications and folders """ import os import sqlite3 from django.conf import settings from cryptography.fernet import Fernet from tools.drivers.driver import Drivers from tools.system_tools import dprint RM_STRING = 'rm -r {}' def run(): """ Main method running at startup Returns ------- """ # Creating needed folders if it not exists base_dir = settings.BASE_DIR # Static directories __create_if_not_exists(os.path.join(base_dir, 'static')) __create_if_not_exists(os.path.join(base_dir, 'static', 'GMS')) __create_if_not_exists(os.path.join(base_dir, 'static', 'GMS', 'DEM')) __create_if_not_exists(os.path.join(base_dir, 'static_root')) __create_if_not_exists(os.path.join(base_dir, 'docs', 'static')) __create_if_not_exists(os.path.join(base_dir, 'docs', 'static', 'docs')) # # Temp files # Remove all os.system('rm -r tmp/*') os.system(RM_STRING.format(os.path.join(base_dir, 'GMS', 'raster', 'tmp'))) os.system(RM_STRING.format(os.path.join(base_dir, 'MG', 'update', 'StaticBase', 'tmp'))) __create_if_not_exists(os.path.join(base_dir, 'GMS', 'raster', 'tmp')) __create_if_not_exists(os.path.join(base_dir, 'MG', 'update', 'StaticBase', 'tmp')) # Check driver implementation Drivers().check_drivers() # Initializing authentication app if not settings.DEBUG: os.system(RM_STRING.format(os.path.join(base_dir, 'MG', 'authenticate', 'certs'))) __create_if_not_exists(os.path.join(base_dir, 'MG', 'authenticate', 'certs')) if not settings.DEBUG or not os.path.exists(settings.CERTS_DB_PATH): os.system(RM_STRING.format(settings.CERTS_DB_PATH)) conn = sqlite3.connect(settings.CERTS_DB_PATH) # Users conn.execute('CREATE TABLE IF NOT EXISTS users (name TEXT PRIMARY KEY,' 'GDriveID TEXT, DropboxID TEXT)') # Google drive protocol conn.execute('CREATE TABLE IF NOT EXISTS gdrive (id TEXT PRIMARY KEY,' 'email TEXT NOT NULL, user_id TEXT NOT NULL, cred TEXT NOT NULL)') # Dropbox protocol conn.execute('CREATE TABLE IF NOT EXISTS dropbox (id TEXT PRIMARY KEY,' 'user_id TEXT NOT NULL, account_id TEXT NOT NULL, scope TEXT NOT NULL,' 'cred TEST NOT NULL)') conn.commit() conn.close() create_key = False if not settings.DEBUG or not os.path.exists(settings.CIPHER_KEY): create_key = True if create_key: with open(settings.CIPHER_KEY, 'wb') as key_file: key_file.write(Fernet.generate_key()) def __create_if_not_exists(path): """ Method to create a folder if it does not exists Parameters ---------- path: str Path of the folder """ if os.path.exists(path): dprint('{} exists, not created'.format(path)) else: os.mkdir(path) dprint('{} created'.format(path))