File size: 2,068 Bytes
362e18c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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://<placeholder>(/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 '<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]
# 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() |