1
Fork 0

Replace black with ruff for formatting Python code

This commit is contained in:
Jakub Beránek 2024-12-03 22:02:58 +01:00
parent f676ed2d2c
commit b24d608967
No known key found for this signature in database
GPG key ID: 909CD0D26483516B

View file

@ -32,7 +32,6 @@ const REL_PY_PATH: &[&str] = &["Scripts", "python3.exe"];
const REL_PY_PATH: &[&str] = &["bin", "python3"]; const REL_PY_PATH: &[&str] = &["bin", "python3"];
const RUFF_CONFIG_PATH: &[&str] = &["src", "tools", "tidy", "config", "ruff.toml"]; const RUFF_CONFIG_PATH: &[&str] = &["src", "tools", "tidy", "config", "ruff.toml"];
const BLACK_CONFIG_PATH: &[&str] = &["src", "tools", "tidy", "config", "black.toml"];
/// Location within build directory /// Location within build directory
const RUFF_CACHE_PATH: &[&str] = &["cache", "ruff_cache"]; const RUFF_CACHE_PATH: &[&str] = &["cache", "ruff_cache"];
const PIP_REQ_PATH: &[&str] = &["src", "tools", "tidy", "config", "requirements.txt"]; const PIP_REQ_PATH: &[&str] = &["src", "tools", "tidy", "config", "requirements.txt"];
@ -124,33 +123,36 @@ fn check_impl(
} }
if python_fmt { if python_fmt {
let mut cfg_args_black = cfg_args.clone(); let mut cfg_args_ruff = cfg_args.clone();
let mut file_args_black = file_args.clone(); let mut file_args_ruff = file_args.clone();
if bless { if bless {
eprintln!("formatting python files"); eprintln!("formatting python files");
} else { } else {
eprintln!("checking python file formatting"); eprintln!("checking python file formatting");
cfg_args_black.push("--check".as_ref()); cfg_args_ruff.push("--check".as_ref());
} }
let mut cfg_path = root_path.to_owned(); let mut cfg_path = root_path.to_owned();
cfg_path.extend(BLACK_CONFIG_PATH); cfg_path.extend(RUFF_CONFIG_PATH);
let mut cache_dir = outdir.to_owned();
cache_dir.extend(RUFF_CACHE_PATH);
cfg_args_black.extend(["--config".as_ref(), cfg_path.as_os_str()]); cfg_args_ruff.extend(["--config".as_ref(), cfg_path.as_os_str()]);
if file_args_black.is_empty() { if file_args_ruff.is_empty() {
file_args_black.push(root_path.as_os_str()); file_args_ruff.push(root_path.as_os_str());
} }
let mut args = merge_args(&cfg_args_black, &file_args_black); let mut args = merge_args(&cfg_args_ruff, &file_args_ruff);
let res = py_runner(py_path.as_ref().unwrap(), true, None, "black", &args); args.insert(0, "format".as_ref());
let res = py_runner(py_path.as_ref().unwrap(), true, None, "ruff", &args);
if res.is_err() && show_diff { if res.is_err() && show_diff {
eprintln!("\npython formatting does not match! Printing diff:"); eprintln!("\npython formatting does not match! Printing diff:");
args.insert(0, "--diff".as_ref()); args.insert(0, "--diff".as_ref());
let _ = py_runner(py_path.as_ref().unwrap(), true, None, "black", &args); let _ = py_runner(py_path.as_ref().unwrap(), true, None, "ruff", &args);
} }
// Rethrow error // Rethrow error
let _ = res?; let _ = res?;