Commit Graph

391 Commits

Author SHA1 Message Date
Marti Bolivar 3ddfd0d090 commands: fine-grained extension command error handling
Rename BadExternalCommand to ExtensionCommandError and make it a
subclass of CommandError so that it fits naturally in the hierarchy
and provides a returncode.

Add a hint attribute to it so that we can print further diagnostic
information to the user when things go wrong.

While we are here, use traceback.print_exc() in the error handlers at
main level to make it more clear that the verbosity is not altering
the control flow of the program, just what is being printed. Also use
the returncode attribute in a CalledProcessError since that is
available.

Print the entire command invocation that caused the problem with -v
added instead of making the user guess what to do.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-31 13:22:02 -07:00
Marti Bolivar a1c5f5087d commands: allow extension commands with arbitrary names
The restriction that extension commands have names which are valid
Python identifiers is arbitrary and unnecessarily restrictive.

For example, it should be possible to have a command with a dash (-)
in its name to separate words, like "submit-pr". This is currently not
possible.

Resolve that by generating module names from an infinite iterator of
fresh per-file values instead of relying on the command name. We don't
actually need the name! Just the canonical path of the file which
defines it is important to avoid double imports.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-31 13:22:02 -07:00
Marti Bolivar 71c5e7833d main: catch BadExternalCommand and print a sensible message
This is leftover work that we meant to do. It fixes up ugly warnings
in case the extension command is badly formed. The user can still get
a full stack trace with verbose operation enabled.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-31 13:22:02 -07:00
Torsten Rasmussen c5323934df selfupdate: Fix for west selfupdate mechanism
Fixes: #173

This commit adds sys.argv[0] when restarting west after an update.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-30 08:49:32 -07:00
Marti Bolivar 74ec604954 commands: update: add --keep-descendants, --rebase options
The base west update is sufficient to get work done, but it has
inconveniences:

1. When working on a local topic branch in a project, I may want to
   "west update" my local installation to sync with upstream, but not
   leave behind my topic branches if they are still up to date

   Make this possible by adding a -k / --keep descendants option,
   which leaves the checked out branch unaltered (including any
   uncommitted state in the working tree) if it's a descendant of the
   new manifest-rev.

2. Similarly, having to run "git rebase" by hand when updating can be
   annoying, especially if the rebases would have all succeeded and
   there are multiple projects with topic branches open.

   Avoid this when users explicitly opt in by adding a --rebase
   option, which attempts to rebase any project branches onto the new
   manifest-rev.

If both -k and -r are given and applicable to a project, -k wins.

Since the west update output is much longer now, refactor the code and
make some of the warning messages less verbose.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 15:09:46 -07:00
Marti Bolivar 97f2d8e85b main: fix bugs / remove hacks in extension command handling
Rather than try to use the same argument parser for figuring out which
extension command to run and actually creating it and parsing its real
arguments, just recreate the top level parser and re-parse the
original argv when handling extension commands.

This makes things like 'west -v SOME_EXTENSION_COMMAND' work again.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 15:09:46 -07:00
Marti Bolivar f3650644f2 commands: update: fix help
Fix incorrect help text and remove an unused variable.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 15:09:46 -07:00
Marti Bolivar ba6d1ac457 commands: fix for naming invalid project from project directory
The behavior of 'west list SOME_INVALID_PROJECT' (and any other
commands which try to match SOME_INVALID_PROJECT with real project)
is incorrect when called from a project directory: the current
project is found instead of an error being returned.

Fix this by requiring that, when treated as a path, the given argument
exactly normalizes to a project directory. Otherwise, it is possible
for it to match nonexistent files inside of the existing project
directory.

Add some more testing. The ones that expect a CalledProcessError fail
without this project.py patch. With it, they all pass.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 15:09:46 -07:00
Marti Bolivar 6b5cee1370 commands: add CommandError class and handle it
Generalize CommandContextError with a general purpose superclass.
Add a returncode attribute that main can use to pass to sys.exit().

This will be used in later patches.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 15:09:46 -07:00
Marti Bolivar 7a44bd270f West v0.5.1
Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 15:00:28 -07:00
Marti Bolivar a74b9f1dcc bootstrapper: improve heuristic for --zephyr-base
We're seeing warnings when people have ZEPHYR_BASE set to the
"<directory>" they pass to "west init -l <directory>".

