11import argparse
22import json
33import os
4- import pathlib
54import platform
65import subprocess
76import sys
87
9- import download_buildifier
10- from simple_report import make_report , put_report , try_combine_reports
11- from unittest_grouper import validate_bazel_groups
12-
13- mongo_dir = pathlib .Path (__file__ ).parents [1 ]
8+ from buildscripts import download_buildifier
9+ from buildscripts .simple_report import make_report , put_report , try_combine_reports
10+ from buildscripts .unittest_grouper import validate_bazel_groups
1411
1512
1613def find_all_failed (bin_path : str ) -> list [str ]:
1714 # TODO(SERVER-81039): Remove once third_party libs can be compiled from the root directory.
1815 ignored_paths = []
19- with open (os .path .join (mongo_dir , ".bazelignore" ), "r" ) as file :
16+ with open (os .path .join (os . curdir , ".bazelignore" ), "r" , encoding = "utf-8 " ) as file :
2017 for line in file .readlines ():
2118 contents = line .split ("#" )[0 ].strip ()
2219 if contents :
@@ -43,8 +40,9 @@ def find_all_failed(bin_path: str) -> list[str]:
4340
4441def lint_all (bin_path : str , generate_report : bool ):
4542 files = find_all_failed (bin_path )
46- lint (bin_path , files , generate_report )
43+ result = lint (bin_path , files , generate_report )
4744 validate_bazel_groups (generate_report = generate_report , fix = False , quick = False )
45+ return result
4846
4947
5048def fix_all (bin_path : str ):
@@ -60,6 +58,7 @@ def fix_unittests(bin_path: str):
6058
6159
6260def lint (bin_path : str , files : list [str ], generate_report : bool ):
61+ found_errors = False
6362 for file in files :
6463 process = subprocess .run (
6564 [bin_path , "--format=json" , "--mode=check" , file ], check = True , capture_output = True
@@ -76,6 +75,7 @@ def lint(bin_path: str, files: list[str], generate_report: bool):
7675 diff = process .stdout
7776 print (f"{ file } has linting errors" )
7877 print (diff )
78+ found_errors = True
7979
8080 if generate_report :
8181 header = (
@@ -91,6 +91,7 @@ def lint(bin_path: str, files: list[str], generate_report: bool):
9191 put_report (report )
9292
9393 print ("Done linting files" )
94+ return not found_errors
9495
9596
9697def fix (bin_path : str , files : list [str ]):
@@ -101,6 +102,14 @@ def fix(bin_path: str, files: list[str]):
101102
102103
103104def main ():
105+ default_dir = os .environ .get ("BUILD_WORKSPACE_DIRECTORY" )
106+ if not default_dir :
107+ print ("This script must be run though bazel. Please run 'bazel run //:format' instead" )
108+ print ("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n " )
109+ print ("python buildscripts/install_bazel.py" )
110+ return 1
111+ os .chdir (default_dir )
112+
104113 parser = argparse .ArgumentParser (description = "buildifier wrapper" )
105114 parser .add_argument (
106115 "--binary-dir" ,
@@ -139,9 +148,6 @@ def main():
139148 lint_parser .set_defaults (subcommand = "fix" )
140149
141150 args = parser .parse_args ()
142- assert os .path .abspath (os .curdir ) == str (
143- mongo_dir .absolute ()
144- ), "buildifier.py must be run from the root of the mongo repo"
145151 binary_name = "buildifier.exe" if platform .system () == "Windows" else "buildifier"
146152 if args .binary_dir :
147153 binary_path = os .path .join (args .binary_dir , binary_name )
0 commit comments