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