Avoid them exactly in the situation that <directory> is a zephyr
repository by using the manifest directory as ZEPHYR_BASE. For
upstream, that is correct anyway.

Merge a couple of lines while here as a cosmetic improvement.

Fixes: #167

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-29 21:47:39 +01:00
Torsten Rasmussen fcfada8b50 bootstrap: Removed second fetch and allow for SHA
This commit cleans up the second fetch, which potentially could have
changed content since first fetch.

Normal ref-space (similar to git clone) and user specified revision is
now fetched in same command.

This further allows to checkout tags in detached HEAD while still
referring to the tag name instead of FETCH_HEAD.

This commit also allows a user to specify a sha.
When specify a sha as revision, that sha must be valid in to the normal
ref-space.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-29 10:58:32 -07:00
Maciej Sobkowski 8caea7fceb README.rst: correct URL to west documentation
Signed-off-by: Maciej Sobkowski <msobkowski@antmicro.com>
2019-01-29 07:42:53 -07:00
Torsten Rasmussen 8679b43df0 bootstrap: allow bootstrapping a manifest repository using ref to a pr
Changing `west init` into using
- git init
- git fetch
- git checkout
as this allows users to specify other refs, such as pull/<no>/head
which is not possible with git clone on github.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-28 16:12:09 -07:00
Marti Bolivar 9a2dc19048 commands: allow extension commands to provide help strings
For various reasons, we don't load extension command modules until the
user has requested that we run a particular extension command.

As is, this limits the "west -h" output to the extension command names
and the projects which define them, which isn't as nice as what
built-in commands get.

Allow extension commands to provide help text by extending the schema
for each command entry with an optional "help" key, and display it in
the output. If it is missing, a default is provided which recommends
just running "west $EXTENSION_COMMAND_NAME -h".

Fix the help formatting by adding a colon after each extension command
name to match the style used for built-ins while we are here.

Keep test cases up to date.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-28 19:56:04 +01:00
Marti Bolivar b700061d66 West 0.5.0
Cut release 0.5.0. This is the initial tree provided to the wider
Zephyr developer community during the Zephyr v1.14 development cycle.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:29:02 -07:00
Marti Bolivar ff209d5692 README.rst: fixes for 0.5.0
Update some stale content.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:29:02 -07:00
Torsten Rasmussen c77a155860 extension command: Fixing issue with extension command duplication
This commit fixes:
- Issue if multiple projects defines the same command.
  This will print a warning for project/command not being available
- Fixed issue where same command could be listed multiple times

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:21:56 -07:00
Marti Bolivar c88aa9c227 commands: project: fix update help
Make the format match the rest of the commands and fix a typo.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:06:42 -07:00
Marti Bolivar 0d39b6b77a west: main: tweak external command help
The current output looks kind of silly. Tweak it and add a TODO
comment for further noncritical improvements.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:06:42 -07:00
Marti Bolivar 2908ede7a7 west.commands: make it easier for external commands to import code
Add the python file's directory to sys.path, to make it easier to
import other modules in the same directory.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:06:42 -07:00
Marti Bolivar c746538960 Remove runner package and related commands and tests
These are moving back to the zephyr repository as west extension
commands. This will (finally) let us remove the copy of west inside
zephyr to make those features available, and lets us keep a separate
west repository for bootstrapping and multirepo management while still
making it easier for people to just edit their runners.

