diff --git a/cobalt/build/gn.py b/cobalt/build/gn.py index 57d38ecd2db1..b6c0f061a310 100755 --- a/cobalt/build/gn.py +++ b/cobalt/build/gn.py @@ -22,6 +22,7 @@ from pathlib import Path from typing import List +from starboard.build.is_on_path import is_on_same_path from starboard.build.platforms import PLATFORMS _BUILD_TYPES = ['debug', 'devel', 'qa', 'gold'] @@ -51,6 +52,8 @@ def main(out_directory: str, platform: str, build_type: str, if __name__ == '__main__': + assert is_on_same_path(__file__), 'Current repo is not first on PYTHONPATH.' + parser = argparse.ArgumentParser() builds_directory_group = parser.add_mutually_exclusive_group() diff --git a/starboard/build/is_on_path.py b/starboard/build/is_on_path.py index c9c149bd78b2..78ac89446eee 100644 --- a/starboard/build/is_on_path.py +++ b/starboard/build/is_on_path.py @@ -16,17 +16,28 @@ import os +SRC_ROOT = src_root_dir = os.path.abspath( + os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) -def main(): + +def is_on_same_path(file_path): + """ + Checks if the passed file path is in the same repo as this file. + + Args: + file_path: The path to the file that should be checked. + + Returns: + True if file_path is a file in the same repo as this file, otherwise False. + """ + return os.path.abspath(file_path).startswith(SRC_ROOT) + + +if __name__ == '__main__': try: - # Try to import this file and compare its path to the current file. import starboard.build.is_on_path # pylint: disable=import-outside-toplevel - this_file = os.path.realpath(__file__) - imported_file = os.path.realpath(starboard.build.is_on_path.__file__) - print(str(this_file == imported_file).lower()) + # Use imported function instead of calling directly to ensure imports are + # being made from the same repo as this file is in. + print(str(starboard.build.is_on_path.is_on_same_path(__file__)).lower()) except ImportError: print('false') - - -if __name__ == '__main__': - main()