summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/utils/globstar.py
blob: 6153349b6afd6e2d0b2964a7eaf57d02fbaaa664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Filename globbing utility."""

import glob as _glob
import os.path
import re

_CONTAINS_GLOB_PATTERN = re.compile("[*?[]")


def is_glob_pattern(string):
    """Return true if 'string' represents a glob pattern, and false otherwise."""

    # Copied from glob.has_magic().
    return _CONTAINS_GLOB_PATTERN.search(string) is not None


def glob(globbed_pathname):
    """Return a list of pathnames matching the 'globbed_pathname' pattern.

    In addition to containing simple shell-style wildcards a la fnmatch,
    the pattern may also contain globstars ("**"), which is recursively
    expanded to match zero or more subdirectories.
    """

    return list(iglob(globbed_pathname))


def iglob(globbed_pathname):
    """Emit a list of pathnames matching the 'globbed_pathname' pattern.

    In addition to containing simple shell-style wildcards a la fnmatch,
    the pattern may also contain globstars ("**"), which is recursively
    expanded to match zero or more subdirectories.
    """

    for pathname in _glob.iglob(globbed_pathname, recursive=True):
        # Normalize 'pathname' so exact string comparison can be used later.
        yield os.path.normpath(pathname)