Ejemplo n.º 1
0
    def _ConvertGlobIntoPathComponents(self, pattern):
        r"""Converts a glob pattern into a list of pathspec components.

    Wildcards are also converted to regular expressions. The pathspec components
    do not span directories, and are marked as a regex or a literal component.

    We also support recursion into directories using the ** notation.  For
    example, /home/**2/foo.txt will find all files named foo.txt recursed 2
    directories deep. If the directory depth is omitted, it defaults to 3.

    Example:
     /home/test/* -> ['home', 'test', '.*\\Z(?ms)']

    Args:
      pattern: A glob expression with wildcards.

    Returns:
      A list of PathSpec instances for each component.

    Raises:
      ValueError: If the glob is invalid.
    """

        components = []
        for path_component in pattern.split("/"):
            # A ** in the path component means recurse into directories that match the
            # pattern.
            m = rdf_paths.GlobExpression.RECURSION_REGEX.search(path_component)
            if m:
                path_component = path_component.replace(m.group(0), "*")

                component = rdf_paths.PathSpec(
                    path=fnmatch.translate(path_component),
                    pathtype=self.state.pathtype,
                    path_options=rdf_paths.PathSpec.Options.RECURSIVE)

                # Allow the user to override the recursion depth.
                if m.group(1):
                    component.recursion_depth = int(m.group(1))

            elif self.GLOB_MAGIC_CHECK.search(path_component):
                component = rdf_paths.PathSpec(
                    path=fnmatch.translate(path_component),
                    pathtype=self.state.pathtype,
                    path_options=rdf_paths.PathSpec.Options.REGEX)
            else:
                component = rdf_paths.PathSpec(
                    path=path_component,
                    pathtype=self.state.pathtype,
                    path_options=rdf_paths.PathSpec.Options.CASE_INSENSITIVE)

            components.append(component)

        return components
Ejemplo n.º 2
0
    def __init__(self, glob, opts=None):
        """Instantiates a new GlobComponent from a given path glob.

    Args:
      glob: A string with potential glob elements (e.g. `foo*`).
      opts: An optional PathOpts instance.
    """
        super().__init__()
        self._glob = glob
        self._is_literal = PATH_GLOB_REGEX.search(self._glob) is None
        self.regex = re.compile(fnmatch.translate(glob), re.I)
        self.opts = opts or PathOpts()
Ejemplo n.º 3
0
 def testProducesExpectedPython2CompatibleOutput(self):
     self.assertEqual(fnmatch.translate("*"), "(?ms).*\\Z")
     self.assertEqual(fnmatch.translate("bar.*"), "(?ms)bar\\..*\\Z")
     self.assertEqual(fnmatch.translate("*.bar.*"), "(?ms).*\\.bar\\..*\\Z")