From 9e3edbbd80cab0e67ef52817d87524214351e892 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Tue, 25 Sep 2018 15:08:16 -0600 Subject: [PATCH] scripts: fix west launcher for macOS The $(readlink -f) call in scripts/west doesn't work on macOS. Let's just ensure compatibility on all the platforms by moving scripts/west-launcher.py to scripts/west, making it executable on macOS and Linux, and launching it from the Windows doskey macro. Signed-off-by: Marti Bolivar --- scripts/west | 109 +++++++++++++++++++++++++++++++++++++-- scripts/west-launcher.py | 103 ------------------------------------ zephyr-env.cmd | 4 +- 3 files changed, 106 insertions(+), 110 deletions(-) delete mode 100644 scripts/west-launcher.py diff --git a/scripts/west b/scripts/west index ddc0661d604..d7995b5c0be 100755 --- a/scripts/west +++ b/scripts/west @@ -1,6 +1,105 @@ -#!/bin/sh +#!/usr/bin/env python3 -# Zephyr meta-tool (west) launcher alias, which keeps -# monorepo Zephyr installations' 'make flash' etc. working. -here=$(readlink -f $(dirname $0)) -python3 "$here/west-launcher.py" $@ +# 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 subprocess +import sys + +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 WestError(RuntimeError): + pass + + +class WestNotFound(WestError): + '''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('Could not find a West installation ' + 'in this or any parent directory') + cur_dir = parent_dir + + +def append_to_pythonpath(directory): + pp = os.environ.get('PYTHONPATH') + os.environ['PYTHONPATH'] = ':'.join(([pp] if pp else []) + [directory]) + + +def wrap(topdir, argv): + # Replace the wrapper process with the "real" west + + # sys.argv[1:] strips the argv[0] of the wrapper script itself + west_git_repo = os.path.join(topdir, WEST_DIR, WEST) + argv = ([sys.executable, + os.path.join(west_git_repo, 'src', 'west', 'main.py')] + + argv) + + try: + append_to_pythonpath(os.path.join(west_git_repo, 'src')) + subprocess.check_call(argv) + except subprocess.CalledProcessError as e: + sys.exit(e.returncode) + + +def run_scripts_meta_west(): + try: + subprocess.check_call([sys.executable, + os.path.join(os.environ['ZEPHYR_BASE'], + 'scripts', 'meta', 'west', + 'main.py')] + sys.argv[1:]) + except subprocess.CalledProcessError as e: + sys.exit(e.returncode) + + +def main(): + try: + topdir = find_west_topdir(__file__) + except WestNotFound: + topdir = None + + if topdir is not None: + wrap(topdir, sys.argv[1:]) + else: + run_scripts_meta_west() + + +if __name__ == '__main__': + main() diff --git a/scripts/west-launcher.py b/scripts/west-launcher.py deleted file mode 100644 index c46e1936144..00000000000 --- a/scripts/west-launcher.py +++ /dev/null @@ -1,103 +0,0 @@ -# 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 subprocess -import sys - -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 WestError(RuntimeError): - pass - - -class WestNotFound(WestError): - '''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('Could not find a West installation ' - 'in this or any parent directory') - cur_dir = parent_dir - - -def append_to_pythonpath(directory): - pp = os.environ.get('PYTHONPATH') - os.environ['PYTHONPATH'] = ':'.join(([pp] if pp else []) + [directory]) - - -def wrap(topdir, argv): - # Replace the wrapper process with the "real" west - - # sys.argv[1:] strips the argv[0] of the wrapper script itself - west_git_repo = os.path.join(topdir, WEST_DIR, WEST) - argv = ([sys.executable, - os.path.join(west_git_repo, 'src', 'west', 'main.py')] + - argv) - - try: - append_to_pythonpath(os.path.join(west_git_repo, 'src')) - subprocess.check_call(argv) - except subprocess.CalledProcessError as e: - sys.exit(e.returncode) - - -def run_scripts_meta_west(): - try: - subprocess.check_call([sys.executable, - os.path.join(os.environ['ZEPHYR_BASE'], - 'scripts', 'meta', 'west', - 'main.py')] + sys.argv[1:]) - except subprocess.CalledProcessError as e: - sys.exit(e.returncode) - - -def main(): - try: - topdir = find_west_topdir(__file__) - except WestNotFound: - topdir = None - - if topdir is not None: - wrap(topdir, sys.argv[1:]) - else: - run_scripts_meta_west() - - -if __name__ == '__main__': - main() diff --git a/zephyr-env.cmd b/zephyr-env.cmd index 1ea7467f272..752156865e3 100644 --- a/zephyr-env.cmd +++ b/zephyr-env.cmd @@ -11,7 +11,7 @@ rem https://www.python.org/dev/peps/pep-0486/ for details on the rem virtualenv-related pieces. (We need to implement this manually rem because Zephyr's minimum supported Python version is 3.4.) if defined VIRTUAL_ENV ( - doskey west=python %ZEPHYR_BASE%\scripts\west-launcher.py $* + doskey west=python %ZEPHYR_BASE%\scripts\west $* ) else ( - doskey west=py -3 %ZEPHYR_BASE%\scripts\west-launcher.py $* + doskey west=py -3 %ZEPHYR_BASE%\scripts\west $* )