| | 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): |
| | |
| | if 'artifact_uri:' in line or 'artifact_location:' in line: |
| | new_line = re.sub(r'file://<placeholder>(/mlruns/)', rf'file://{absolute_path}\1', line) |
| | if new_line != line: |
| | lines[i] = new_line |
| | modified = True |
| | |
| | elif 'user_id:' in line and '<user_placeholder>' in line: |
| | new_line = re.sub(r'user_id:\s*<user_placeholder>', 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 <absolute_path> <user_id>") |
| | 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] |
| | |
| | if not absolute_path.startswith('/'): |
| | absolute_path = '/' + absolute_path |
| | absolute_path = absolute_path.rstrip('/') |
| | |
| | |
| | yaml_files = glob.glob('**/*.yaml', recursive=True) |
| | for file_path in yaml_files: |
| | restore_file(file_path, absolute_path, user_id) |
| | |
| | |
| | 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() |