def load_manager_data_full(self, data_to_load_path): logger().info("Performing full load of data") services_path = Path(join(str(data_to_load_path), "services.json")) with open(services_path, "r", encoding="utf-8") as infile: unloaded_services = json.loads(infile.read()) tasks_path = Path(join(str(data_to_load_path), "tasks.json")) with open(tasks_path, "r", encoding="utf-8") as infile: unloaded_tasks = json.loads(infile.read()) s = managersession() logger().info("Loading services.json into database") for unloaded_service in unloaded_services: data = only(unloaded_service, ["name", "code", "description"]) new_service = Service(**data) s.add(new_service) logger().debug("Loaded Service: {}".format(new_service.name)) logger().info("Loading tasks.json into database") for unloaded_task in unloaded_tasks: data = only(unloaded_task, ["name", "code", "description", "skippable"]) new_task = Task(**data) s.add(new_task) logger().debug("Loaded Task: {}".format(new_task.name)) s.commit() self.app_conf.update({'loaded': 1}) write_minerva_conf(self.app_conf)
def load_project(self): project_conf = None name_or_short_name = self.options.get('project', None) # load project_conf from cwd, fail if necessary if not name_or_short_name: for other_project_conf in read_projects_conf(): if abspath(other_project_conf.get('path')) == abspath( str(getcwd())): project_conf = other_project_conf if not project_conf and self.project_required: logger().critical( "Project not found in current directory. Please specify a project name or change into the project directory" ) exit(1) # load project_conf from project_id, fail if necessary else: for other_project_conf in read_projects_conf(): if abspath(other_project_conf.get( 'name')) == name_or_short_name or abspath( other_project_conf.get( 'short_name')) == name_or_short_name: project_conf = other_project_conf if not project_conf and self.project_required: logger().critical( "Project name or short name not found. Please specify a name or change into the project directory" ) exit(1) self.project_conf = project_conf
def make_sub_directories(self, project_path): directory_names = ['data', 'reports', '.db', 'scripts'] directory_paths = [Path(join(project_path, d)) for d in directory_names] for directory_path in directory_paths: logger().info('Creating {}'.format(str(directory_path))) self.make_directory(directory_path) logger().warning('Directories created. Do not delete the ".db" folder please! '.format(directory_names))
def register_project(self, project_name, project_short_name, project_path, db_path): project_data = {} project_data['name'] = project_name project_data['short_name'] = project_short_name project_data['path'] = str(project_path) project_data['db'] = str(db_path) logger().info('Adding project to projects.json'.format(db_path)) write_project_conf(project_data)
def run(self): self.validate_service_code(self.options.get('type')) self.log_start() project_path = Path(self.resolve_path()).resolve() project_name = self.resolve_name(project_path) logger().info('Initializing: {}'.format(str(project_path))) self.make_project_directory(project_path) self.make_sub_directories(project_path) project_short_name = self.resolve_short_name(project_name) db_path = join(join(project_path, '.db'), project_short_name + ".sqlite") self.make_db(db_path) self.register_project(project_name, project_short_name, project_path, db_path)
def run(self): self.log_start() data_to_load_path = Path(minerva_path('load')) database_path = Path(minerva_path('manager.sqlite')) if not database_path.is_file(): logger().info("Creating database file for manager at {}".format( str(database_path))) database_path.touch() logger().debug("Attaching session engine for manager database") load_database_engine()
def ensure_minerva_is_ready(): _minerva_path = Path(minerva_path()) if not _minerva_path.is_dir(): logger().info("Making minerva directory at {}".format(_minerva_path)) Path(minerva_path()).mkdir() _minerva_conf_path = Path(minerva_path("minerva.json")) if not _minerva_conf_path.is_file(): logger().info("Making minerva conf file at {}".format(str(_minerva_conf_path))) minerva_conf_data = {} minerva_conf_data["conf_dir"] = minerva_path() minerva_conf_data["log_path"] = minerva_path("minerva.log") minerva_conf_data["log_level"] = "INFO" minerva_conf_data["loaded"] = 0 write_minerva_conf(minerva_conf_data) # can set other binary paths later _minerva_projects_path = Path(minerva_path("projects.json")) if not _minerva_projects_path.is_file(): logger().info("Copying minerva projects template to {}".format(str(_minerva_projects_path))) copy(data_path('projects.json.template'), str(_minerva_projects_path)) copy(data_path('projects.json.example'), str(minerva_path("projects.json.example"))) _minerva_load_data_path = Path(minerva_path("load")) if not _minerva_load_data_path.is_dir(): logger().info("Copying loadable minerva data to {}".format(str(_minerva_load_data_path))) copytree(data_path('load'), str(_minerva_load_data_path))
def run(self): self.log_start() data_to_load_path = Path(minerva_path('load')) database_path = Path(minerva_path('manager.sqlite')) if not database_path.is_file(): if not data_to_load_path.is_dir(): raise DataToLoadDirNotFound(data_to_load_path) logger().info("Creating database file for manager at {}".format( str(database_path))) database_path.touch() load_database_tables() if not self.app_conf.get('loaded', 0): self.load_manager_data_full(data_to_load_path) else: self.load_manager_data_update(data_to_load_path)
def run(self): self.log_start() data_to_load_path = Path(minerva_path('load')) if not data_to_load_path.is_dir(): raise DataToLoadDirNotFound(data_to_load_path) message = "Do you really want to destroy the loadable data in {}".format( str(data_to_load_path)) if not self.force(): if confirm(message): message = "Are you sure you want to delete {}? This will delete any changes made to your loadable data".format( str(data_to_load_path)) if confirm(message): logger().warning( "My the gods grant you mercy. Deleting {}".format( str(data_to_load_path))) shutil.rmtree(str(data_to_load_path)) exit(0) else: message = "You opted to force delete load data. Interesting choice." logger().warning(message) shutil.rmtree(str(data_to_load_path)) exit(0) logger().warning("I guess we won't do that then. Smart choice")
def run(self): # run the first command # create .minerva configuration folder and related files ensure_minerva_is_ready() self.app_conf = read_minerva_conf() setup_logger(self.app_conf) args = self.parser.parse_args() # get rid of namespace object self.args = dict(vars(args)) logger().debug('Minerva cli running') self.boot_manager() logger().debug('Minerva cli booted') try: if 'command' not in self.args: result = self.run_default(self.args) elif self.has_command(self.args['command']): result = self.run_command(self.args['command'], self.args) else: if self.args['command'] != None: logger().warning("Command '{}' not found!".format(self.args['command'])) self.parser.print_help() return 1 except BaseException as e: logger().critical('[!] An Exception occurred while running a command!') raise e # allow commands to trigger other commands while True: if isinstance(result, BaseCommand): try: result = self.call_command(result) except BaseException as e: print('[!] An Exception occurred while running a command!') raise e if not isinstance(result, BaseCommand): break # return the result of the last command return result
def run(self): self.log_start() database_path = Path(minerva_path('manager.sqlite')) if not database_path.is_file(): logger().warning( "Manager database path does not exist. Skipping".format( str(database_path))) return message = "Do you really want to destroy the manager database" if not self.force(): if confirm(message): logger().warning("Deleting manager database") database_path.unlink() exit(0) else: logger().warning("I guess we won't do that then") else: logger().warning("Deleting manager database by force") database_path.unlink()
def make_directory(self, path): if path.is_file(): message = "Cannot initialize directory. A file exists at that location" logger().critical(message) raise BaseException(message) elif path.is_dir(): if self.force(): message = "Directory exists! Continuing anyways! ({})".format(str(path)) logger().warning(message) else: message = "Target Directory Exists. Continue?" if not confirm(message): logger().info('You opted not to continue creating the directory. I respect your decision') exit(0) else: path.mkdir()
def load_manager_data_update(self, data_to_load_path): logger().info("Performing update load of data") services_path = Path(join(str(data_to_load_path), "services.json")) with open(services_path, "r", encoding="utf-8") as infile: unloaded_services = json.loads(infile.read()) tasks_path = Path(join(str(data_to_load_path), "tasks.json")) with open(tasks_path, "r", encoding="utf-8") as infile: unloaded_tasks = json.loads(infile.read()) s = managersession() logger().info("Loading updated services.json into database") for unloaded_service in unloaded_services: cols = ["name", "code", "description"] data = only(unloaded_service, cols, strict=True) code = data['code'] current_service = s.query(Service).filter_by(code=code).first() if current_service: for col in cols: setattr(current_service, col, data[col]) else: new_service = Service(**data) s.add(new_service) logger().info("Loading updated tasks.json into database") for unloaded_task in unloaded_tasks: cols = ["name", "code", "description", "skippable"] data = only(unloaded_task, cols, strict=True) code = data['code'] current_task = s.query(Task).filter_by(code=code).first() if current_task: for col in cols: setattr(current_task, col, data[col]) else: new_task = Task(**data) s.add(new_task) s.commit()
def make_db(self, db_path): load_database_engine(db_path) logger().info('Creating project database at {}'.format(db_path)) load_database_tables(db_path) populate_project_from_service_code(self.options.get('type').upper()))
def log_start(self): logger().debug('Running Command: {}'.format( str(self.__class__.__name__))) logger().debug('Command Options: {}'.format(str(self.options)))
def loaded_is_required(self): status = self.app_conf.get('loaded', 0) if not status and self.loaded_required: logger().critical( "The manager database is not loaded. Please load it.") exit(1)