Skip to content

Commit

Permalink
Improve PYTHONPATH checker (youtube#3085)
Browse files Browse the repository at this point in the history
Refactor to make checker work when imported and called from other
module.
Also add to gn.py.

b/299098707
  • Loading branch information
oxve authored May 14, 2024
1 parent 286e132 commit 5652cda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cobalt/build/gn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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()
Expand Down
29 changes: 20 additions & 9 deletions starboard/build/is_on_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 5652cda

Please sign in to comment.