shiv icon indicating copy to clipboard operation
shiv copied to clipboard

Error if output path doesn't exist

Open vitoyucepi opened this issue 2 years ago • 1 comments

Summary

If the output path doesn't exist, then shiv will fail with an error FileNotFoundError: [Errno 2] No such file or directory.

Log

Traceback (most recent call last):
  File "/tmp/test/.venv/bin/shiv", line 8, in <module>
    sys.exit(main())
  File "/tmp/test/.venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/tmp/test/.venv/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/tmp/test/.venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/tmp/test/.venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/tmp/test/.venv/lib/python3.10/site-packages/shiv/cli.py", line 269, in main
    builder.create_archive(
  File "/tmp/test/.venv/lib/python3.10/site-packages/shiv/builder.py", line 94, in create_archive
    with target.open(mode="wb") as fd:
  File "/usr/lib/python3.10/pathlib.py", line 1119, in open
    return self._accessor.open(self, mode, buffering, encoding, errors,
FileNotFoundError: [Errno 2] No such file or directory: 'dist/main'

To reproduce

  1. python -m venv .venv
  2. . .venv/bin/activate
  3. pip install shiv==1.0.3
  4. echo "print('hello world')" >> __main__.py
  5. shiv -e __main__.py -o dist/main --site-packages .venv/lib/python3.10/site-packages/

Expected behavior

Shiv should create leading directories if they do not exist.

Versions

  • python: 3.10
  • shiv: 1.0.3

Additional

Maybe something like

target_dir = os.path.dirname(target)
if not os.path.exists(target_dir):
  os.makedirs(target_dir)

should be inserted somewhere here https://github.com/linkedin/shiv/blob/eeec71f31a08203cffd184602eb6d10e30634126/src/shiv/builder.py#L93-L94

vitoyucepi avatar Jan 30 '23 06:01 vitoyucepi

Hi @vitoyucepi Thanks for the report, I agree that shiv could & should create the path if it doesn't exist. I'm happy to make this change, but I would also accept a PR with the same, assuming it comes with a test :)

lorencarvalho avatar Feb 01 '23 01:02 lorencarvalho