Skip to content

Commit

Permalink
Use imported module instead of PYTHONPATH
Browse files Browse the repository at this point in the history
  • Loading branch information
oxve committed Apr 26, 2024
1 parent 2da96f2 commit ee53c69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
5 changes: 2 additions & 3 deletions cobalt/build/gn.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pathlib import Path
from typing import List

from starboard.build.is_on_path import is_first_on_pythonpath
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 @@ -52,8 +52,7 @@ def main(out_directory: str, platform: str, build_type: str,


if __name__ == '__main__':
assert is_first_on_pythonpath(
__file__), 'Current repo is not first on PYTHONPATH.'
assert is_on_same_path(__file__), 'Current repo is not first on PYTHONPATH.'

parser = argparse.ArgumentParser()

Expand Down
23 changes: 11 additions & 12 deletions starboard/build/is_on_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

import os

SRC_ROOT = src_root_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))

def is_first_on_pythonpath(file_path):

def is_on_same_path(file_path):
"""
Checks if the passed file path is in the first entry on PYTHONPATH.
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.
Expand All @@ -28,20 +31,16 @@ def is_first_on_pythonpath(file_path):
True if the file is in the first entry on PYTHONPATH, False otherwise.
"""
try:
src_root_dir = os.path.join(
os.path.dirname(file_path), os.pardir, os.pardir)
pythonpath = os.environ['PYTHONPATH']
if not pythonpath:
return False

first_path = pythonpath.split(os.pathsep)[0]
return os.path.abspath(first_path) == os.path.abspath(src_root_dir)
return os.path.abspath(file_path).startswith(SRC_ROOT)
except ImportError:
return False


if __name__ == '__main__':
try:
print(str(is_first_on_pythonpath(__file__)).lower())
except KeyError:
import starboard.build.is_on_path # pylint: disable=import-outside-toplevel
# 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')

0 comments on commit ee53c69

Please sign in to comment.