Accessing the build directory in conf.py?

663 views
Skip to first unread message

Markus Schöpflin

unread,
Sep 9, 2016, 4:55:22 AM9/9/16
to sphinx...@googlegroups.com
I am using the Jinja extension from sphinxcontrib. For this I have added the
following to conf.py:

extensions = ['sphinxcontrib.jinja']

import json
jinja_contexts = {
'messages': json.load(open("messages.json"))
}

The file "messages.json" is created automatically during the build of the
project, before sphinx-build is called. The file is located in the build
directory of the project.

Now I'm struggling to refer to that file from conf.py, as the current
directory is set to the directory where conf.py is located (which is the
source directory of the project) when conf.py is loaded.

My workaround currently is to copy conf.py to the build directory and execute
sphinx-build like so, with the current directory set to the build directory:

sphinx-build -b html -c . -d doctree '/.../source' html

Is there a way to avoid copying conf.py to the build directory first? E.g. any
variable which holds the value of the directory where sphinx-build is executed
in, which can be accessed from conf.py?

I also tried to add -D build_dir='/...' to sphinx-build in the hope that maybe
this would make build_dir defined when conf.py is evaluated, but this is not
the case.

TIA, Markus

Message has been deleted

Rex Guinn

unread,
Sep 9, 2016, 12:31:59 PM9/9/16
to sphinx...@googlegroups.com
Did you try link instead of copy
ln -s filename to location filename 
The s is for symbolic
--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users+unsubscribe@googlegroups.com.
To post to this group, send email to sphinx...@googlegroups.com.
Visit this group at https://groups.google.com/group/sphinx-users.
For more options, visit https://groups.google.com/d/optout.


--
Rex Guinn 
My Blog https://rexguinn.github.io

Markus Schöpflin

unread,
Sep 12, 2016, 3:25:06 AM9/12/16
to sphinx...@googlegroups.com
Am 09.09.2016 um 16:50 schrieb Rex Guinn:

> Did you try link instead of copy
> ln -s filename to location filename
> The s is for symbolic

conf.py is so small that there isn't any difference between linking the file
or copying the file.

It's not the actual copy I'm worried about, it's the fact that I think that
conf.py is usually located in the source directory and that maybe I will run
into problems later on, now that conf.py is not at it's standard location.

But maybe I'm being too pessimistic here. :-)

Markus

Komiya Takeshi

unread,
Sep 12, 2016, 12:58:56 PM9/12/16
to sphinx-users, markus.s...@comsoft.aero
Hi,

As far as I know, there are no proper way to obtain build directory in conf.py.
But, as a adhoc way, you con obtain it through environment variables.

in Makefile:
    BUILDDIR      = _build
    export BUILDDIR

in conf.py:
    builddir = os.environ['BUILDDIR']
    jinja_contexts = { 
        'messages': json.load(open(os.path.join(builddir, "messages.json"))) 
    }

Thanks,
Takeshi KOMIYA

2016年9月9日金曜日 17時55分22秒 UTC+9 Markus Schöpflin:

gilberto dos santos alves

unread,
Sep 12, 2016, 9:43:19 PM9/12/16
to sphinx...@googlegroups.com
[1] http://www.sphinx-doc.org/en/stable/tutorial.html#setting-up-the-documentation-sources
hi. look carefully to [1] and sphinx-quickstart and see make.bat file or linux / mac make details for better understand what is sphinx defaults assigned when you answer questions made by sphinx-quickstart.



Markus

--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users+unsubscribe@googlegroups.com.
To post to this group, send email to sphinx...@googlegroups.com.
Visit this group at https://groups.google.com/group/sphinx-users.
For more options, visit https://groups.google.com/d/optout.



--
gilberto dos santos alves
+55(11)9-8646-5049
sao paulo - sp - brasil




Markus Schöpflin

unread,
Sep 13, 2016, 2:41:34 AM9/13/16
to sphinx...@googlegroups.com, public-markus.schoepfli...@plane.gmane.org
Am 12.09.2016 um 18:58 schrieb Komiya Takeshi:

> As far as I know, there are no proper way to obtain build directory in conf.py.
> But, as a adhoc way, you con obtain it through environment variables.
> [...]

Yes of course! *smack on forehead* I've completely overlooked the most obvious
solution.

For the records, this is now how I call sphinx_build inside an Ant target for
an out-of-source-tree build, maybe it is of use to someone:

<exec
executable="sphinx-build" failonerror="true"
dir="${subproject.build.basedir}">
<env key="BUILDDIR" value="${subproject.build.basedir}"/>
<arg value="-b"/> <arg value="html"/>
<arg value="-d"/> <arg value="doctree"/>
<arg value="-D"/> <arg value="release=${subproject.release}"/>
<arg value="${subproject.basedir}/source"/>
<arg value="html"/>
</exec>

Thanks a lot,
Markus

Reply all
Reply to author
Forward
0 new messages