diff --git a/mlc/index.py b/mlc/index.py index 13858fddc..c8a90716a 100644 --- a/mlc/index.py +++ b/mlc/index.py @@ -232,8 +232,12 @@ def _index_single_repo(self, repo, repos_changed=False, if os.path.isfile(yaml_path): config_path = yaml_path + with open(config_path, "r") as f: + data = yaml.safe_load(f) or {} elif os.path.isfile(json_path): config_path = json_path + with open(config_path, "r") as f: + data = json.load(f) or {} else: # No config file found, remove from index if exists delete_flag = False @@ -257,6 +261,7 @@ def _index_single_repo(self, repo, repos_changed=False, if delete_flag: changed = True continue + if current_item_keys is not None: current_item_keys.add(config_path) mtime = self.get_item_mtime(config_path) @@ -266,14 +271,25 @@ def _index_single_repo(self, repo, repos_changed=False, if old_mtime == mtime and not repos_changed: continue + # Validate script meta against schema during indexing if meta changed or repos changed + if folder_type == "script": + # logger.debug(f"-----Meta changed for {automation_path}, validating against schema... -----") + errors, warnings = validate_meta(data, config_path) + for e in errors: + logger.error(f"Meta validation error: {e}") + for w in warnings: + logger.debug(f"Meta validation warning: {w}") + if errors: + raise ValueError( + f"Meta validation failed for {config_path}. Fix the above error(s) and try again.") + self.modified_times[config_path] = { "mtime": mtime, "date_time": datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S") } # meta file changed, so reindex - self._process_config_file( - config_path, folder_type, automation_path, repo) + self._process_config_file(config_path, folder_type, automation_path, repo) changed = True return changed @@ -346,7 +362,7 @@ def _remove_index_entry(self, key): if removed_count > 0: logger.debug( f"Removed {removed_count} item(s) from {ft} index") - + def _delete_index_entries(self, folder_type, key, value): """ Remove index entries matching for the same path or same UID. @@ -400,22 +416,10 @@ def _process_config_file( tags = data.get("tags", []) alias = data.get("alias", None) - # Validate script meta against schema during indexing - if folder_type == "script": - errors, warnings = validate_meta(data, config_file) - for e in errors: - logger.error(f"Meta validation error: {e}") - for w in warnings: - logger.debug(f"Meta validation warning: {w}") - if errors: - raise ValueError( - f"Meta validation failed for {config_file}. Fix the above error(s) and try again.") - # Remove stale entry for the same meta file path if exists self._delete_index_entries(folder_type, "path", folder_path) - # Remove index entry with the same UID for other meta file if - # exists + # Remove index entry with the same UID for other meta file if exists self._delete_index_entries(folder_type, "uid", unique_id) self.indices[folder_type].append({