Gitlab Job variable to use in python code - python

I am trying to create a pipeline and have defined some variable in my gitlab job which will be used by python script to populate results.
Is there any way I can call/define those variable in my python script.
For example:
.test-report:
extends: .test_report_setup
tags: *def_runners_tags
variables:
value-path: ${CI_PROJECT_DIR}/value
Now in my python script I want to call the variable 'value-path' to fetch or read files located in that directory. (Please note that variable is a custom variable on gitlab)
file_path = '<value-path>' <---- To get the gitlab job variable here
file_in = open(file_path + "id.txt")
Please help me how I can get it in my python script as I am a bit stuck on it.
Any suggestion/help on this will be appreciated. Thanks in advance.

You can access environment variables with os.environ or os.getenv. os.environ is a dict with the environment variables and will fail if you attempt to retrieve a key that doesn't exist. os.getenv is basically os.environ.get, which allows you to set a default value if there is no environment variable with that name.

Related

How to set environment variables in AWS lambda using Python

I'm trying to set environment variables in AWS lambda using python
initially I have an environment variable stackname with no value in lambda configuration.
def lambda_handler(event, context):
if os.environ["stackname"]:
print("Not empty")
print(os.environ["stackname"])
else:
print("its empty")
os.environ["stackname"]="mystack"
print(os.environ["stackname"])
Now I'm seeing weird intermittent behaviour here for the first I expect to print
its empty
mystack
and from thereon whenever I execute lambda it should print
Not empty
mystack
for initial couple of times it prints
Not empty
mystack
but after couple or more executions lambda prints below which is weird.
its empty
mystack
Please suggest if this is any other better way to set environment variables which gives consistent output.
AWS Lambda functions run inside of an Amazon Linux environment. Sequential invocations of the same Lambda function may result in the function running on the same environment or a different environment. By environment I mean computer, container, etc.
This means that you cannot reliably set environment variables and expect them to be there on the next invocation.
A better approach is to store your run-time variables in persistent storage such as DynamoDB.
Why not just use the environment variable support provided by lambda? You can config the env vars when you create or update your function, and then reference those vars in your function code. Regarding why it prints out 'its empty', #John Hanley's answer is pretty accurate.
you can do it as you do in system using os
import os
os.environ['varible']='value'

Using %systemdrive% in python

I would like to open and work with a file with Python however I would like to use Windows %systemdrive% when referring to a file instead of full path.
This piece of code works:
if not os.path.isfile('C:\\Work\\test\sample.txt'):
This does not:
if not os.path.isfile('%systemdrive%\\Work\\test\\sample.txt'):
Any idea? Thank you in advance!
There is a dedicated function for solving this problem named os.path.expandvars.
Return the argument with environment variables expanded. Substrings of
the form $name or ${name} are replaced by the value of environment
variable name. Malformed variable names and references to non-existing
variables are left unchanged.
On Windows, %name% expansions are supported in addition to $name and
${name}.
if not os.path.isfile(os.path.expandvars('%systemdrive%\\Work\\test\\sample.txt')):
pass # do something
You can use os.environ
import os
import os.path
fn = r'{0}\Work\test\sample.txt'.format( os.environ['systemdrive'] )
if not os.path.isfile(fn):
...

How to access enironment variables of a popen process

Consider I have two python scripts caller.py and being_called.py
Caller.py:
p = Popen(path_to_being_called, shell=True, stdout=fh, stderr=fh1)
path_to_being_called contains executable path of script 'being_called.py'
Now being_called.py does following:
os.environ['hello'] = 'Yolo'
I want to access this value of environment variable hello inside the script caller.py
Is there a way to do this with popen calls?
Note that I know I can update environment variable for 'being_called' from caller itself as a paramter env and mapping of os.environ and kwargs, but thats not what I want to do. Env variable will be updated from being_called.py only and I need to access its value in caller.py
I tried to see what dir(p) offers but not getting any help from any of that.

What are the differences between os.putenv(key, value) and os.environ[key]=value in python?

I need build a system environ variable, and I use os.putenv(key, value) to build one, then print os.getenv(key), the console outputs None.
But the console outputs value (here is print os.getenv(key) or print os.environ[key]) when I use os.environ[key] = value to build it.
However, the key and the value are not in the dictionary if print os.environ.
Why can I not build the system environment variable successfully? I use Windows 7 and Python 2.7.5.
If you read the documentation you will get the answer to why os.putenv does not work:
This mapping is captured the first time the os module is imported, typically during
Python startup as part of processing site.py. Changes to the environment made after
this time are not reflected in os.environ, except for changes made by modifying
os.environ directly.
If the platform supports the putenv() function, this mapping may be used to modify the
environment as well as query the environment. putenv() will be called automatically
when the mapping is modified.
Note Calling putenv() directly does not change os.environ, so it’s better to modify
os.environ.

how to set environment variable in python script

i am using SCONS Construction tool.
i am unable to use the environment variable which is initialized in python script.
In My project USER can change some variables to work with the compiler.
For that we have 2 files.
Config.py
Sconstruct
Config.py is having all the variables which are like Include directories, CFLAGS , CPPDEFINES etc. So, Here we can set some variables. Those variables i need to use in Sconstruct file. In config.py i set a variable like below
SCONS_INC = "Include files"
os.environ["SCONS_INC"] = SCONS_INC
I need to use those variables in Sconstruct File. The code is
env["CPPPATH"] = os.environ["SCONS_INC"]
But I am getting an error like Undefined variable SCONS_INC.
How to do this?
SCons by default does not use the invoked environment, this is to make sure that you can reproduce the build no matter which configurations your environment have.
The environment variables are stored within the scons environment under the key ENV so you access the general environment variables like this:
env = Environment()
variable = env['ENV']['SomeVariable']
env['ENV']['SomeVariable'] = SomeValue
I understand your question like you need to use variables set in the python script within SCons. To do this you need to transfer them using the two method you describe in combination.
env = Enviroment()
python_variable = os.environ['SomeVariable']
env['ENV']['SomeVariable'] = python_variable
I would however perhaps recommend other ways of controlling the build, so you do not have to go with the hassle of transferring environment variable. IMHO using arguments are simpler. The arguments are simply a dict that are generated by the invocation of scons, so when you say:
scons -D some_argument=blob
You can get that argument by simply:
some_variable = ARGUMENTS["some_argument"]
Of course I do not know why you need the environment variables, so this might be completely irrelevant for you.
I once had a similar need, where the compiler was looking for a certain Env variable that hadnt been set. I was able to solve this problem as follows:
env = Environment()
env['ENV']['THE_VARIABLE'] = 'SomeValue'

Categories