Repository navigation
Expand file tree
/
Copy pathenv.py
More file actions
237 lines (183 loc) · 7.34 KB
/
Copy pathenv.py
File metadata and controls
237 lines (183 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# -*- coding:utf-8 -*-
#
# File : env.py
# This file is part of RT-Thread RTOS
# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Change Logs:
# Date Author Notes
# 2018-5-28 SummerGift Add copyright information
# 2019-1-16 SummerGift Add chinese detection
# 2020-4-13 SummerGift refactoring
# 2025-1-27 bernard Add env.json for env information
# 2026-09-12 Dongly Add show_version banner with --info flag; migrate to info accessors
import os
import sys
import argparse
import logging
import subprocess
script_path = os.path.abspath(__file__)
mpath = os.path.dirname(script_path)
sys.path.insert(0, mpath)
from cmds import cmd_menuconfig, cmd_package, cmd_plugin, cmd_sdk, cmd_system, cmd_webui
from env_paths import get_env_root, get_package_root, get_rtt_root as resolve_rtt_root
from vars import Export
from info import get_name, get_version
from plugins.errors import PluginError
def run_command(args):
"""Translate legacy boolean results and preserve explicit exit codes."""
try:
result = args.func(args)
except PluginError as exc:
print('rt-env: %s' % exc, file=sys.stderr)
return exc.exit_code
except subprocess.CalledProcessError as exc:
print('rt-env: command failed (exit %s): %s' % (exc.returncode, exc.cmd), file=sys.stderr)
return exc.returncode if exc.returncode > 0 else 1
except (OSError, ValueError) as exc:
print('rt-env: %s' % exc, file=sys.stderr)
return 1
if isinstance(result, bool):
return 0 if result else 1
if isinstance(result, int):
return result if result >= 0 else 1
return 0
def show_version():
rtt_ver = get_rtt_verion()
rt_env_name, rt_env_ver = get_name(), get_version()
print('\033[1;36m===================================================================\033[0m')
print('\033[1;36m Welcome to %s %s\033[0m' % (rt_env_name, rt_env_ver))
print('\033[1;36m===================================================================\033[0m')
print('Environment Information:')
print(' - ENV_ROOT : %s' % get_env_root())
print(' - PKGS_ROOT: %s' % get_package_root())
if rtt_ver != (0, 0, 0):
print(' - RTT_ROOT : %s' % get_rtt_root())
print(' - BSP_ROOT : %s' % get_bsp_root())
print(' - RT-Thread Version: %d.%d.%d' % rtt_ver)
print('\033[1;36m===================================================================\033[0m')
def show_version_warning(is_show_version=True):
rtt_ver = get_rtt_verion()
if rtt_ver <= (5, 1, 0) and rtt_ver != (0, 0, 0):
if is_show_version:
show_version()
print('env v2.0 has made the following important changes:')
print('1. Upgrading Python version from v2 to v3')
print('2. Replacing kconfig-frontends with Python kconfiglib')
print('')
print(
'env v2.0 require python kconfiglib (install by \033[4mpip install kconfiglib\033[0m),\n'
'but env v1.5.x confilt with kconfiglib (please run \033[4mpip uninstall kconfiglib\033[0m)'
)
print('')
print(
'\033[1;31;40m** WARNING **\n'
'env v2.0 only FULL SUPPORT RT-Thread > v5.1.0 or master branch.\n'
'but you are working on RT-Thread V%d.%d.%d, please use env v1.5.x \033[0m' % rtt_ver,
)
print('===================================================================')
def init_argparse():
# 'rt-env' mirrors the [project.scripts] entry in pyproject.toml
parser = argparse.ArgumentParser(prog='rt-env', description=__doc__)
subs = parser.add_subparsers()
rt_env_name, rt_env_ver = get_name(), get_version()
env_ver_str = '%s %s' % (rt_env_name, rt_env_ver)
parser.add_argument('-v', '--version', action='version', version=env_ver_str)
parser.add_argument('--info', action='store_true', help='Show environment information')
for command in (cmd_system, cmd_menuconfig, cmd_package, cmd_sdk, cmd_plugin, cmd_webui):
command.add_parser(subs)
return parser
def init_logger(env_root):
log_format = "%(module)s %(lineno)d %(levelname)s %(message)s \n"
date_format = '%Y-%m-%d %H:%M:%S %a '
logging.basicConfig(
level=logging.WARNING,
format=log_format,
datefmt=date_format,
# filename=log_name
)
def get_rtt_verion():
import re
rtt_root = get_rtt_root()
if not rtt_root:
return (0, 0, 0)
if not os.path.isfile(os.path.join(rtt_root, "include", "rtdef.h")):
return (0, 0, 0)
major, minor, patch = 0, 0, 0
with open(os.path.join(rtt_root, "include", 'rtdef.h')) as kconfig:
lines = kconfig.readlines()
for i in range(len(lines)):
if "#define RT_VERSION_MAJOR" in lines[i]:
major = int(re.split(r"\s+", lines[i].strip())[2])
if "#define RT_VERSION_MINOR" in lines[i]:
minor = int(re.split(r"\s+", lines[i].strip())[2])
if "#define RT_VERSION_PATCH" in lines[i]:
patch = int(re.split(r"\s+", lines[i].strip())[2])
return (major, minor, patch)
def get_rtt_root():
return resolve_rtt_root(get_bsp_root())
def get_bsp_root():
return os.getcwd()
def export_environment_variable():
env_root = get_env_root()
pkgs_root = get_package_root()
bsp_root = get_bsp_root()
os.environ["ENV_ROOT"] = env_root
os.environ['PKGS_ROOT'] = pkgs_root
os.environ['PKGS_DIR'] = pkgs_root
os.environ['BSP_ROOT'] = bsp_root
os.environ['BSP_DIR'] = bsp_root
Export('env_root')
Export('pkgs_root')
Export('bsp_root')
def exec_arg(arg, argv=None):
arguments = sys.argv[1:] if argv is None else argv
return main([arg] + list(arguments))
def cmd_env_info(args):
"""Handle environment information display."""
show_version()
show_version_warning(False)
return 0
def main(argv=None):
parser = init_argparse()
args = parser.parse_args(argv)
if args.info:
return cmd_env_info(args)
# Check if any subcommand was provided
if not hasattr(args, 'func'):
# No subcommand provided, show help
parser.print_help()
return 0
show_version_warning()
export_environment_variable()
init_logger(get_env_root())
return run_command(args)
def menuconfig():
return exec_arg('menuconfig')
def pkgs():
return exec_arg('pkg')
def sdk():
return exec_arg('sdk')
def system():
return exec_arg('system')
def webui():
show_version_warning()
export_environment_variable()
init_logger(get_env_root())
return cmd_webui.main()
if __name__ == '__main__':
sys.exit(main())