2022-02-14 20:47:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2022 The Chromium OS Authors. All rights reserved.
|
2021-10-15 01:56:44 +00:00
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2021-11-11 23:14:38 +00:00
|
|
|
# Run `rustfmt` on all Rust code contained in the crosvm workspace, including
|
|
|
|
# all commmon/* crates as well.
|
2021-10-15 01:56:44 +00:00
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# $ bin/fmt
|
|
|
|
#
|
|
|
|
# To print a diff and exit 1 if code is not formatted, but without changing any
|
|
|
|
# files, use:
|
|
|
|
#
|
|
|
|
# $ bin/fmt --check
|
|
|
|
#
|
|
|
|
|
2022-03-22 20:04:31 +00:00
|
|
|
from impl.common import (
|
|
|
|
CROSVM_ROOT,
|
|
|
|
find_scripts,
|
|
|
|
parallel,
|
|
|
|
run_main,
|
|
|
|
cmd,
|
|
|
|
chdir,
|
|
|
|
find_source_files,
|
|
|
|
)
|
2022-02-18 18:37:14 +00:00
|
|
|
from pathlib import Path
|
2021-10-15 01:56:44 +00:00
|
|
|
|
2022-02-14 20:47:01 +00:00
|
|
|
mdformat = cmd("mdformat")
|
2022-02-18 18:37:14 +00:00
|
|
|
rustfmt = cmd(cmd("rustup which rustfmt"))
|
2022-03-22 20:04:31 +00:00
|
|
|
black = cmd("black", "--line-length 100")
|
2022-02-18 18:37:14 +00:00
|
|
|
|
|
|
|
# How many files to check at once in each thread.
|
|
|
|
BATCH_SIZE = 8
|
|
|
|
|
2022-03-02 19:10:59 +00:00
|
|
|
# Files not under our control or auto-generated.
|
|
|
|
IGNORE = [
|
|
|
|
"infra/README.recipes.md",
|
|
|
|
"infra/recipes.py",
|
|
|
|
]
|
|
|
|
|
2022-02-18 18:37:14 +00:00
|
|
|
|
2022-02-14 20:47:01 +00:00
|
|
|
def main(check: bool = False):
|
|
|
|
chdir(CROSVM_ROOT)
|
|
|
|
check_arg = "--check" if check else None
|
|
|
|
|
2022-03-22 20:04:31 +00:00
|
|
|
print(f"{'Checking format' if check else 'Formatting'}: Rust, Markdown")
|
2022-02-18 18:37:14 +00:00
|
|
|
parallel(
|
2022-03-22 20:04:31 +00:00
|
|
|
*rustfmt(check_arg).foreach(find_source_files("rs"), batch_size=BATCH_SIZE),
|
2022-03-02 19:10:59 +00:00
|
|
|
*mdformat("--wrap 100", check_arg).foreach(
|
|
|
|
find_source_files("md", IGNORE),
|
|
|
|
batch_size=BATCH_SIZE,
|
|
|
|
),
|
2022-03-22 20:04:31 +00:00
|
|
|
*black(check_arg).foreach(
|
|
|
|
(
|
2022-03-02 19:10:59 +00:00
|
|
|
*find_source_files("py", IGNORE),
|
2022-03-22 20:04:31 +00:00
|
|
|
*find_scripts(Path("tools"), "/usr/bin/env python3"),
|
|
|
|
),
|
|
|
|
batch_size=BATCH_SIZE,
|
|
|
|
),
|
2022-02-18 18:37:14 +00:00
|
|
|
).fg()
|
2022-02-14 20:47:01 +00:00
|
|
|
|
|
|
|
|
2022-03-22 20:04:31 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
run_main(main)
|