How to use the pathspec.PathSpec function in pathspec

To help you get started, we’ve selected a few pathspec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github psf / black / tests / test_black.py View on Github external
def test_empty_include(self) -> None:
        path = THIS_DIR / "data" / "include_exclude_tests"
        report = black.Report()
        gitignore = PathSpec.from_lines("gitwildmatch", [])
        empty = re.compile(r"")
        sources: List[Path] = []
        expected = [
            Path(path / "b/exclude/a.pie"),
            Path(path / "b/exclude/a.py"),
            Path(path / "b/exclude/a.pyi"),
            Path(path / "b/dont_exclude/a.pie"),
            Path(path / "b/dont_exclude/a.py"),
            Path(path / "b/dont_exclude/a.pyi"),
            Path(path / "b/.definitely_exclude/a.pie"),
            Path(path / "b/.definitely_exclude/a.py"),
            Path(path / "b/.definitely_exclude/a.pyi"),
        ]
        this_abs = THIS_DIR.resolve()
        sources.extend(
            black.gen_python_files_in_dir(
github ambv / retype / src / retype / __init__.py View on Github external
def _load_ignore(at_path, parent_spec, ignores):
    ignore_file = at_path / ".gitignore"
    if not ignore_file.exists():
        return parent_spec
    lines = ignore_file.read_text().split(os.linesep)
    spec = PathSpec.from_lines("gitwildmatch", lines)
    spec = PathSpec(parent_spec.patterns + spec.patterns)
    ignores[at_path] = spec
    return spec
github ambv / retype / src / retype / __init__.py View on Github external
def _load_ignore(at_path, parent_spec, ignores):
    ignore_file = at_path / ".gitignore"
    if not ignore_file.exists():
        return parent_spec
    lines = ignore_file.read_text().split(os.linesep)
    spec = PathSpec.from_lines("gitwildmatch", lines)
    spec = PathSpec(parent_spec.patterns + spec.patterns)
    ignores[at_path] = spec
    return spec
github in-toto / in-toto / in_toto / runlib.py View on Github external
# Apply exclude patterns on the passed artifact paths if available
  if exclude_patterns:
    securesystemslib.formats.NAMES_SCHEMA.check_match(exclude_patterns)
    norm_artifacts = _apply_exclude_patterns(norm_artifacts, exclude_patterns)

  # Check if any of the prefixes passed for left stripping is a left substring
  # of another
  if lstrip_paths:
    for prefix_one, prefix_two in itertools.combinations(lstrip_paths, 2):
      if prefix_one.startswith(prefix_two) or \
          prefix_two.startswith(prefix_one):
        raise in_toto.exceptions.PrefixError("'{}' and '{}' "
            "triggered a left substring error".format(prefix_one, prefix_two))

  # Compile the gitignore-style patterns
  exclude_filter = PathSpec.from_lines('gitwildmatch', exclude_patterns or [])

  # Iterate over remaining normalized artifact paths
  for artifact in norm_artifacts:
    if os.path.isfile(artifact):
      # FIXME: this is necessary to provide consisency between windows
      # filepaths and *nix filepaths. A better solution may be in order
      # though...
      artifact = artifact.replace('\\', '/')
      key = _apply_left_strip(artifact, artifacts_dict, lstrip_paths)
      artifacts_dict[key] = _hash_artifact(artifact,
          normalize_line_endings=normalize_line_endings)

    elif os.path.isdir(artifact):
      for root, dirs, files in os.walk(artifact,
          followlinks=follow_symlink_dirs):
        # Create a list of normalized dirpaths
github dryan / d3ploy / d3ploy / d3ploy.py View on Github external
gitignores.append(".gitignore")
        for root, dir_names, file_names in os.walk(local_path):
            for dir_name in dir_names:
                if dir_name in svc_directories:
                    continue
                dir_name = os.path.join(root, dir_name)
                gitignore_path = os.path.join(dir_name, ".gitignore")
                if os.path.exists(gitignore_path):
                    gitignores.append(gitignore_path)
            for file_name in file_names:
                if file_name == ".gitignore":
                    gitignore_path = os.path.join(root, file_name)
                    gitignores.append(gitignore_path)
        for gitignore_file in gitignores:
            with open(gitignore_file) as f:
                spec = pathspec.PathSpec.from_lines("gitwildmatch", f)
                gitignore_patterns += [x for x in spec.patterns if x.regex]
        if not gitignores:
            alert(
                "--gitignore option set, but no .gitignore files were found",
                color=ALERT_COLOR,
            )
    gitignore_spec = pathspec.PathSpec(gitignore_patterns)

    files = []
    if os.path.isdir(local_path):
        for root, dir_names, file_names in os.walk(local_path):
            for file_name in file_names:
                file_name = os.path.join(root, file_name)
                if not gitignore_spec.match_file(file_name):
                    files.append(file_name)
            for svc_directory in svc_directories:
github app-registry / appr / appr / pack.py View on Github external
def ignore(pattern, path):
    spec = pathspec.PathSpec.from_lines('gitwildmatch', pattern.splitlines())
    return spec.match_file(path)
github dryan / d3ploy / d3ploy / d3ploy.py View on Github external
if os.path.exists(gitignore_path):
                    gitignores.append(gitignore_path)
            for file_name in file_names:
                if file_name == ".gitignore":
                    gitignore_path = os.path.join(root, file_name)
                    gitignores.append(gitignore_path)
        for gitignore_file in gitignores:
            with open(gitignore_file) as f:
                spec = pathspec.PathSpec.from_lines("gitwildmatch", f)
                gitignore_patterns += [x for x in spec.patterns if x.regex]
        if not gitignores:
            alert(
                "--gitignore option set, but no .gitignore files were found",
                color=ALERT_COLOR,
            )
    gitignore_spec = pathspec.PathSpec(gitignore_patterns)

    files = []
    if os.path.isdir(local_path):
        for root, dir_names, file_names in os.walk(local_path):
            for file_name in file_names:
                file_name = os.path.join(root, file_name)
                if not gitignore_spec.match_file(file_name):
                    files.append(file_name)
            for svc_directory in svc_directories:
                if svc_directory in dir_names:
                    dir_names.remove(svc_directory)
    elif os.path.isfile(local_path) or os.path.islink(local_path):
        if not gitignore_spec.match_file(local_path):
            files.append(local_path)
    return files
github MichaelAquilina / S4 / s4 / clients / local.py View on Github external
def traverse(path, ignore_files=None):
    if not os.path.exists(path):
        return
    if ignore_files is None:
        ignore_files = []

    for item in scandir(path):
        full_path = os.path.join(path, item.name)
        spec = pathspec.PathSpec.from_lines(
            pathspec.patterns.GitWildMatchPattern, ignore_files
        )
        if spec.match_file(full_path):
            logger.debug("Ignoring %s", item)
            continue

        if item.is_dir():
            for result in traverse(item.path, ignore_files):
                yield os.path.join(item.name, result)
        else:
            yield item.name