1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2025 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import os 19from pathlib import Path 20from typing import ClassVar 21 22from dotenv import load_dotenv 23 24from runner.init_runner import InitRunner, MandatoryProps, MustExist, PropName 25from runner.logger import Log 26 27_LOGGER = Log.get_logger(__file__) 28 29MandatoryPropDescription = list[tuple[PropName, MustExist]] 30 31 32class RunnerEnv: 33 mandatory_props: ClassVar[MandatoryPropDescription] = [ 34 ('ARKCOMPILER_RUNTIME_CORE_PATH', True), 35 ('ARKCOMPILER_ETS_FRONTEND_PATH', True), 36 ('PANDA_BUILD', True), 37 ('WORK_DIR', False) 38 ] 39 urunner_path_name: ClassVar[str] = 'URUNNER_PATH' 40 41 def __init__(self, *, 42 local_env: Path | None = None, 43 global_env: Path = InitRunner.urunner_env_default, 44 urunner_path: Path | None = None): 45 self.global_env_path: Path = global_env 46 self.local_env_path: Path | None = local_env 47 self.urunner_path: Path | None = urunner_path 48 49 @staticmethod 50 def check_expand_mandatory_prop(var_name: PropName, must_exist: MustExist) -> None: 51 var_value = os.getenv(var_name) 52 if var_value is None: 53 raise OSError(f"Mandatory environment variable '{var_name}' is not set. " 54 "Either specify it in the system environment or " 55 "run the runner with the only option `--init`") 56 expanded = Path(var_value).expanduser().resolve() 57 if must_exist and not expanded.exists(): 58 raise OSError(f"Mandatory environment variable '{var_name}' is set " 59 f"to value {var_value} which does not exist.") 60 os.environ[var_value] = expanded.as_posix() 61 62 @classmethod 63 def get_mandatory_props(cls) -> MandatoryProps: 64 result: MandatoryProps = {} 65 for (prop_name, must_exist) in cls.mandatory_props: 66 result[prop_name] = os.getenv(prop_name), must_exist 67 return result 68 69 def load_environment(self) -> None: 70 self.load_home_env() 71 self.load_local_env() 72 73 for (prop, must_exist) in self.mandatory_props: 74 self.check_expand_mandatory_prop(prop, must_exist) 75 76 if self.urunner_path: 77 os.environ[self.urunner_path_name] = self.urunner_path.as_posix() 78 79 def load_local_env(self) -> None: 80 """ 81 Loads the local environment file .env located near the main.py 82 """ 83 if self.local_env_path and self.local_env_path.exists(): 84 load_dotenv(self.local_env_path) 85 86 def load_home_env(self) -> None: 87 """ 88 Loads the global environment file .urunner.env located at the user's home 89 """ 90 if self.global_env_path.exists(): 91 load_dotenv(self.global_env_path) 92