https://pantsbuild.org/ logo
#development
Title
# development
r

rough-minister-58256

05/25/2016, 1:17 AM
Copy code
def glob_to_regex(glob):
  """Given a glob pattern, return a regex that will match it's path relative to the buildroot.

  :param string glob: The glob pattern.
  :returns: A regex string that will match a relative glob path with no leading `/`.
  """
  replacement_map = (('?', r'.'),
                     ('.', r'[.]'),
                     ('**/', r'(?:.*/)?'),
                     ('*', r'[^/]*'))

  for from_pattern, to_pattern in replacement_map:
    glob = glob.replace(from_pattern, to_pattern)

  # Treat trailing slashes as "the dir and everything beneath it".
  if glob.endswith('/'):
    glob = r'{}(/.*|$)'.format(glob[:-1])

  if glob.startswith('/'):
    glob = r'^{}'.format(glob[1:])

  return glob