All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 from functools import lru_cache
4 from pathlib import Path
20 from mypy_extensions import mypyc_attr
21 from pathspec import PathSpec
22 from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
25 from black.output import err
26 from black.report import Report
27 from black.handle_ipynb_magics import jupyter_dependencies_are_installed
30 import colorama # noqa: F401
34 def find_project_root(srcs: Sequence[str]) -> Path:
35 """Return a directory containing .git, .hg, or pyproject.toml.
37 That directory will be a common parent of all files and directories
40 If no directory in the tree contains a marker that would specify it's the
41 project root, the root of the file system is returned.
44 srcs = [str(Path.cwd().resolve())]
46 path_srcs = [Path(Path.cwd(), src).resolve() for src in srcs]
48 # A list of lists of parents for each 'src'. 'src' is included as a
49 # "parent" of itself if it is a directory
51 list(path.parents) + ([path] if path.is_dir() else []) for path in path_srcs
55 set.intersection(*(set(parents) for parents in src_parents)),
56 key=lambda path: path.parts,
59 for directory in (common_base, *common_base.parents):
60 if (directory / ".git").exists():
63 if (directory / ".hg").is_dir():
66 if (directory / "pyproject.toml").is_file():
72 def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> Optional[str]:
73 """Find the absolute filepath to a pyproject.toml if it exists"""
74 path_project_root = find_project_root(path_search_start)
75 path_pyproject_toml = path_project_root / "pyproject.toml"
76 if path_pyproject_toml.is_file():
77 return str(path_pyproject_toml)
80 path_user_pyproject_toml = find_user_pyproject_toml()
82 str(path_user_pyproject_toml)
83 if path_user_pyproject_toml.is_file()
86 except PermissionError as e:
87 # We do not have access to the user-level config directory, so ignore it.
88 err(f"Ignoring user configuration directory due to {e!r}")
92 @mypyc_attr(patchable=True)
93 def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:
94 """Parse a pyproject toml file, pulling out relevant parts for Black
96 If parsing fails, will raise a tomli.TOMLDecodeError
98 with open(path_config, encoding="utf8") as f:
99 pyproject_toml = tomli.loads(f.read())
100 config = pyproject_toml.get("tool", {}).get("black", {})
101 return {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
105 def find_user_pyproject_toml() -> Path:
106 r"""Return the path to the top-level user configuration for black.
108 This looks for ~\.black on Windows and ~/.config/black on Linux and other
111 if sys.platform == "win32":
113 user_config_path = Path.home() / ".black"
115 config_root = os.environ.get("XDG_CONFIG_HOME", "~/.config")
116 user_config_path = Path(config_root).expanduser() / "black"
117 return user_config_path.resolve()
121 def get_gitignore(root: Path) -> PathSpec:
122 """Return a PathSpec matching gitignore content if present."""
123 gitignore = root / ".gitignore"
124 lines: List[str] = []
125 if gitignore.is_file():
126 with gitignore.open(encoding="utf-8") as gf:
127 lines = gf.readlines()
129 return PathSpec.from_lines("gitwildmatch", lines)
130 except GitWildMatchPatternError as e:
131 err(f"Could not parse {gitignore}: {e}")
135 def normalize_path_maybe_ignore(
136 path: Path, root: Path, report: Report
138 """Normalize `path`. May return `None` if `path` was ignored.
140 `report` is where "path ignored" output goes.
143 abspath = path if path.is_absolute() else Path.cwd() / path
144 normalized_path = abspath.resolve().relative_to(root).as_posix()
146 report.path_ignored(path, f"cannot be read because {e}")
150 if path.is_symlink():
151 report.path_ignored(path, f"is a symbolic link that points outside {root}")
156 return normalized_path
159 def path_is_excluded(
160 normalized_path: str,
161 pattern: Optional[Pattern[str]],
163 match = pattern.search(normalized_path) if pattern else None
164 return bool(match and match.group(0))
167 def gen_python_files(
168 paths: Iterable[Path],
170 include: Pattern[str],
171 exclude: Pattern[str],
172 extend_exclude: Optional[Pattern[str]],
173 force_exclude: Optional[Pattern[str]],
175 gitignore: Optional[PathSpec],
180 """Generate all files under `path` whose paths are not excluded by the
181 `exclude_regex`, `extend_exclude`, or `force_exclude` regexes,
182 but are included by the `include` regex.
184 Symbolic links pointing outside of the `root` directory are ignored.
186 `report` is where output about exclusions goes.
188 assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
190 normalized_path = normalize_path_maybe_ignore(child, root, report)
191 if normalized_path is None:
194 # First ignore files matching .gitignore, if passed
195 if gitignore is not None and gitignore.match_file(normalized_path):
196 report.path_ignored(child, "matches the .gitignore file content")
199 # Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.
200 normalized_path = "/" + normalized_path
202 normalized_path += "/"
204 if path_is_excluded(normalized_path, exclude):
205 report.path_ignored(child, "matches the --exclude regular expression")
208 if path_is_excluded(normalized_path, extend_exclude):
210 child, "matches the --extend-exclude regular expression"
214 if path_is_excluded(normalized_path, force_exclude):
215 report.path_ignored(child, "matches the --force-exclude regular expression")
219 # If gitignore is None, gitignore usage is disabled, while a Falsey
220 # gitignore is when the directory doesn't have a .gitignore file.
221 yield from gen_python_files(
229 gitignore + get_gitignore(child) if gitignore is not None else None,
234 elif child.is_file():
235 if child.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
236 verbose=verbose, quiet=quiet
239 include_match = include.search(normalized_path) if include else True
244 def wrap_stream_for_windows(
246 ) -> Union[io.TextIOWrapper, "colorama.AnsiToWin32"]:
248 Wrap stream with colorama's wrap_stream so colors are shown on Windows.
250 If `colorama` is unavailable, the original stream is returned unmodified.
251 Otherwise, the `wrap_stream()` function determines whether the stream needs
252 to be wrapped for a Windows environment and will accordingly either return
253 an `AnsiToWin32` wrapper or the original stream.
256 from colorama.initialise import wrap_stream
260 # Set `strip=False` to avoid needing to modify test_express_diff_with_color.
261 return wrap_stream(f, convert=None, strip=False, autoreset=False, wrap=True)