Coverage for apio/utils/env_options.py: 94%

26 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +0000

1# -*- coding: utf-8 -*- 

2# -- This file is part of the Apio project 

3# -- (C) 2016-2018 FPGAwars 

4# -- Author Jesús Arroyo 

5# -- License GPLv2 

6# -- Derived from: 

7# ---- Platformio project 

8# ---- (C) 2014-2016 Ivan Kravets <me@ikravets.com> 

9# ---- License Apache v2 

10"""Functions for reading the APIO env options. This are system env 

11variables that are used to modify the default behavior of APIO. 

12""" 

13 

14import os 

15from typing import List 

16 

17# -- Env variable to override the apio home dir ~/.apio. If specified, 

18# -- it contains the profile.json file and if APIO_PACKAGES is not 

19# -- specified, the 'packages' directory with the individual packages. 

20# -- This env variable is used in testing to have an isolated home for each 

21# -- test. 

22APIO_HOME = "APIO_HOME" 

23 

24# -- Env variable to override the apio packages dir <apio_home>/packages. 

25# -- it contains the apio packages loaded from github. If specified, the 

26# -- path must must contains the string 'packages' for safety. This env 

27# -- variable used in testing to share downloaded packages across tests. 

28APIO_PACKAGES = "APIO_PACKAGES" 

29 

30# -- Env variable to override the platform id that is determined automatically 

31# -- from the system properties. If specified, the value should match one 

32# -- of the platforms specified in resources/platforms.json. 

33APIO_PLATFORM = "APIO_PLATFORM" 

34 

35# -- Env variable to enable printing of debug info, valid values are from 

36# -- 1 to 10 with 1 for minimal debug info and 10 for maximum. 

37# -- 

38# -- Do not access it directly. For the apio process use util.is_debug(n) and 

39# -- for the scons process use scons_util.is_debug(n). 

40APIO_DEBUG = "APIO_DEBUG" 

41 

42# -- An env variable that if defined, contains an override url of the remote 

43# -- config file defined in apio/resources/config.jsonc. During automated 

44# -- tests, it is overridden to point to the local copy of the remote config 

45# -- file. 

46# 

47# Examples: 

48# file:///projects/apio-dev/repo/remote-config/apio-0.9.7.jsonc 

49# file:///projects/apio-dev/repo/remote-config/apio-{V}.jsonc 

50# https://github.com/zapta/apio_dev/raw/develop/remote-config/apio-{V}.jsonc 

51# 

52APIO_REMOTE_CONFIG_URL = "APIO_REMOTE_CONFIG_URL" 

53 

54 

55# -- List of all supported env options. 

56_SUPPORTED_APIO_VARS = [ 

57 APIO_HOME, 

58 APIO_PACKAGES, 

59 APIO_PLATFORM, 

60 APIO_DEBUG, 

61 APIO_REMOTE_CONFIG_URL, 

62] 

63 

64 

65def get(var_name: str, default: str = None): 

66 """Return the given APIO config env value or default if not found. 

67 var_name must be in _SUPPORTED_APIO_VARS. The returned 

68 value is not cached such that mutating the var in this program will 

69 affect the result of this function. 

70 """ 

71 

72 # -- Sanity check. To make sure we are aware of all the vars used. 

73 assert ( 

74 var_name in _SUPPORTED_APIO_VARS 

75 ), f"Unknown apio env var '{var_name}'" 

76 

77 # -- Get the value, None if not defined. 

78 var_value = os.getenv(var_name) 

79 

80 if var_value is None: 

81 # -- Var is undefined. Use default 

82 var_value = default 

83 else: 

84 # -- Var is defined. For windows benefit, remove optional quotes. 

85 if var_value.startswith('"') and var_value.endswith('"'): 85 ↛ 86line 85 didn't jump to line 86 because the condition on line 85 was never true

86 var_value = var_value[1:-1] 

87 

88 return var_value 

89 

90 

91def is_defined(var_name) -> bool: 

92 """Returns true if the env var is currently defined, regardless to its 

93 value, or False otherwise. var_name must be in _SUPPORTED_APIO_VARS. The 

94 returned value is not cached such that mutating the var in this program may 

95 affect the result of this function.""" 

96 # -- Sanity check. To make sure we are aware of all the vars used. 

97 assert ( 

98 var_name in _SUPPORTED_APIO_VARS 

99 ), f"Unknown apio env var '{var_name}'" 

100 

101 # -- Get the value, None if not defined. 

102 var_value = os.getenv(var_name) 

103 return var_value is not None 

104 

105 

106def get_defined() -> List[str]: 

107 """Return the list of apio env options vars in _SUPPORTED_APIO_VARS 

108 that are currently defined. The returned value is not cached such that 

109 mutating the var in this program may affect the result.""" 

110 result = [] 

111 for var in _SUPPORTED_APIO_VARS: 

112 if is_defined(var): 

113 result.append(var) 

114 return result