commands: add 'west topdir' command

This just prints the top level directory of the west installation.

To fit it into the 'west help' output, tweak the section headings.

Signed-off-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Marti Bolivar 2019-09-26 13:08:59 -06:00
parent fc690257d1
commit a335ea0510
3 changed files with 50 additions and 3 deletions

View File

@ -691,6 +691,25 @@ class ForAll(_ProjectCommand):
self._handle_failed(args, failed)
class Topdir(_ProjectCommand):
def __init__(self):
super().__init__(
'topdir',
'print the top level directory of the installation',
textwrap.dedent('''\
Prints the absolute path of the current west installation's
top directory.
This is the directory containing .west. All project
paths in the manifest are relative to this top directory.'''))
def do_add_parser(self, parser_adder):
return self._parser(parser_adder)
def do_run(self, args, user_args):
log.inf(self.topdir)
class SelfUpdate(_ProjectCommand):
def __init__(self):
super().__init__(

View File

@ -29,14 +29,14 @@ from west import configuration as config
from west.commands import extension_commands, \
CommandError, CommandContextError, ExtensionCommandError
from west.commands.project import List, ManifestCommand, Diff, Status, \
SelfUpdate, ForAll, Init, Update
SelfUpdate, ForAll, Init, Update, Topdir
from west.commands.config import Config
from west.manifest import Manifest, MalformedConfig, MalformedManifest
from west.util import quote_sh_list, west_topdir, WestNotFound
from west.version import __version__
BUILTIN_COMMANDS = {
'commands for managing multiple git repositories': [
'built-in commands for managing git repositories': [
Init(),
Update(),
List(),
@ -46,7 +46,10 @@ BUILTIN_COMMANDS = {
ForAll(),
],
'configuring west': [Config()],
'other built-in commands': [
Config(),
Topdir(),
],
# None is for hidden commands we don't want to show to the user.
None: [SelfUpdate()]

View File

@ -447,6 +447,31 @@ def test_extension_command_duplicate(repos_tmpdir):
assert actual == expected
def test_topdir_none(tmpdir):
# Running west topdir outside of any installation ought to fail.
tmpdir.chdir()
with pytest.raises(subprocess.CalledProcessError):
cmd('topdir')
def test_topdir_in_installation(west_init_tmpdir):
# Running west topdir anywhere inside of an installation ought to
# work, and return the same thing.
expected = str(west_init_tmpdir)
# This should be available immediately after west init.
assert cmd('topdir').strip() == expected
# After west update, it should continue to work, and return the
# same thing (not getting confused when called from within a
# project directory or a random user-created subdirectory, e.g.)
cmd('update')
assert cmd('topdir', cwd=str(west_init_tmpdir / 'subdir' /
'Kconfiglib')).strip() == expected
west_init_tmpdir.mkdir('pytest-foo')
assert cmd('topdir', cwd=str(west_init_tmpdir /
'pytest-foo')).strip() == expected
#
# Helper functions used by the test cases and fixtures.