diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 7f35fbdd6..a46208baf 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -34,7 +34,7 @@ jobs: python-version: "3.10" - name: check style - run: python scripts/check_style.py --check src + run: python scripts/style.py --check src test: strategy: diff --git a/scripts/check_style.py b/scripts/style.py similarity index 88% rename from scripts/check_style.py rename to scripts/style.py index aa7c56f1e..4c676442d 100644 --- a/scripts/check_style.py +++ b/scripts/style.py @@ -7,14 +7,29 @@ MAX_LINE_LENGTH = 100 -arg_parser = argparse.ArgumentParser() -arg_parser.add_argument("dirs", action="append") -arg_parser.add_argument("--check", action="store_true") -arg_parser.add_argument("-v", "--verbose", action="store_true") -args = arg_parser.parse_args() +def main(): + arg_parser = argparse.ArgumentParser() + arg_parser.add_argument("dirs", action="append") + arg_parser.add_argument("--check", action="store_true") + arg_parser.add_argument("-v", "--verbose", action="store_true") + args = arg_parser.parse_args() -def get_files(): + files_to_check = get_files(args) + + checks = [ + unused_imports, + line_length, + ] + fails = 0 + for check in checks: + print("Running check: ", check.__name__) + fails += check(args, files_to_check) + if fails: + exit(1) + + +def get_files(args): files_to_check = [] dirs = [*args.dirs] while len(dirs) > 0: @@ -30,19 +45,18 @@ def get_files(): return files_to_check -# Checks for unused imports in files. -def unused_imports(): - files_to_check = get_files() +def unused_imports(args, files_to_check): + """Checks for unused imports in files.""" import_line_regex = re.compile( - r'const\s+([a-zA-Z]+)\s+=\s+@import\("([a-zA-Z.]+)"\);' + r'const\s+([a-zA-Z0-9_]+)\s+=\s+(@import\("[a-zA-Z0-9_./]+"\))?[a-zA-Z0-9_.]*;' ) total_lines_removed = 0 - lines_removed = 0 while True: lines_removed = 0 for path in files_to_check: + # get all lines of code with open(path) as f: orig_file = f.read() orig_lines = orig_file.split("\n") @@ -103,11 +117,11 @@ def unused_imports(): print("Total unused imports found:", total_lines_removed) else: print("Total unused imports removed:", total_lines_removed) - if total_lines_removed > 0: - exit(1) + + return total_lines_removed -excluded_files = [ +files_excluded_from_line_length_check = [ "src/accountsdb/accounts_file.zig", "src/accountsdb/bank.zig", "src/accountsdb/cache.zig", @@ -232,15 +246,14 @@ def unused_imports(): ] -# Enforces rows to be at most 100 characters long. -def row_size(): - files_to_check = get_files() +def line_length(args, files_to_check): + """Enforces lines of code to be at most 100 characters long.""" unique_files = [] lines_found = 0 for path in files_to_check: - if path in excluded_files: + if path in files_excluded_from_line_length_check: continue with open(path) as f: lines = f.readlines() @@ -258,15 +271,8 @@ def row_size(): for file in unique_files: print(f'"{file}",') - if lines_found > 0: - exit(1) - + return lines_found -checks = [ - unused_imports, - row_size, -] -for check in checks: - print("Running check: ", check.__name__) - check() +if __name__ == '__main__': + main() diff --git a/src/accountsdb/sysvars.zig b/src/accountsdb/sysvars.zig index 5137e2239..538e1346f 100644 --- a/src/accountsdb/sysvars.zig +++ b/src/accountsdb/sysvars.zig @@ -6,13 +6,10 @@ const ArrayList = std.ArrayList; const Slot = @import("../core/time.zig").Slot; const Epoch = @import("../core/time.zig").Epoch; const Pubkey = @import("../core/pubkey.zig").Pubkey; -const Hash = @import("../core/hash.zig").Hash; const StakeHistoryEntry = @import("./snapshots.zig").StakeHistoryEntry; const UnixTimestamp = @import("genesis_config.zig").UnixTimestamp; -const ThreadPool = @import("../sync/thread_pool.zig").ThreadPool; -const BitVec = @import("../bloom/bit_vec.zig").BitVec; const DynamicArrayBitSet = @import("../bloom/bit_set.zig").DynamicArrayBitSet; const BitVecConfig = @import("../bloom/bit_vec.zig").BitVecConfig; const bincode = @import("../bincode/bincode.zig"); diff --git a/src/shred_collector/shred_tracker.zig b/src/shred_collector/shred_tracker.zig index 3dc5874f6..64f31480b 100644 --- a/src/shred_collector/shred_tracker.zig +++ b/src/shred_collector/shred_tracker.zig @@ -1,6 +1,5 @@ const std = @import("std"); const sig = @import("../sig.zig"); -const shred_collector = @import("lib.zig"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; diff --git a/src/shred_collector/shred_verifier.zig b/src/shred_collector/shred_verifier.zig index 75dc734f6..4dfdf75ee 100644 --- a/src/shred_collector/shred_verifier.zig +++ b/src/shred_collector/shred_verifier.zig @@ -1,6 +1,5 @@ const std = @import("std"); const sig = @import("../sig.zig"); -const shred_collector = @import("lib.zig"); const shred_layout = sig.ledger.shred.layout;