import glob


def LabelPaths(label, src_suffix):
  """Compute single proto file source/destination paths from a Bazel proto label.

  Args:
    label: Bazel source proto label string.
    src_suffix: suffix string to append to source path.
  Returns:
    source, destination path tuple. The source indicates where in the Bazel
      cache the protoxform.py artifact with src_suffix can be found. The
      destination is a provisional path in the Envoy source tree for copying the
      contents of source when run in fix mode.
  """
  assert (label.startswith('@envoy_api//'))
  proto_file_canonical = label[len('@envoy_api//'):].replace(':', '/')
  # We use ** glob matching here to deal with the fact that we have something
  # like
  # bazel-bin/external/envoy_api/envoy/admin/v2alpha/pkg/envoy/admin/v2alpha/certs.proto.proto
  # and we don't want to have to do a nested loop and slow bazel query to
  # recover the canonical package part of the path.
  # While we may have reformatted the file multiple times due to the transitive
  # dependencies in the aspect above, they all look the same. So, just pick an
  # arbitrary match and we're done.
  glob_pattern = 'bazel-bin/external/envoy_api/**/%s.%s' % (proto_file_canonical, src_suffix)
  src = glob.glob(glob_pattern, recursive=True)[0]
  dst = 'api/%s' % proto_file_canonical
  return src, dst