import re


def get_step_from_checkpoint_path(checkpoint_path):
    """Returns the global step for the checkpoint at `checkpoint_path`.

  **Note**: This function works for checkpoints that are saved in the TF format
  only.

  Assumes `checkpoint_path` corresponds to a file which contains the substring
  model.ckpt-{global_step}

  Args:
    checkpoint_path: str of path to a checkpoint file.

  Returns:
    int of the global step corresponding to the checkpoint file.

  Raises:
    ValueError if checkpoint_path does not correspond to a model checkpoint file
    which contains the global_step in its filename.
  """
    match = re.match(r".*model\.ckpt\-(\d+).*", checkpoint_path)
    if match is None:
        raise ValueError("Invalid checkpoint path {}".format(checkpoint_path))
    return int(match.group(1))