Skip to content

Commit

Permalink
west manifest: detect when target directory already exists, and fail
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
attie-argentum authored and mbolivar-ampere committed Aug 31, 2023
1 parent 3e53dd5 commit dacb54b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ def bootstrap(self, args) -> Path:

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))
Expand Down

0 comments on commit dacb54b

Please sign in to comment.