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

[feature] add self.*_path properties that return pathlib.Path #11304

Closed
1 task done
mattyclarkson opened this issue May 19, 2022 · 6 comments · Fixed by #11585
Closed
1 task done

[feature] add self.*_path properties that return pathlib.Path #11304

mattyclarkson opened this issue May 19, 2022 · 6 comments · Fixed by #11585

Comments

@mattyclarkson
Copy link

We have recipe_folder, source_folder, etc that return Optional[str].

It would be nice to work with filesystem paths using pathlib.Path.

As the *_folder can be None in certain stages, it is usually a programming error accessing them when they are None.

It would be nice to have the following accessors on the ConanFile:

    @property
    def package_path(self) -> Path:
        """
        :returns: The location of the final packaging location              
        """
        assert self.package_folder is not None, "`package_folder` is `None`"
        return Path(self.package_folder)

Then we can use it like so:

    def build(self) -> None:
        exe = self.package_path / "bin" / "hello-world"
        exe.parent.mkdir()
        with exe.open(encoding="utf8") as stream:
            stream.write("#! /bin/sh\necho 'Hello, world!'')
        exe.chmod(0o755)

Which is much nicer that the equivalent code with os.path.

@memsharded
Copy link
Member

The problem with this is that Conan 1.X still supports Python 2.7 (sigh, I know...), so this couldn't be added.
Then we need to keep the current xxx_folder as strings for the Conan 1.X -> Conan 2.0 migration.

We will probably start to use Pathlib gradually at Conan 2.X (once the migration cliff has been surpassed), and from there we might be able to consider this request.

@mattyclarkson
Copy link
Author

Makes sense.

A possible way to support this AOT would be to add pathlib2 in conans/requirements.txt:

pathlib2>=2;python_version<='2.7'
try:
    from pathlib import Path
except ImportError:
    from pathlib2 import Path

Then we could have the newer properties available before v2.0.0 to aid transition. It might be possible then to just nuke *_folder attributes as a backwards incompatible change. Would be nice to be in pathlib world in v2.

@SSE4
Copy link
Contributor

SSE4 commented Jun 4, 2022

The problem with this is that Conan 1.X still supports Python 2.7 (sigh, I know...), so this couldn't be added. Then we need to keep the current xxx_folder as strings for the Conan 1.X -> Conan 2.0 migration.

does it? it seems to be removed already #11357 #11360

@ytimenkov
Copy link
Contributor

ytimenkov commented Jul 7, 2022

@memsharded Are there any chances that Conan 2.0 will have it? I find it extremely convenient to be able to write self.source_folder / "build".

I think it is important to make this switch in Conan 2.0 so new recipes can rely on this functionality.

@memsharded
Copy link
Member

Maybe now that Conan 1.X dropped Py2 support, it is an option. Someone wants to contribute a PR for Conan 1? (it needs to be there, in order to have compatible recipes in both 1.X and 2.0)

@SSE4
Copy link
Contributor

SSE4 commented Jul 7, 2022

here we go: #11585

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

Successfully merging a pull request may close this issue.

4 participants