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

Question - how to interact with Windows OSFS paths? #526

Open
danizen opened this issue Mar 1, 2022 · 3 comments
Open

Question - how to interact with Windows OSFS paths? #526

danizen opened this issue Mar 1, 2022 · 3 comments
Labels

Comments

@danizen
Copy link

danizen commented Mar 1, 2022

Please see issue palewire/django-bakery#156. In one commit, I changed a couple places where that code uses os.path.join() for a path that will be validated by PyFilesystem2. However, there is still a validation issue with paths that start with C:/ and it is not clear to me how the abstraction is supposed to work.

The issue is that when the BUILD_DIR is set to a path such as "C:\Users\davisda4\workdir\build", then the path is not valid according to fs.path.validatepath. The error occurs when checking whether the path exists at https://github.com/palewire/django-bakery/blob/master/bakery/views/base.py#L62.

In this code, self.fs is an OSFS filesystem initialized from "osfs:///", which may not be valid on Windows.

@althonos
Copy link
Member

althonos commented Mar 26, 2022

PyFilesystem is always expecting paths with forward slashes, and you should not use fs.path with filesystem paths. In theory, the idea would be that you do:

import os
import fs

with fs.open_fs(os.getenv("BUILD_DIR")) as build_dir:
    build_dir.makedirs("/the/dir/to/make")

So in your example:

    def prep_directory(self, target_dir):
        """
        Prepares a new directory to store the file at the provided path, if needed.
        """
        dirname = path.dirname(target_dir)
        if dirname:
            with fs.open_fs(settings.BUILD_DIR) as build_dir:
                if not build_dir.exists(dirname):
                    logger.debug("Creating directory at {}".format(build_dir.getsyspath(dirname)))
                    build_dir.makedirs(dirname)

@lurch
Copy link
Contributor

lurch commented Apr 1, 2022

@althonos Presumably that last line in your example should have been build_dir.makedirs(dirname) ?

@althonos
Copy link
Member

althonos commented Apr 1, 2022

Yes indeed, edited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants