- LOG.error(f"Running black for {repo_path} timed out ({cmd})")
- except CalledProcessError as cpe:
- # TODO: This might need to be tuned and made smarter for higher signal
- if not project_config["expect_formatting_changes"] and cpe.returncode == 1:
+ results.failed_projects[project_name] = CalledProcessError(
+ 69, [], f"{project_name} has no repo_path: {repo_path}".encode(), b""
+ )
+ return
+
+ stdin_test = project_name.upper() == "STDIN"
+ cmd = [str(which(BLACK_BINARY))]
+ if "cli_arguments" in project_config and project_config["cli_arguments"]:
+ cmd.extend(_flatten_cli_args(project_config["cli_arguments"]))
+ cmd.append("--check")
+ if not no_diff:
+ cmd.append("--diff")
+
+ # Workout if we should read in a python file or search from cwd
+ stdin = None
+ if stdin_test:
+ cmd.append("-")
+ stdin = repo_path.read_bytes()
+ elif "base_path" in project_config:
+ cmd.append(project_config["base_path"])
+ else:
+ cmd.append(".")
+
+ timeout = (
+ project_config["timeout_seconds"]
+ if "timeout_seconds" in project_config
+ else TEN_MINUTES_SECONDS
+ )
+ with TemporaryDirectory() as tmp_path:
+ # Prevent reading top-level user configs by manipulating environment variables
+ env = {
+ **os.environ,
+ "XDG_CONFIG_HOME": tmp_path, # Unix-like
+ "USERPROFILE": tmp_path, # Windows (changes `Path.home()` output)
+ }
+
+ cwd_path = repo_path.parent if stdin_test else repo_path
+ try:
+ LOG.debug(f"Running black for {project_name}: {' '.join(cmd)}")
+ _stdout, _stderr = await _gen_check_output(
+ cmd, cwd=cwd_path, env=env, stdin=stdin, timeout=timeout
+ )
+ except asyncio.TimeoutError: