Use environment variable in Python Sphinx image directive - environment-variables

I have a Python project with Sphinx documentation, and to keep the code repository small, some of the images and other files I want to include in the docs are in a separate repo.
For the Sphinx docs build, I do have a shell environment variable set to point to the other repo. I tried using it from an image directive like this:
.. image:: $OTHER_REPO/docs_images/image.png
but that doesn't work.
For now, I've put some code in docs/conf.py that copy over the files I need into the docs source tree, and then it works. But this feels like a hack, there must be a proper / better way to do this?

I found the following solution that solves my problem.
Visit Change variable in sphinx conf.py as part of build command
Basically, we can define the env variable in Jupyter that can initialize in the make.bat file by set the following comments:
Jupyter notebook:
try:
status_flag = %env status_flag
print('Status_flag defined as ' + status_flag + '!')
except:
status_flag = 'MP4'
print('Status_flag defined as ' + status_flag + '!')
make.bat:
SET status_flag=HTML
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

Related

How can I use docker env files in shell scripts?

Docker env files look similar to shell scripts defining variables.
Unfortunately in env files, values cannot be quoted and so simply sourcing them only works if there are no "special" characters in the values.
VAR1=This_works
VAR2=This will not work
VAR3=This won't either
Is there any way you can use these files in a shell script?
My current approach is this:
eval $( perl -ne '
s/\x27/\x27\\\x27\x27/g;
s/^(\w+)=(.+)$/export $1=\x27$2\x27/ and print
' "path/to/env_file" )
So I'm searching for any quote in each line of the env file and replace it by '\''.
Then I'm modifying each line which starts with an identifier (\w+) followed by a = and any text (.+). So the VAR3 will become: export VAR3='This won'\''t either.
The modified line is then printed.
Everything which was printed is eval-ed and so the variables will be available in my shell's environment.
Are there other proposals how to achieve this?

Appending to PATH in a Windows Docker container

I need to append to the PATH within a Windows Docker container, and I've tried many permutations.
ENV PATH=%PATH%;C:\\Foo\\bin
ENV PATH=$PATH;C:\\Foo\\bin
ENV PATH="%PATH%;C:\Foo\bin"
ENV PATH="$PATH;C:\Foo\bin"
RUN "set PATH=%PATH%;C:\Foo\bin"
None of these work: they don't evaluate the preexisting PATH variable.
What is the right syntax to append to the PATH? Can I even append to the PATH inside Docker? (I can on similar Linux containers)
Unfortunately ENV won't work, because windows environment variable work a little differently than linux. more info
As of now the only way to do this is through RUN
But you don't need to create a separate file to do this. This can be done by the following much simpler one line command:
RUN setx path "%path%;C:\Foo\bin"
You can set environment variables permanently in the container using a powershell script.
Create a powershell script in yout docker context (e.g. setpath.ps1 ) containing this:
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Foo\bin", [EnvironmentVariableTarget]::Machine)
Add this script to your container dockerfile and RUN the script. Add something like this to your dockerfile:
ADD ./setpath.ps1 c:/MyScripts/setpath.ps1
RUN powershell -Command c:\MyScripts\setpath.ps1
[Environment]::SetEnvironmentVariable is a good way, but will not work in nanoserver.
The best choice is:
RUN setx path '%path%;C:\Foo\bin'
This worked for me:
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:/your/path"
USER ContainerUser
As seen in the .net sdk Dockerfile: https://github.com/dotnet/dotnet-docker/blob/20ea9f045a8eacef3fc33d41d58151d793f0cf36/2.1/sdk/nanoserver-1909/amd64/Dockerfile#L28-L29
Despite all previous answers, I've faced an issue in some environments. Basically on a custom local test environment the setx using the %PATH%;C:\foo\bar way works even when the folder has spaces like C:\Program Files. That though didn't work when trying it on our production environment.
Checking what Microsoft do when they install the base packages on their own images it turns out a better and more reliable way is to use the command this way:
RUN setx /M PATH $(${Env:PATH} + \";${Env:ProgramFiles(x86)}\foo\bar\")
This way docker will be able to get the proper paths and properly update the PATH data.
Edit:
I've fixed the missing trailing \ in the command, thanks to Robin Ding :)
The following works for me in nanoserver-1809 (from this GitHub issue):
ENV PATH="$WindowsPATH;C:\Foo\bin"
My answer is similar to mirsik's but has no need for a separate script. Just put this in your Dockerfile
RUN $env:PATH = 'C:\Foo\bin;{0}' -f $env:PATH ; \
[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)

How to pass environment variable values into files in the docker container I'm trying to run

Let's say I have a config file in my docker container located here:
/opt/jboss/bin/config.xml
The Config file looks like this:
<Database-Password>$PASSWORD</Database-Password>
I want to pass the actual password in when I go to run the docker container using the "--env-file" argument.
This is the contents on the env-file I'm passing in:
PASSWORD=MyPassword
I understand the VARIABLE=VALUE syntax. "MyPassword" is the value of the Password variable.
How will docker know to find the specific file (/opt/jboss/bin/config.xml) with this variable and swap it out? Am I declaring the variable correctly in the config file? For some reason I'm having trouble finding this information.
The short answer is - it won't necessarily. Unless you have some mechanism to rewrite your config file from the host environment.
This is pretty easy in a shell script - you can just refer to it as you have done.
But an XML file isn't "run" in that sense, so it probably won't just work.
As an example - if you want to 'edit' your xml, you could do it with perl - but you'll have to install a bit more stuff to get it to work:
perl -MXML::Twig -e'XML::Twig -> new ( twig_handlers => { Database-Password => sub { $_ -> set_text ( $ENV{PASSWORD} ) } } ) -> parsefile_inplace("/opt/jboss/bin/config.xml")'
(This will need both perl and XML::Twig installing, so there may be better options)
the syntax in your env file should be
variable=value
see the doc at https://docs.docker.com/engine/reference/commandline/run/
extract
cat ./env.list
TEST_FOO=BAR

Jenkins sourcing a config file during build shell step

I'm trying to set up a build in jenkins that reads a config file generated during a previous build (if it exists) as part of a shell build step. However, the variables I define in the config file don't seem to get put into the environment of the current shell, and I can't figure out why.
The first build step will pull in the config file from the upstream project.
The config file has just simple variable defs:
VAR_ONE=foo
VAR_TWO=bar
#etc...
The second build step is the shell, which looks like this:
if [ -f $WORKSPACE/build_config ];
then
source $WORKSPACE/build_config
echo $VAR_ONE
echo $VAR_TWO
fi
In the jenkins console output for the job I see:
+ '[' -f /var/lib/hudson-slave/workspace/build_config ']'
+ source /var/lib/hudson-slave/workspace/build_config
++ VAR_ONE=foo
++ VAR_TWO=bar
+ echo
+ echo
I don't know what the double plus means, maybe it's being exported into a different scope? If it is, why?
I haven't really looked into the EnvInject or EnvFile plugins yet. I plan to after this exercise in frustration, but I figured this would be a good question to ask anyway since I thought this would be possible to just bash out.
Anyone know what the heck is going on?
Just a shot in the dark, but have you tried to surround the variables with {} like in ${VAR_ONE} when using them?
About the double plus i just can guess either. I would say it is because of the variables declaration coming from the config file which is loaded into the current shell... But this is really just me guessing.
Hope this helps though.

Lua os.getenv does not work in a batch file

I have a little script to read my PATH and store in a file, which I would like to be scheduled to run daily.
path = os.getenv("PATH")
file_name = "C:\\temp.txt"
file = io.open(file_name, "w")
file:write(path)
file:close()
If I run it from command line it works, but when I create batch file (I work on Windows XP) and double click it - the os.getenv("PATH") returns false. The batch file:
"C:\Program Files\Lua\5.1\lua" store_path.lua
I read in comments to this question that it "is not a process environment variable, it's provided by the shell, so it won't work". And indeed, some other env variables (like username) work fine.
The two questions I have are:
Why the shell does not have access to the PATH? I thought it would
make a copy of the environment (so only setting env variable would be a problem)?
What would be the best way to read the PATH in such a way that I can add
it to a scheduler?
Have the batch file run it from a shell so that you get shell variables:
cmd /c C:\path\to\lua myfile.lua

Resources