xref: /aosp_15_r20/tools/asuite/atest/constants.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1# Copyright 2017, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Imports the various constant files that are available (default, google, etc)."""
16# pylint: disable=wildcard-import
17# pylint: disable=unused-wildcard-import
18
19import json
20import os
21import pathlib
22import sys
23from atest.constants_default import *
24
25
26def _load_asuite_python_paths():
27  """Load additional python paths to module find path.
28
29  When atest is built with embedded mode, the PYTHONPATH is ignored. We use
30  this function to add the paths to the module search paths. Specifically, we
31  only need to add the asuite python paths so that we can load the
32  `constants_google` module.
33  """
34  python_paths = os.environ.get('PYTHONPATH', '').split(':')
35  for python_path in python_paths:
36    if 'asuite' in python_path and python_path not in sys.path:
37      sys.path.append(python_path)
38
39
40_load_asuite_python_paths()
41
42# Now try to import the various constant files outside this repo to overwrite
43# the globals as desired.
44# pylint: disable=g-import-not-at-top
45try:
46  from constants_google import *
47except ImportError:
48  pass
49
50
51# Note: This is part of the work to eventually replace the dangling import of
52# constants_google entirely. We will start with migrating the constants to json
53# and source code. In the future, we will migrate to use a config object instead
54# of relying on composing the constants module.
55def _load_vendor_config():
56  """Load the atest vendor configs from json path if available."""
57
58  config_path = os.environ.get('ATEST_VENDOR_CONFIG_PATH', None)
59  if config_path:
60    with open(config_path, 'r') as config_file:
61      globals().update(json.load(config_file))
62    return
63
64  build_top = os.environ.get('ANDROID_BUILD_TOP', None)
65  if not build_top:
66    return
67  # Load from hard-coded relative path as a transition as some users may not
68  # have re-run envsetup after repo sync.
69  config_path = pathlib.Path(build_top).joinpath(
70      'vendor/google/tools/atest/atest_vendor_configs.json'
71  )
72  if config_path.exists():
73    sys.stderr.write(
74        '\n\nWarning: Detected vendor setup script updated but not loaded.'
75        ' Please re-run the repo\'s envsetup script through ".'
76        ' build/envsetup".\n\n\n'
77    )
78    with open(config_path, 'r') as config_file:
79      globals().update(json.load(config_file))
80
81
82_load_vendor_config()
83