Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_get_file: fix makedirs call when the lpath is relative and in the working directory #618

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

skshetry
Copy link
Contributor

@skshetry skshetry commented Apr 8, 2024

Let's say if lpath is file literal, a relative path and to be downloaded to
the current working directory, _get_file currently fails with FileNotFoundError
during os.makedirs call.

await fs._get_file('gs://bucket/file', 'file')

This happens because os.path.dirname() returns an empty string, which when passed to os.makedirs(),
it raises a FileNotFoundError.

>>> os.path.dirname('file')
''
>>> os.makedirs('')
FileNotFoundError: [Errno 2] No such file or directory: ''

The following is a part of a traceback that I get.

File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/fsspec/asyn.py", line 118, in wrapper
    return sync(self.loop, func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/fsspec/asyn.py", line 103, in sync
    raise return_result
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/fsspec/asyn.py", line 56, in _runner
    result[0] = await coro
                ^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/gcsfs/core.py", line 1491, in _get_file
    await self._get_file_request(u2, lpath, callback=callback, **kwargs)
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/decorator.py", line 221, in fun
    return await caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/gcsfs/retry.py", line 123, in retry_request
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/gcsfs/core.py", line 1472, in _get_file_request
    os.makedirs(os.path.dirname(lpath), exist_ok=True)
File "<frozen os>", line 225, in makedirs

FileNotFoundError: [Errno 2] No such file or directory: ''

The fix here is to fallback to os.curdir on an empty string.

…rking directory

Let's say if `lpath` is `file` literal, a relative path and to be downloaded to
the current working directory, `_get_file` currently fails with `FileNotFoundError`
during `os.makedirs` call.
```python
await fs._get_file('gs://bucket/file', 'file')
```

This happens because `os.path.dirname()` returns an empty string, which when passed to `os.makedirs()`,
it raises a `FileNotFoundError`.

```py
>>> os.path.dirname('file')
''
>>> os.makedirs('')
FileNotFoundError: [Errno 2] No such file or directory: ''
```

The following is a part of a traceback that I get.

```pytb
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/fsspec/asyn.py", line 118, in wrapper
    return sync(self.loop, func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/fsspec/asyn.py", line 103, in sync
    raise return_result
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/fsspec/asyn.py", line 56, in _runner
    result[0] = await coro
                ^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/gcsfs/core.py", line 1491, in _get_file
    await self._get_file_request(u2, lpath, callback=callback, **kwargs)
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/decorator.py", line 221, in fun
    return await caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/gcsfs/retry.py", line 123, in retry_request
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/dev/solutions/checkpoints-gcp/.venv/lib/python3.11/site-packages/gcsfs/core.py", line 1472, in _get_file_request
    os.makedirs(os.path.dirname(lpath), exist_ok=True)
File "<frozen os>", line 225, in makedirs

FileNotFoundError: [Errno 2] No such file or directory: ''
```

The fix here is to fallback to `os.curdir` on an empty string.
Comment on lines +523 to +525
with tempdir() as dn:
os.makedirs(dn)
monkeypatch.chdir(dn)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used tempdir to be consistent with the other tests, but we could simplify this using tmp_path fixture and use monkeypatch.chdir() on it.

Suggested change
with tempdir() as dn:
os.makedirs(dn)
monkeypatch.chdir(dn)
monkeypatch.chdir(tmp_path)

@martindurant martindurant merged commit 17e649a into fsspec:main Apr 11, 2024
5 checks passed
@skshetry skshetry deleted the fix-get-file branch April 11, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants