1#!/usr/bin/env python3 2# Copyright 2022 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6# Run `rustfmt` on all Rust code contained in the crosvm workspace, including 7# all commmon/* crates as well. 8# 9# Usage: 10# 11# $ bin/fmt 12# 13# To print a diff and exit 1 if code is not formatted, but without changing any 14# files, use: 15# 16# $ bin/fmt --check 17# 18 19from impl.common import CROSVM_ROOT, parallel, run_main, cmd, chdir 20from pathlib import Path 21 22mdformat = cmd("mdformat") 23rustfmt = cmd(cmd("rustup which rustfmt")) 24 25# How many files to check at once in each thread. 26BATCH_SIZE = 8 27 28 29def find_sources(extension: str): 30 for file in Path(".").glob(f"**/{extension}"): 31 if file.is_relative_to("third_party"): 32 continue 33 if "target" in file.parts: 34 continue 35 yield str(file) 36 37 38def main(check: bool = False): 39 chdir(CROSVM_ROOT) 40 check_arg = "--check" if check else None 41 42 print(f"{'Checking' if check else 'Formatting'}: Rust, Markdown") 43 parallel( 44 *rustfmt(check_arg).foreach(find_sources("*.rs"), batch_size=BATCH_SIZE), 45 *mdformat("--wrap 100", check_arg).foreach(find_sources("*.md"), batch_size=BATCH_SIZE), 46 ).fg() 47 48 49run_main(main) 50