west manifest: detect when target directory already exists, and fail

When setting up a project with west, the target directory may not be
initialized correctly. In the typical case, if a directory named
`./zephyr/` already exists, the user may find that checkout files are
located at `./zephyr/manifest-tmp/*` instead of the expected
`./zephyr/*`.

This patch will abort and refuse to complete `west init` if the
destination directory alread exists. This check would ideally occur
before the potentially lengthy clone operation, but `manifest_path` is
derived from the files retrieved...

NOTE: If the project quotes a value other than `zephyr` for
`manifest.self.path` in `/west.yml`, then this will affect that
directory instead.

Steps to reproduce before this patch:

  mkdir ./zephyr/
  west init ./ -m https://github.com/zephyrproject-rtos/zephyr.git
  ls -l ./zephyr/

Possible fix for some of the symptoms described in #558

Signed-off-by: Attie Grande <attie.grande@argentum-systems.co.uk>
This commit is contained in:
Attie Grande 2023-08-03 23:22:23 +01:00 committed by Martí Bolívar
parent 3e53dd599a
commit dacb54ba1d
1 changed files with 9 additions and 0 deletions

View File

@ -325,6 +325,15 @@ With neither, -m {MANIFEST_URL_DEFAULT} is assumed.
self.dbg('moving', tempdir, 'to', manifest_abspath,
level=Verbosity.DBG_EXTREME)
# As shutil.move() is used to relocate tempdir, if manifest_abspath
# is an existing directory, tmpdir will be moved _inside_ it, instead
# of _to_ that path - this must be avoided. If manifest_abspath exists
# but is not a directory, then semantics depend on os.rename(), so
# avoid that too...
if manifest_abspath.exists():
self.die(f'target directory already exists ({manifest_abspath})')
manifest_abspath.parent.mkdir(parents=True, exist_ok=True)
try:
shutil.move(os.fspath(tempdir), os.fspath(manifest_abspath))