Skip to content

Instantly share code, notes, and snippets.

@martinsotir
Created June 28, 2019 19:28
Show Gist options
  • Save martinsotir/d1b44cc22dcc257259dbd89ac657851c to your computer and use it in GitHub Desktop.
Save martinsotir/d1b44cc22dcc257259dbd89ac657851c to your computer and use it in GitHub Desktop.
Using git username as mlflow run ID
import subprocess
from mlflow.utils.mlflow_tags import MLFLOW_USER
def get_username_from_git() -> str:
"""Retrieve a (sanitized) username from git config. Raise a runtime error if the git command fails."""
try:
cmd = subprocess.run(['git', 'config', 'user.name'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if len(cmd.stderr.strip()) > 0:
raise ValueError(cmd.stderr.decode('utf-8').strip())
raw_name = cmd.stdout.decode('utf-8')
user_name = "".join(x for x in raw_name.strip().lower().replace(" ", '_') if x.isalnum() or x in '-_')
if not user_name:
raise ValueError("invalid user name")
except (FileNotFoundError, ValueError) as e:
raise RuntimeError(f"Could not retrieve name from git config.\n error={e}")
return user_name
job_id = ...
user = get_username_from_git()
mlflow.start_run(run_name=job_id)
mlflow.set_tag(MLFLOW_USER, user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment