import re import glob import sys def restore_file(file_path, absolute_path, user_id): """Restore placeholders in files with the specified absolute path and user ID.""" with open(file_path, 'r') as f: lines = f.readlines() modified = False for i, line in enumerate(lines): # Restore artifact_uri or artifact_location if 'artifact_uri:' in line or 'artifact_location:' in line: new_line = re.sub(r'file://(/mlruns/)', rf'file://{absolute_path}\1', line) if new_line != line: lines[i] = new_line modified = True # Restore user_id elif 'user_id:' in line and '' in line: new_line = re.sub(r'user_id:\s*', f'user_id: {user_id}', line) if new_line != line: lines[i] = new_line modified = True if modified: with open(file_path, 'w') as f: f.writelines(lines) print(f"Restored: {file_path}") def main(): if len(sys.argv) != 3: print("Usage: python restore_mlflow_paths.py ") print("Example: python restore_mlflow_paths.py /home/user/path/to/mlruns hendrik-roth") sys.exit(1) absolute_path = sys.argv[1] user_id = sys.argv[2] # Ensure the path starts with / and does not end with / if not absolute_path.startswith('/'): absolute_path = '/' + absolute_path absolute_path = absolute_path.rstrip('/') # Find all yaml files recursively yaml_files = glob.glob('**/*.yaml', recursive=True) for file_path in yaml_files: restore_file(file_path, absolute_path, user_id) # Also restore mlflow.user tag files user_files = glob.glob('**/tags/mlflow.user', recursive=True) for file_path in user_files: with open(file_path, 'w') as f: f.write(f'{user_id}\n') print(f"Restored: {file_path}") print("Path restoration complete.") if __name__ == "__main__": main()