zephyr/scripts/west

93 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python3
# Zephyr launcher which is interoperable with:
#
# 1. "mono-repo" Zephyr installations that have 'make flash'
# etc. supplied by a copy of some west code in scripts/meta.
#
# 2. "multi-repo" Zephyr installations where west is provided in a
# separate Git repository elsewhere.
#
# This is basically a copy of the "wrapper" functionality in the west
# bootstrap script for the multi-repo case, plus a fallback onto the
# copy in scripts/meta/west for mono-repo installs.
import os
import sys
import colorama
if sys.version_info < (3,):
sys.exit('fatal error: you are running Python 2')
# Top-level west directory, containing west itself and the manifest.
WEST_DIR = 'west'
# Subdirectory to check out the west source repository into.
WEST = 'west'
# File inside of WEST_DIR which marks it as the top level of the
# Zephyr project installation.
#
# (The WEST_DIR name is not distinct enough to use when searching for
# the top level; other directories named "west" may exist elsewhere,
# e.g. zephyr/doc/west.)
WEST_MARKER = '.west_topdir'
class WestNotFound(RuntimeError):
'''Neither the current directory nor any parent has a West installation.'''
def find_west_topdir(start):
'''Find the top-level installation directory, starting at ``start``.
If none is found, raises WestNotFound.'''
cur_dir = start
while True:
if os.path.isfile(os.path.join(cur_dir, WEST_DIR, WEST_MARKER)):
return cur_dir
parent_dir = os.path.dirname(cur_dir)
if cur_dir == parent_dir:
# At the root
raise WestNotFound()
cur_dir = parent_dir
def wrap(west_dir, argv):
# Pull in the west main module, after adding the directory
# containing the package to sys.path.
sys.path.append(west_dir)
import west.main
# Invoke west's main with our arguments. It needs to be run from
# this process for 'west debug' to work properly, so don't change
# this code to running main in a subprocess.
west.main.main(sys.argv[1:])
def main():
# Figure out which west to run. If we're in a multirepo
# installation, prefer the standalone west. Otherwise, we're in a
# monorepo installation, so we need to fall back on the copy of
# west in the Zephyr repository's scripts/meta directory.
try:
topdir = find_west_topdir(__file__)
west_dir = os.path.join(topdir, 'west', 'west', 'src')
except WestNotFound:
west_dir = os.path.join(os.environ['ZEPHYR_BASE'], 'scripts', 'meta')
try:
wrap(west_dir, sys.argv[1:])
finally:
print(colorama.Fore.LIGHTRED_EX, end='')
print('NOTE: you just ran a copy of west from {};'.
format(os.path.dirname(__file__)),
'this will be removed from the Zephyr repository in the future.',
'West is now developed separately.')
print(colorama.Style.RESET_ALL, end='', flush=True)
if __name__ == '__main__':
main()