How to use jenkins plugin in python script? - python

I've wrote a script to create a job in jenkins, next in that script I want to get environment variables and set them.
So I want to use "envInject" plugin in my script, but I don't know how can I call or bring it to my python script?
Any help is appreciated. Thanks

In Python env variables can be accessed through os.environ
import os
print os.environ['BUILD_NUMBER']
see your jenkins_site/jenkins/env-vars.html for more info on build vars available

Related

Can not get an Environmental Variable that is defined in BASH and exported to show up in Python

I have uses nano .bash_profile, then I added a line - export OBN_KEY='blahblahblah' and saved it and exited and when I use printenv in the terminal it is on the list. How ever when I go over to IDLE and import os and print(os.environ) it does not show up on that list I am using python 3.7, what am I missing? I am also new to coding so I may be missing something simple but pls help.

Access exported variables by a called script from python

While there are many questions regarding accessing/ setting env variables in python, I could not find the answer for my particular scenario.
I have a shell script that when called it does the export of a bunch of env variables that are used later on.
When you need those variables to be available from your current session, then you would do
. ./script_exp_var.sh
that does, say
export MYVAR = MYVAL
then if you run python, you could access it with os.environ.get('MYVAR').
My question is how to invoke the script from python and then to have to access the env variables that called script just exported. Is it possible at all and if yes how?
Note: I know I could set the env var from python using os.environ["MYVAR"] = MYVAL but I would like to use the existing logic in my ./script_exp_var.sh because it exports many variables.
Also making sure to execute the script first and then execute the python is also not an option for my scenario.
You can't. Environment variables are copied from parent to child, never back to the parent.
If you execute a shell script from python then the environment variables will be set in that shell process (the child) and python will be unaware of them.
You could write a parser to read the shell commands from python, but that's a lot of work.
Better to write a shell script with the settings in that and then call the python program as a child of the script.
Alternatively, write a shell script that echoes the values back to python which can be picked-up using a pipe.

Python script running from SCONS cannot access environment variables

Inside of my scons script I execute another python script:
fs = env.Command('fs', None, 'python updatefs.py')
AlwaysBuild(fs)
Depends(fs, main)
In python script I am trying to access an environment variable:
import os
mode = os.environ['PROC_MODE']
The variable was previously set up in the shell:
export PROC_MODE='some_mode'
Python complain:
KeyError: 'PROC_MODE'
What is the proper way to propagate environment to an external script?
This is covered in lightly in the FAQ:
FAQ
Basically SCons constructs a clean reproducible set of environment variables so that differences in any user's environment won't break a build.
So if you want to propagate a particular variable from your shell you can explicitly do it as such:
env['ENV']['MY_VARIABLE']=os.environ['MY_VARIABLE']
If you wanted to progagate all environment variables you'd do this:
env['ENV'] = os.environ
Where env is your Environment()

Using python subprocess with module load

I'm currently using Python 2.7 on a unix environment.
I need to run R scripts in my python scripts but I can't manage to make it work because my R module needs to be loaded before (using "module load")
Here's my python script :
import os
import subprocess as sp
os.system('module load R/3.2.3')
out = sp.check_output(['Rscript','test.R'], universal_newlines=True)
I keep having the same error : "[Errno 2] No such file or directory"
Any idea ?
I looked here and here but couldn't make it work.
Thank you for your help !
So what "module load" actually does is set some environment variables in the calling shell. So when you do this:
os.system('module load R/3.2.3')
Python creates a process, runs /bin/sh in it, and passes that command to the shell. The module environment variables are set in that shell. Then that shell exits--job done!
The environment variables do not--and cannot--propagate back to the Python process. So when you do this:
sp.check_output(['Rscript','test.R'])
It's totally irrelevant that you ran module load before.
So how can you fix this? Well, one possibility would be to explicitly specify the path to Rscript:
sp.check_output(['/your/full/path/to/Rscript','test.R'])
Another would be to combine your commands:
sp.check_output('module load R/3.2.3 && Rscript test.R', shell=True)
Finally, you could simply run module load before running your Python script in the first place. The environment variables it sets can propagate all the way to the R invocation within Python.
By the way, it is possible to invoke R directly from Python: http://rpy.sourceforge.net/rpy2/doc-dev/html/introduction.html

Modifying operating system environment variables from Python

I'm using Tesseract OCR and everytime I run a new session it asks for setting the TESSDATA_PREFIX variable,
I do so by running the command export TESSDATA_PREFIX="PATH_TO_FILES"
How can I do it inside the python script i'm running ?
Thanks !
You can do:
import os
os.putenv("TESSDATA_PREFIX", "PATH_TO_FILES")
More info
http://docs.python.org/2/library/os.html#os.putenv
Please try adding to your python:
import os
os.environ["TESSDATA_PREFIX"] = "PATH_TO_FILES"
You could try using os module to set environment variable:
setting:
os.environ['TESSDATA_PREFIX'] = "PATH_TO_FILES"
getting:
pat_to_files = os.environ['TESSDATA_PREFIX']
Such variable will be accessible from the python code but it may not remain accessible for other programs when your python code quits.
If your goal is to set env variable for other program then you could try this recipe:
http://code.activestate.com/recipes/159462-how-to-set-environment-variables/

Categories