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

27 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +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 

15 

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

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

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

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

20# -- test. 

21APIO_HOME = "APIO_HOME" 

22 

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

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

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

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

27APIO_PACKAGES = "APIO_PACKAGES" 

28 

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

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

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

32APIO_PLATFORM = "APIO_PLATFORM" 

33 

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

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

36# -- 

37# -- Do not access it directly, use debug_util.is_debug(n) instead. 

38APIO_DEBUG = "APIO_DEBUG" 

39 

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

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

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

43# -- file. 

44# 

45# Examples: 

46# file:///projects/apio-dev/repo/remote-config/apio-1.1.x.jsonc 

47# file:///projects/apio-dev/repo/remote-config/apio-{major}.{minor}.x.jsonc 

48# https://github.com/zapta/apio_dev/raw/main/remote-config/apio-{major}.{minor}.x.jsonc 

49# 

50APIO_REMOTE_CONFIG_URL = "APIO_REMOTE_CONFIG_URL" 

51 

52 

53# -- List of all supported env options. 

54_SUPPORTED_APIO_VARS = [ 

55 APIO_HOME, 

56 APIO_PACKAGES, 

57 APIO_PLATFORM, 

58 APIO_REMOTE_CONFIG_URL, 

59 APIO_DEBUG, 

60] 

61 

62 

63def get(var_name: str, default: str | None = None): 

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

65 var_name must be in _SUPPORTED_APIO_VARS. The returned 

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

67 affect the result of this function. 

68 """ 

69 

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

71 assert ( 

72 var_name in _SUPPORTED_APIO_VARS 

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

74 

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

76 var_value = os.getenv(var_name) 

77 

78 if var_value is None: 

79 # -- Var is undefined. Use default 

80 var_value = default 

81 else: 

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

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

84 var_value = var_value[1:-1] 

85 

86 return var_value 

87 

88 

89def is_defined(var_name) -> bool: 

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

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

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

93 affect the result of this function.""" 

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

95 assert ( 

96 var_name in _SUPPORTED_APIO_VARS 

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

98 

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

100 var_value = os.getenv(var_name) 

101 return var_value is not None 

102 

103 

104def get_defined() -> list[str]: 

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

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

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

108 result = [] 

109 for var in _SUPPORTED_APIO_VARS: 

110 if is_defined(var): 

111 result.append(var) 

112 return result 

113 

114 

115def get_all() -> list[str]: 

116 """Return the list of all of apio supported env vars.""" 

117 return _SUPPORTED_APIO_VARS.copy()