rez icon indicating copy to clipboard operation
rez copied to clipboard

Access __file__ global in Early bind function (package.py)

Open vanridal opened this issue 1 year ago • 2 comments

Sometimes it can be quite useful to access the location of a package file in a Early bind function Maybe you want to find a relative file to read in, like a python requirments.txt or a VERSION token file

In the global preprocess, the DeveloperPackage object as a filepath attribute, Unfortunately in Early bind function the this object does not, I thought to add the attribute to the this object but then this will add this path attribute to the built package file, which i think is undesirable.

I think the best option is to support the standard global variable __file__

As this will not be added to the final build package file

vanridal avatar Oct 07 '24 22:10 vanridal

I might be misunderstanding but for anything during build, can you not just use any of the REZ_BUILD_* environment variables? https://rez.readthedocs.io/en/3.0.0/environment.html#build-environment-variables

For your use case specifically, you'd use REZ_BUILD_SOURCE_PATH.

beatreichenbach avatar Nov 09 '24 02:11 beatreichenbach

This is for an early bind function in a package.py, those variables wont exist yet.

My use case was around the version property and I actually solved my use case for this issue, I was a bit overzealous with submitting it. So used to using __file__ as a reliable way to get a python file

As im using it for building packages, i can use os.getcwd() but I decided to keep the issue open, as I could think of some use cases where os.getcwd() wouldn't work, for a loose example you had some manually created dynamic package in your repo, or even if at some point rez supported calling rez-build with a arg to the package.py instead of cwd.

Also I think ensuring the existence of the __file__ is more pythonic as it is a standard global variable. Its also easily implemented with a single line of code

I created a pull request for it, and ive been using my commit with the rez 3.2.1 with no discovered ill effects. The maintainers may do with it with what they wish.

vanridal avatar Nov 09 '24 06:11 vanridal