• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""The env module defines the environment variables used by Pigweed."""
15
16from pathlib import Path
17from typing import Optional
18
19from pw_cli import envparse
20
21
22def pigweed_environment_parser() -> envparse.EnvironmentParser:
23    """Defines Pigweed's environment variables on an EnvironmentParser."""
24    parser = envparse.EnvironmentParser(prefix='PW_')
25
26    parser.add_var('PW_BOOTSTRAP_PYTHON')
27    parser.add_var('PW_ENABLE_PRESUBMIT_HOOK_WARNING', default=False)
28    parser.add_var('PW_EMOJI', type=envparse.strict_bool, default=False)
29    parser.add_var('PW_ENVSETUP')
30    parser.add_var('PW_ENVSETUP_FULL')
31    parser.add_var(
32        'PW_ENVSETUP_NO_BANNER', type=envparse.strict_bool, default=False
33    )
34    parser.add_var(
35        'PW_ENVSETUP_QUIET', type=envparse.strict_bool, default=False
36    )
37    parser.add_var('PW_ENVIRONMENT_ROOT', type=Path)
38    parser.add_var('PW_PACKAGE_ROOT', type=Path)
39    parser.add_var('PW_PROJECT_ROOT', type=Path)
40    parser.add_var('PW_ROOT', type=Path)
41    parser.add_var(
42        'PW_DISABLE_ROOT_GIT_REPO_CHECK',
43        type=envparse.strict_bool,
44        default=False,
45    )
46    parser.add_var('PW_SKIP_BOOTSTRAP')
47    parser.add_var('PW_SUBPROCESS', type=envparse.strict_bool, default=False)
48    parser.add_var('PW_USE_COLOR', type=envparse.strict_bool, default=False)
49    parser.add_var('PW_USE_GCS_ENVSETUP', type=envparse.strict_bool)
50
51    parser.add_allowed_suffix('_CIPD_INSTALL_DIR')
52
53    parser.add_var(
54        'PW_ENVSETUP_DISABLE_SPINNER', type=envparse.strict_bool, default=False
55    )
56    parser.add_var('PW_DOCTOR_SKIP_CIPD_CHECKS')
57    parser.add_var(
58        'PW_ACTIVATE_SKIP_CHECKS', type=envparse.strict_bool, default=False
59    )
60
61    parser.add_var('PW_BANNER_FUNC')
62    parser.add_var('PW_BRANDING_BANNER')
63    parser.add_var('PW_BRANDING_BANNER_COLOR', default='magenta')
64
65    parser.add_var(
66        'PW_PRESUBMIT_DISABLE_SUBPROCESS_CAPTURE', type=envparse.strict_bool
67    )
68
69    parser.add_var('PW_CONSOLE_CONFIG_FILE')
70    parser.add_var('PW_ENVIRONMENT_NO_ERROR_ON_UNRECOGNIZED')
71
72    parser.add_var('PW_CIPD_SERVICE_ACCOUNT_JSON')
73
74    # RBE environment variables
75    parser.add_var('PW_USE_RBE', default=False)
76    parser.add_var('PW_RBE_DEBUG', default=False)
77    parser.add_var('PW_RBE_CLANG_CONFIG', default='')
78    parser.add_var('PW_RBE_ARM_GCC_CONFIG', default='')
79
80    return parser
81
82
83# Internal: memoize environment parsing to avoid unnecessary computation in
84# multiple calls to pigweed_environment().
85_memoized_environment: Optional[envparse.EnvNamespace] = None
86
87
88def pigweed_environment() -> envparse.EnvNamespace:
89    """Returns Pigweed's parsed environment."""
90    global _memoized_environment  # pylint: disable=global-statement
91
92    if _memoized_environment is None:
93        _memoized_environment = pigweed_environment_parser().parse_env()
94
95    return _memoized_environment
96