Invoke bamboo plan variables in a python script under script task - python

I have a simple script task added to my Bamboo plan which is inline Python script.
/usr/bin/python <<EOF
print "Hello"
EOF
It works just fine.
Now I want to just pass Hello as a plan specific variable so I configured a variable called pythontest within bamboo plan variables and provided it the value Hello.
When I switch the script to:-
/usr/bin/python <<EOF
print "$bamboo_pythontest"
EOF
It does nothing.
After going through below post:-
https://community.atlassian.com/t5/Bamboo-questions/How-I-can-access-global-and-or-plan-specific-variables-from/qaq-p/162809
/usr/bin/python <<EOF
import os
print os.environ['bamboo_pythontest]
EOF
I get error
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/lib64/python2.6/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: ''
Is there a way to successfully invoke Bamboo plan variables within a Python inline script just like it works for a shell or batch script.
Any help here would be greatly appreciated.
Cheers,
Ashley

I am looking into a solution where we can run the python code from inside bamboo. The error that you are pointing, looks to be syntax error. Try this.
/usr/bin/python <<EOF
print("Hello World")
EOF

Related

Running a Python script from the Python Shell

I wrote a script and saved it as test.py (code shown below). And when I run it from the script (Run Module 5), I get the result in the Python Shell.
But I have tried multiple suggestions available online to have it run from the Python Shell instead to which I fail (one error pasted below).
How can I run a python script from the python shell?
The version of Python I am running is 3.7.3 and in Windows.
#!/usr/bin/python3
print(" Hello, world!")
exec(open(test.py).read())
Output:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
exec(open(test.py).read())
NameError: name 'test' is not defined
You don't need the last line for it to run. All you need is:
!/usr/bin/python3
print(" Hello, world!")
If you want to run it from another file, don't use exec. Instead, import it.
import test
You need to pass the "test.py" as a string (use quotes).
test is not a known object.

How to run a python script taking arguments with blender?

I have a script I want to run within blender to generate AO maps (script was given to me and the source guarantees it works).
I try to run the script as follows:
blender --background --python /opt/ff/product_builder/furniture_builder/generate_ao_maps.py --input_dir /tmp/test.obj --output_dir /tmp/test.png --mode ao
Which produces:
AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
found bundled python: /usr/share/blender/2.79/python
Traceback (most recent call last):
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 195, in <module>
main()
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 178, in main
args = parse_args()
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 21, in parse_args
return parser.parse_args(os.getenv(BLENDER_ENV).split(' '))
AttributeError: 'NoneType' object has no attribute 'split'
Error: File format is not supported in file '/tmp/test.obj'
Blender quit
If I run this same script without blender (but with the argument) it tells me:
Traceback (most recent call last):
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 5, in <module>
import bpy
ImportError: No module named bpy
What do I need to do to pass the parameters to the script and get it working?
You are seeing that error because your script is looking for the environment variable BLENDER_ENV, which is not on your system. I don't recognize BLENDER_ENV as a standard Blender related environment variable so it's probable that your friend added BLENDER_ENV to his or her environment.
Firstly, blender processes its cli args in the order they are given, so your example will start in the background, run a script, then set the input_dir... This will most likely not have the result you are after.
The problem with your script failing is that the arg passed to os.getenv() needs to be a string that is the name of a shell environment variable, if you are using bash you need to export the variable to put it into the environment before you start blender.
export BLENDER_ARGS="arg1 arg2"
blender -b myfile.blend --python myscript.py
If you are using a csh then use setenv BLENDER_ARGS "arg1 arg2"
Then in your py script, you use os.getenv('BLENDER_ARGS').split(' ')
Note that each shell instance is a separate environment, you need to set the variables in the same instance that starts blender.
You may also be interested in passing cli arguments to the script as explained in response to this question.

Calling python script from a Bash script

I'm trying to call a python script from a bash script. I get import errors only if I try to run the .py from the bash script. If I run with python myscript.py everything is fine. This is my bash script:
while true; do
python script.py
echo "Restarting...";
sleep 3;
done
The error I get:
Traceback (most recent call last):
File "script.py", line 39, in <module>
from pokemongo_bot import logger
File "/Users/Paolo/Downloads/folder/t/__init__.py", line 4, in <module>
import googlemaps
ImportError: No module named googlemaps
There is more to this story that isn't in your question.
Your PYTHONPATH variable is getting confused somewhere along the way.
Insert a couple quick test lines:
in bash:
echo $PYTHONPATH
in your python:
import os
print os.environ["PYTHONPATH"]
At some point, the path to googlemaps got lost.
Your problem is in the script itself, your bash code is OK!. If you don't have problem running python scrip.py from bash directly, you should test if you use the same interpreter for both calls. You can check the shebang line in the python script, it is the first line in the file for example #!/usr/bin/env python or #!/usr/bin/python and compare it to the output of which python command if the output is different try to change or add the shebang line in to the file. If you call directly file in bash ./some_script.py bash reads the first line and if it is shebang line he wil execute the specific command for the file. My point is that if you use two diferent interpreters for calling file directly with python script.py and indirectly ./script.py one of them may not have the proper python modules.
Howto code:
$ which python
/usr/local/bin/python
So the second line is the path for your interpreter to build a shebang from it write in the first line of your script file this.
#!/usr/local/bin/python

Python 2 raw_input() : EOFError when reading a line in WINDOWS 7 command prompt

NOTE: Many of the same questions have been asked about python raw_input() in sublime text. This question is NOT about sublime. The python code is called in Windows command prompt which unlike the sublime terminal does support interactive inputs.
I have a python program that takes user input with the built-in function raw_input(). See below.
def password_score():
pwd = raw_input('Enter a password: ')
gname = raw_input('Enter your first name: ')
...
I call the program in cmd by
echo password_score()|python -i a06q1.py
where a06q1.py is the file name. The path of the python directory has been added to system variable %PATH% temporarily. I am in the directory of the file. My operating system is Windows 7. I am using python 2.6. The same command has worked until now.
Then cmd returns
File "<stdin>", line 1, in <module>
File "a06q1.py", line 27, in password_score
pwd = raw_input(p_prompt)
EOFError: EOF when reading a line
Is there a way to get around it within cmd?
EDIT: I just tried in on an iOS terminal too. With the same command as in cmd (with quotes), it returns the same error. Is there anything wrong about the command line I used? Thank you!
EDIT: Sebastian's answer solves the problem. The command should adapt to windows as follows.
printf "a06q1.password_score()\n'arg1\n'arg2"|python -i -c "import a06q1"
The single quotes succeeding \n can be replaced by spaces. They separate multiple inputs.
EOF means that there is no more input. And it is true, the only line is consumed by -i option:
$ echo "f()" | python -i -c "def f(): print('x'); input('y\n')"
>>> x
y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in f
EOFError: EOF when reading a line
>>>
Provide more input:
$ printf "f()\n'z'" | python -i -c "def f(): print('x'); print(input('y\n')*3)"
>>> x
y
zzz
>>>
As you said: it is "canonically bad" to specify a function to run in such manner. If you don't know in advance, what function you want to run then as an alternative, you could run it as:
$ python -c "from a06q1 import password_score as f; f()" < input_file.txt
What you're trying to do is not the way to call a specific function from the command line.
You need an if __name__ == "__main__"-block in your code.
At the end of your file:
`if __name__ == "__main__"`:
password_score()
And now run the program by:
python a06q1.py
If you run a python file from the command file, the __name__-variable will be "__main__". Notice that if you import a06q1 to some other python file, the name will equal the module name and thus the if __name__ block evaluates to False.
From python docs:
This module represents the (otherwise anonymous) scope in which the
interpreter’s main program executes — commands read either from
standard input, from a script file, or from an interactive prompt. It
is this environment in which the idiomatic “conditional script” stanza
is run
As J.F Sebastian writes in the comments, you can also execute a specific python command by providing the -c switch. The following will import the a06q1 and run function_name:
python -c "from a06q1 import function_name; function_name()"

How to set a python path when using runit

I am am using runit to manage a process on Ubuntu 12.04. I get the below error in the logs when I run:
sv up test/
I assume it is a python path issue.
ImportError: No module named htcommon.ht_redis
Traceback (most recent call last):
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py", line 17, in <module>
from htpData import HTPItemBase, HTPUserBase
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/htpData.py", line 9, in <module>
from htcommon.ht_redis import HTRedisConnection
ImportError: No module named htcommon.ht_redis]
I have also set the path in /etc/environment and also set in .bashrc.
Below is my runit script.
#!/bin/sh
exec 2>&1
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1
root#aws-rpc-server-east-staging-20130203070552:/etc/sv#
When I run the process from the command line I get no issues and it works.
/usr/bin/python /home/ubuntu/workspace/htFrontEnd/heythat/htanalytics/ht_rpc_server.py
Why will runit not work? Why can it not find the path?
Look like a few potential problems with this script. I don't think an 'export' can be 'exec'd. At least, it fails in my version of bourne. The exec command replaces the current process (your script) with the called command and doesn't return unless the called command fails. So, it doesn't make sense to 'exec' the exports anyway. Also they can be collapsed into one line. So, you're script should look more like this:
#!/bin/sh
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1

Categories

Resources