Since this just leaves project and hidden commands, we don't print
indented groups of built-in commands, as that would be ugly and not
make sense.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-25 10:06:42 -07:00
Torsten Rasmussen a83963726a extension_commands: Added test and allow for projects not cloned
Added tests to ensure missing extension files (in case a project in
not cloned) will not cause west built-in commands to fail.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:43:07 -07:00
Torsten Rasmussen c767957faa manifest-test: Adjusted assert command to use os.path.sep
On windows a couple of manifest test cases were failing due to path
delimiter issues.
This commit fixes those test cases by using os.path.sep instead of '/'

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen c6fbe31591 init: Added additional tests for west init -l
This commit creates additional tests for 'west init -l' to ensure
behaviour when initializing west around an existing manifest
repository.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen 6005797b21 init: Added ability to initialize west with an existing manifest repo
Ability to initialize west in a folder which contains a manifest
project (as example by an earlier 'git clone') but which is in west
context uninitialized.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen d88b502b28 config file: Removed url and revision from west config file
Removed url and revision from west config file from west config file as
they are no longer needed when manifest repository can be handled
directly with git

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen 42b24d3cfd bootstrap: Removed support for reinitialization
Removed the support for reinitialization of project in west

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen 3ee9f3dbcd project: Added new update command
New update command will clone, fetch, and checkout a detached HEAD
according to manifest file.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen cadc683539 commands: several project commands removed according to new design
Following commands has been removed:
- clone
- fetch
- pull
- rebase
- branch
- checkout

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Torsten Rasmussen f052123ce1 projects: Renamed west update to west selfupdate
Renamed 'west update' to 'west selfupdate' to keep it separated from
future update command.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-24 08:03:46 -07:00
Marti Bolivar 36b1b87a55 main: add hack to fix extension commands
See comments in the patch for details.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-23 08:01:31 -07:00
Marti Bolivar 416cfe53d1 tests: add basic tests for external commands
These should really be expanded.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar 7ddd6af983 west.main: wire up support for external commands
Add logic to retrieve external command specifications grouped by the
project which provides them. Filter these so built-in commands never
get shadowed, and wire them up to the argument parsing and help
printing logic. Add a special-case command handler for these which
re-parses command line arguments after instantiating the command
object and installing its parser.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar 561dafaedd west: add infrastructure for external commands
Add support for a new optional 'west-commands' key in each project in
a manifest. This specifies the location of a file which describes
additional west commands provided by that project.

Extend the Manifest class to detect this key and record the
information inside. Nothing else is done about external commands in
that class, since its purpose is just to parse the manifest.

Extend west.commands with a new external_commands() function, which
consumes a Manifest and returns a list of "descriptors" of external
commands the manifest's projects declare. These descriptors contain
metadata about the commands, but do not actually import any Python
modules related to them or otherwise try to instantiate the relevant
WestCommand instances.

This is for two reasons:

1. Performance: don't import what you might not need

2. Security: don't import random code you downloaded somewhere on the
   internet! Importing a module you got from somewhere random is
   basically curl | sh.

This infrastructure will be wired into main() in a subsequent patch.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar eff8f1561b west.util: add escapes_directory() helper
This function helps us detect directory traversal attacks.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar 2204906533 commands: group related commands together
Some utilities which use subcommands break them into groups in their
help output, to make it easier for the reader to find what they are
looking for. Two notable examples are docker and git.

Do the same thing for west, by tweaking the COMMANDS type to group
related commands together under a string key which describes the
group, and adding a custom argument parser subclass which uses this
information when formatting the top-level command help.

Add a group of "hidden" commands which is just PostInit for
now. Eventually that will be going away, and in the meantime, we'd
like to sweep it under the rug.

I would have preferred to do this by hooking into argparse itself, but
it is annoyingly opaque about what constitutes stable API.

The top-level "west -h" output now looks like this (without ">"s):

> $ west -h
> usage: west [-h] [-z ZEPHYR_BASE] [-v] [-V] <command> ...
>
> The Zephyr RTOS meta-tool.
>
> optional arguments:
>   -h, --help            show this help message and exit
>   -z ZEPHYR_BASE, --zephyr-base ZEPHYR_BASE
>                         Override the Zephyr base directory. The
>                         default is the manifest project with path
>                         "zephyr".
>   -v, --verbose         Display verbose output. May be given multiple
>                         times to increase verbosity.
>   -V, --version         print the program version and exit
>
> west commands used in various situations:
>  building and running zephyr:
>      build:             compile a Zephyr application
>      flash:             flash and run a binary on a board
>      debug:             flash and interactively debug a Zephyr
>                         application
>      debugserver:       connect to board and launch a debug server
>      attach:            interactively debug a board
>
>  managing multiple repositories in the installation:
>      list:              print information about projects in the west
>                         manifest
>      clone:             clone remote projects into local installation
>      fetch:             "git fetch" changes from project remotes
>      pull:              "git pull" changes from project remotes
>      rebase:            "git rebase" local projects onto manifest
>                         versions
>      branch:            create a branch in one or more local projects
>      checkout:          check out a branch in one or more local
>                         projects
>      diff:              "git diff" for one or more projects
>      status:            "git status" for one or more projects
>      update:            update the manifest and west repositories
>      forall:            run a command in one or more local projects
>
> Run "west <command> -h" for help on each command.

The command-level help (e.g. "west build -h") is not changed.

