Get the absolute path of the var directory

I have a method getThumbnailPath which returns the path to a preview image, as presented TTW (/++thumbnail++/UID); when the catalog metadata of an object is refreshed, the thumbnail image is checked and, if necessary, (re)created. The /++thumbnail++/ URLs are excluded from the RewriteRule, and DocumentRoot/++thumbnail++ is a symbolic link to var/thumbnails.

Now I noticed that there is some surprising difference between my development and production servers: In the development setup, the working directory (os.getcwd()) is the instance directory (which contains the parts/), while in the productive setup (using systemctl start myproject.service), it is parts/instance. This yielded some surprising effects - my thumbnails generation didn't seem to work, while they indeed have been created - just not at the place where Apache expected them.

Is there some "official way" to get the absolute location of the var/ and log (normally var/log, but who knows ...) directories, or should I write my own? I'd like to avoid code differences between the both if at all possible.

Just as a quick pointer for inspiration for your project, check the code in eea.downloads, which is an add'on made by Eaudeweb for the European Environment Agency. It's a helper add'on to prepare and write unique downloads outside the zodb in the var directory which can then be downloaded (albeit through Plone itself).

There's little context in your question, but you do know that Plone has quite extensive support for generating and storing and refreshing cachable image scales on image fields in contenttypes, right? Just checking.

I'm aware of them <fieldname>_<namedsize> suffixes, but I'd like to delegate that whole image delivery stuff to Apache.

AFAICS, eea.downloads simply uses environment vars EEADOWNLOADS_{NAME,PATH} for initialisation; no special Zope/Plone examiniation here. Or did I overlook something?

I just stumbled over this old thread, because I also need the var-directory.
So, I warm this up with kind of a solution.

I worked around by assuming the PID-file always resides there.
=> (!) heuristics, to be used with care.

from App.config import getConfiguration
import os

varbase = os.path.dirname(getConfiguration().pid_filename)

i set enviroment vars in the buildout

var-dir=${buildout:directory}/var
environment-vars +=
    VAR_DIR ${buildout:var-dir}

and in my python scripts i do:

vardir = os.environ.get('VAR_DIR', None)
2 Likes

Good idea; this will work for most setups, including mine.

Unfortunately, it is possible to configure the pid file location to be elsewhere, and I can imagine there are valid reasons to do so. Thus, since we don't have anything better (apart from environment variables, but those should be strictly optional IMHO), an official way would be something to be established ...

Yes, right.
The problem is the , None part: if the environment variable is missing, we have None.
I'd lke to avoid environment vars as a dependency.