def step(filenames: List[str], yaml: Optional[str]) -> None: """ Update a step config(s) in valohai.yaml based on Python source file(s). Example: vh yaml step hello.py :param filenames: Path(s) of the Python source code files. """ project = get_project() if project is None: info("no project linked - assuming files are in current directory.") project = Project(data={}, directory=os.getcwd()) project.name = "YAML command simulated project" config_path = project.get_config_filename(yaml_path=yaml) yaml = yaml or project.get_yaml_path() for source_path in filenames: if not os.path.isfile(config_path): update_yaml_from_source(source_path, project, yaml) info(f"{yaml} generated.") create_or_update_requirements(project) elif yaml_needs_update(source_path, project, yaml): update_yaml_from_source(source_path, project, yaml) info(f"{yaml} updated.") create_or_update_requirements(project) else: info(f"{yaml} already up-to-date.")
def get_updated_config(source_path: str, project: Project) -> Config: """Opens the old valohai.yaml, parses source Python file and merges the resulting config to the old Call to valohai.prepare() will contain step name, parameters and inputs. We use the AST parser to parse those from the Python source code file and return the merged config. :param source_path: Path of the Python source code file :param project: Currently linked Valohai project """ old_config = get_current_config(project) new_config = parse_config_from_source(source_path, project.get_config_filename()) if old_config: return old_config.merge_with(new_config) return new_config
def update_yaml_from_source(source_path: str, project: Project) -> bool: """Updates valohai.yaml by parsing the source code file for a call to valohai.prepare() Call to valohai.prepare() will contain step name, parameters and inputs. We use the AST parser to parse those from the Python source code file and update (or generate) valohai.yaml accordingly. :param source_path: Path of the Python source code file :param project: Currently linked Valohai project """ old_config = get_current_config(project) new_config = get_updated_config(source_path, project) if old_config != new_config: with open(project.get_config_filename(), 'w') as out_file: out_file.write(config_to_yaml(new_config)) return True return False