Fixes: #107
Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar 269ea4ee88 commands: add per-command help
This is a baby step on issue #107 (Improve `west --help` formatting).

Add per-command help output to each command. While we're here, rename
'init' to 'post-init' to avoid a name clash with the init command
handled by the bootstrapper.

The "commands:" help output now looks like this in west -h:

positional arguments:
  <command>
    build               compile a Zephyr application
    flash               flash and run a binary on a board
    debug               flash and interactively debug a Zephyr application
    debugserver         connect to board and launch a debug server
    attach              interactively debug a board
    post-init           finish init tasks
    list                print information about projects in the west manifest
    clone               clone remote projects into local installation
    fetch               "git fetch" changes from project remotes
    pull                "git pull" changes from project remotes
    rebase              "git rebase" local projects onto manifest versions
    branch              create a branch in one or more local projects
    checkout            check out a branch in one or more local projects
    diff                "git diff" for one or more projects
    status              "git status" for one or more projects
    update              update the manifest and west repositories
    forall              run a command in one or more local projects

It would be nice to be able to group these subparsers and hide the
post-init command, though, which we can't seem to do with plain
argparse.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar d9582ac752 project: remove support for -m option
In order to add support for extension commands, we will need to know
where the manifest is before we parse any command line arguments.

This means the -m argument supported by the existing project commands
has to go away. We'll always let the manifest module fetch the
contents from the location specified in the configuration file.

(The Manifest class is still capable of parsing manifests from
arbitrary file system paths.)

While we're here, add a validity check for the sections parameter to
the Manifest constructor and factory methods. This wasn't being used
properly: it should be a list. The fact that strings are sequences
means it basically gets silently ignored when passing a string.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar 431d8438fe manifest: remove META_NAMES
Let's not deal with META_NAMES anymore, which is only used once. We
can emit a nicer error message for the user without it.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar 03e0908f3e project: west is the only meta repository now
- Fix comments and project help messages that refer to the manifest
  as a distinct special repository
- Remove extra code that assumes multiple meta projects may exist

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Marti Bolivar af35c6a638 west: _bootstrap: clean up bootstrapping exceptions
Calling bootstrap() from the WestNotFound exception handler in init()
is making the WestNotFound show up in stack traces for errors in
bootstrap(), which makes for confusing output. Fix that by moving the
call to bootstrap() out.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-18 12:35:08 +01:00
Torsten Rasmussen c074732c42 bootstrapper: Ignore errors on temp folder cleanup
A git clone of manifest repository into temporary folder on windows
will contain read-only files.

This will cause shutil.rmtree to fail and abort bootstrapping.
Current fix is to ignore such errors on windows and rely on windows
temp folder cleanup.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-16 08:53:48 -07:00
Torsten Rasmussen 71992b53d5 manifest: Added MalformedConfig exception
For better handling of errors the Manifest class now raises a
MalformedConfig exception if required settings are missing in the west
config file

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-15 13:31:18 -07:00
Torsten Rasmussen 1390b99b94 west: topdir marker removed
Topdir marker '.west_topdir' removed.
West now determines the topdir by looking for the '.west' folder.

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-15 13:31:18 -07:00
Torsten Rasmussen d287e35f3c project: init command added to west main
Added west init command to west.main to support post-process of
west init.

Added --use-cache to limit network use when cloning manifest project
repository

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-15 13:31:18 -07:00
Torsten Rasmussen 0514c4a781 manifest: Manifest file inside a project repository
- self key introduced in manifest file to support local path location
  for the repository containing the manifest file
- Removed manifest repository under west folder

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-15 13:31:18 -07:00
Torsten Rasmussen 045b591e17 structure: Renamed west toplevel folder to .west
As part of moving manifest file inside a project repository, the west
folder is renamed to .west as it will no longer contain a manifest
repository (and potentially also not west repo in future)

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-15 13:31:18 -07:00
Torsten Rasmussen 572d412d94 manifest: Renamed manifest file from default.yml to west.yml
As part of moving manifest file inside a project reposity, the manifest
file is renamed from default.yml to west.yml

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-01-15 13:31:18 -07:00
Marti Bolivar f01059a2e5 config.py: add missing copyright and license
Trivial cleanup based on git blame.

Signed-off-by: Marti Bolivar <marti@foundries.io>
2019-01-10 10:08:08 -07:00