import json import os import pathlib import re import shutil DEFAULT = '\033[0m' class Constants: sip_trunk_operators = ["default", "tmobile", "systegra"] class Settings: output_directory = None def print_grey(*values): print('\033[37m', *values, DEFAULT) def print_red(*values): print('\033[91m', *values, DEFAULT) def print_black_light(*values): print('\033[90m', *values, DEFAULT) def print_yellow(*values): print('\033[93m', *values, DEFAULT) def print_green(*values): print('\033[92m', *values, DEFAULT) def get_minimal_configuration(): with open(os.path.join(get_templates_path(), "basic_indexes_tenant_data.json"), 'rb') as file: tenant_data = json.load(file) return tenant_data def get_templates_path(): current_folder = pathlib.Path(__file__).parent.resolve() return os.path.join(current_folder, "templates") def get_output_path(): if Settings.output_directory: return Settings.output_directory current_folder = pathlib.Path(__file__).parent.resolve() return os.path.join(current_folder, "output") def copy_file(src_file, dest_dir): dst = shutil.copy(src_file, dest_dir) # CRLF TO LF LINE ENDING: with open(dst, 'rb') as open_file: content = open_file.read() # Windows ➡ Unix content = content.replace(b'\r\n', b'\n') with open(dst, 'wb') as open_file: open_file.write(content) def copy_directory(source_dir, destination_dir): """ Copies the entire contents of 'source_dir' to 'destination_dir'. Both the directories and files inside are copied. If 'destination_dir' does not exist, it will be created. """ # Check if the source directory exists if not os.path.exists(source_dir): print_red(f"The source directory {source_dir} does not exist.") return # Check if the destination directory already exists if os.path.exists(destination_dir): print_red(f"The destination directory {destination_dir} already exists.") return # Copy the directory try: shutil.copytree(source_dir, destination_dir) print_black_light(f"Directory copied from {source_dir} to {destination_dir}") except Exception as e: print_red(f"Failed to copy directory: {e}") def basic_on_location_sed(inputfile, replaced_variable, new_variable): file = open(inputfile, 'r') output_text = file.read() output_text = re.sub(replaced_variable, new_variable, output_text) with open(inputfile, "w") as file: file.write(output_text) def get_token(name): token_path = os.path.join(get_output_path(), "tokens", name) # Check if the destination directory already exists if not os.path.exists(token_path): print_red(f"The token {token_path} not exists.") return file = open(token_path, 'r') return str(file.read())