Getting tool dirpath in a Python code file
Hi, I have a tool with a <code file="my_script.py"/> tag and in that code file I'm trying to get the tool dirpath where that script and the tool XML exist. I've tried: os.path.abspath(os.path.dirname(sys.argv[0])) os.path.abspath(os.path.dirname(__file__)) And both don't work as expected. Is there a galaxy class I could import which will have the tool directory path? regards, Leandro
On Thu, Apr 14, 2011 at 2:08 PM, Leandro Hermida <softdev@leandrohermida.com> wrote:
Hi,
I have a tool with a <code file="my_script.py"/> tag and in that code file I'm trying to get the tool dirpath where that script and the tool XML exist. I've tried:
os.path.abspath(os.path.dirname(sys.argv[0])) os.path.abspath(os.path.dirname(__file__))
And both don't work as expected. Is there a galaxy class I could import which will have the tool directory path?
regards, Leandro
For standard Python tools in Galaxy, I'm using os.path.split(sys.argv[0])[0] to get the path, which on reflection probably should be written as os.path.dirname(sys.argv[0]) as you suggest. What do __file__ and sys.argv[0] give you? The simplest way to debug this is to add a print statement, since Galaxy will show the stdout. Peter
On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <p.j.a.cock@googlemail.com>wrote:
On Thu, Apr 14, 2011 at 2:08 PM, Leandro Hermida <softdev@leandrohermida.com> wrote:
Hi,
I have a tool with a <code file="my_script.py"/> tag and in that code file I'm trying to get the tool dirpath where that script and the tool XML exist. I've tried:
os.path.abspath(os.path.dirname(sys.argv[0])) os.path.abspath(os.path.dirname(__file__))
And both don't work as expected. Is there a galaxy class I could import which will have the tool directory path?
regards, Leandro
For standard Python tools in Galaxy, I'm using os.path.split(sys.argv[0])[0] to get the path, which on reflection probably should be written as os.path.dirname(sys.argv[0]) as you suggest.
What do __file__ and sys.argv[0] give you? The simplest way to debug this is to add a print statement, since Galaxy will show the stdout.
Hi Peter, __file__ throws an error: global name '__file__' is not defined os.path.abspath(os.path.dirname(sys.argv[0])) gives me /path/to/galaxy/scripts directory which is two levels up from what the tool directory I want for example /path/to/galaxy/tools/mytool Isn't there some Galaxy class I can import? I remember seeing in other code files "import galaxy" and would this have somewhere in it the current tool directory? -Leandro
Hi sorry to ping again, but do any of the Galaxy developers have advice/recommended approach for the problem in this thread? On Thu, Apr 14, 2011 at 3:56 PM, Leandro Hermida <softdev@leandrohermida.com
wrote:
On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <p.j.a.cock@googlemail.com>wrote:
On Thu, Apr 14, 2011 at 2:08 PM, Leandro Hermida <softdev@leandrohermida.com> wrote:
Hi,
I have a tool with a <code file="my_script.py"/> tag and in that code file I'm trying to get the tool dirpath where that script and the tool XML exist. I've tried:
os.path.abspath(os.path.dirname(sys.argv[0])) os.path.abspath(os.path.dirname(__file__))
And both don't work as expected. Is there a galaxy class I could import which will have the tool directory path?
regards, Leandro
For standard Python tools in Galaxy, I'm using os.path.split(sys.argv[0])[0] to get the path, which on reflection probably should be written as os.path.dirname(sys.argv[0]) as you suggest.
What do __file__ and sys.argv[0] give you? The simplest way to debug this is to add a print statement, since Galaxy will show the stdout.
Hi Peter,
__file__ throws an error: global name '__file__' is not defined os.path.abspath(os.path.dirname(sys.argv[0])) gives me /path/to/galaxy/scripts directory which is two levels up from what the tool directory I want for example /path/to/galaxy/tools/mytool
Isn't there some Galaxy class I can import? I remember seeing in other code files "import galaxy" and would this have somewhere in it the current tool directory?
-Leandro
On Thu, Apr 14, 2011 at 2:56 PM, Leandro Hermida <softdev@leandrohermida.com> wrote:
On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <p.j.a.cock@googlemail.com> wrote:
For standard Python tools in Galaxy, I'm using os.path.split(sys.argv[0])[0] to get the path, which on reflection probably should be written as os.path.dirname(sys.argv[0]) as you suggest.
What do __file__ and sys.argv[0] give you? The simplest way to debug this is to add a print statement, since Galaxy will show the stdout.
Hi Peter,
__file__ throws an error: global name '__file__' is not defined
I guess the script is being loaded as a string, and run with eval(...) or something like that. It would also explain why sys.argv[0] would be one of the Galaxy script files.
os.path.abspath(os.path.dirname(sys.argv[0])) gives me /path/to/galaxy/scripts directory which is two levels up from what the tool directory I want for example /path/to/galaxy/tools/mytool
So combine that with ../tools/mytool/ and you're done? OK, you have to know the name of the folder your tool *should* be in... so not a perfect solution. Peter
On Fri, Apr 15, 2011 at 7:27 PM, Peter Cock <p.j.a.cock@googlemail.com>wrote:
On Thu, Apr 14, 2011 at 2:56 PM, Leandro Hermida <softdev@leandrohermida.com> wrote:
On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <p.j.a.cock@googlemail.com> wrote:
For standard Python tools in Galaxy, I'm using os.path.split(sys.argv[0])[0] to get the path, which on reflection probably should be written as os.path.dirname(sys.argv[0]) as you suggest.
What do __file__ and sys.argv[0] give you? The simplest way to debug this is to add a print statement, since Galaxy will show the stdout.
Hi Peter,
__file__ throws an error: global name '__file__' is not defined
I guess the script is being loaded as a string, and run with eval(...) or something like that. It would also explain why sys.argv[0] would be one of the Galaxy script files.
os.path.abspath(os.path.dirname(sys.argv[0])) gives me /path/to/galaxy/scripts directory which is two levels up from what the tool directory I want for example /path/to/galaxy/tools/mytool
So combine that with ../tools/mytool/ and you're done? OK, you have to know the name of the folder your tool *should* be in... so not a perfect solution.
Thanks Peter, yes that's the same idea I did as a quick fix.... also don't like the fact that my tool directory is hard-coded but oh well. There must be a way within Python in Galaxy importing something from Galaxy that has the current tool directory path, it would seem that Galaxy needs to know this and would store it anyway? best, Leandro
Hi galaxy developers, Just want to double-check, there is no way to import some kind of galaxy tool context info into python code you are running for a tool? best, leandro On Fri, Apr 15, 2011 at 8:03 PM, Leandro Hermida <softdev@leandrohermida.com
wrote:
On Fri, Apr 15, 2011 at 7:27 PM, Peter Cock <p.j.a.cock@googlemail.com>wrote:
On Thu, Apr 14, 2011 at 2:56 PM, Leandro Hermida <softdev@leandrohermida.com> wrote:
On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <p.j.a.cock@googlemail.com> wrote:
For standard Python tools in Galaxy, I'm using os.path.split(sys.argv[0])[0] to get the path, which on reflection probably should be written as os.path.dirname(sys.argv[0]) as you suggest.
What do __file__ and sys.argv[0] give you? The simplest way to debug this is to add a print statement, since Galaxy will show the stdout.
Hi Peter,
__file__ throws an error: global name '__file__' is not defined
I guess the script is being loaded as a string, and run with eval(...) or something like that. It would also explain why sys.argv[0] would be one of the Galaxy script files.
os.path.abspath(os.path.dirname(sys.argv[0])) gives me /path/to/galaxy/scripts directory which is two levels up from what the tool directory I want for example /path/to/galaxy/tools/mytool
So combine that with ../tools/mytool/ and you're done? OK, you have to know the name of the folder your tool *should* be in... so not a perfect solution.
Thanks Peter, yes that's the same idea I did as a quick fix.... also don't like the fact that my tool directory is hard-coded but oh well. There must be a way within Python in Galaxy importing something from Galaxy that has the current tool directory path, it would seem that Galaxy needs to know this and would store it anyway?
best, Leandro
Leandro Hermida wrote, On 04/28/2011 08:55 AM:
Hi galaxy developers,
Just want to double-check, there is no way to import some kind of galaxy tool context info into python code you are running for a tool?
Nothing is impossible... just depends on how messy you want to get :) for me, the following works: === python_path.xml === <tool id="cshl_python_path_test" name="pythonpath" description="" > <command>echo '$input1' > '$output'</command> <inputs> <param name="input1" type="text" value="10" label="Dummy"/> </inputs> <code file="python_path_code.py" /> <outputs> <data name="output" format="txt" /> </outputs> </tool> ====== ==== python_path_code.py ===== from os import path import sys def exec_after_process(app, inp_data, out_data, param_dict, tool, stdout, stderr): tool_path = path.abspath(tool.tool_dir); sys.stderr.write("!!!!! path = %s\n" % (tool_path)) ======================== When the tool runs, the following line is printed to STDERR: ====== !!!!! path = /home/gordon/projects/galaxy_dev/tools/cshl_tests ====== Help this helps, -gordon
best, leandro
On Fri, Apr 15, 2011 at 8:03 PM, Leandro Hermida <softdev@leandrohermida.com <mailto:softdev@leandrohermida.com>> wrote:
On Fri, Apr 15, 2011 at 7:27 PM, Peter Cock <p.j.a.cock@googlemail.com <mailto:p.j.a.cock@googlemail.com>> wrote:
On Thu, Apr 14, 2011 at 2:56 PM, Leandro Hermida <softdev@leandrohermida.com <mailto:softdev@leandrohermida.com>> wrote: > On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <p.j.a.cock@googlemail.com <mailto:p.j.a.cock@googlemail.com>> > wrote: >> >> For standard Python tools in Galaxy, I'm using >> os.path.split(sys.argv[0])[0] >> to get the path, which on reflection probably should be written as >> os.path.dirname(sys.argv[0]) as you suggest. >> >> What do __file__ and sys.argv[0] give you? The simplest way to debug >> this is to add a print statement, since Galaxy will show the stdout. >> > > Hi Peter, > > __file__ throws an error: global name '__file__' is not defined
I guess the script is being loaded as a string, and run with eval(...) or something like that. It would also explain why sys.argv[0] would be one of the Galaxy script files.
> os.path.abspath(os.path.dirname(sys.argv[0])) gives me > /path/to/galaxy/scripts directory which is two levels up from what the tool > directory I want for example /path/to/galaxy/tools/mytool
So combine that with ../tools/mytool/ and you're done? OK, you have to know the name of the folder your tool *should* be in... so not a perfect solution.
Thanks Peter, yes that's the same idea I did as a quick fix.... also don't like the fact that my tool directory is hard-coded but oh well. There must be a way within Python in Galaxy importing something from Galaxy that has the current tool directory path, it would seem that Galaxy needs to know this and would store it anyway?
best, Leandro
Hi Assaf, On Thu, Apr 28, 2011 at 7:55 PM, Assaf Gordon <gordon@cshl.edu> wrote:
Leandro Hermida wrote, On 04/28/2011 08:55 AM:
Hi galaxy developers,
Just want to double-check, there is no way to import some kind of galaxy tool context info into python code you are running for a tool?
Nothing is impossible... just depends on how messy you want to get :)
for me, the following works: === python_path.xml === <tool id="cshl_python_path_test" name="pythonpath" description="" > <command>echo '$input1' > '$output'</command> <inputs> <param name="input1" type="text" value="10" label="Dummy"/> </inputs> <code file="python_path_code.py" /> <outputs> <data name="output" format="txt" /> </outputs> </tool> ======
==== python_path_code.py ===== from os import path import sys
def exec_after_process(app, inp_data, out_data, param_dict, tool, stdout, stderr): tool_path = path.abspath(tool.tool_dir); sys.stderr.write("!!!!! path = %s\n" % (tool_path)) ========================
When the tool runs, the following line is printed to STDERR: ====== !!!!! path = /home/gordon/projects/galaxy_dev/tools/cshl_tests ======
Help this helps, -gordon
Thanks so much, I never knew of the page https://bitbucket.org/galaxy/galaxy-central/wiki/CustomCode until seeing what you wrote and searching now. But I have one complication I guess. My tool is already running a Python code file to dynamically create a drop-down menu using the dynamic_options attribute a la: <param type="select" dynamic_options="my_options()"> ... <code file="my_options.py"/> In this function my_options I would need the tool dir path. How would I be able to combine what you did with this? Which function runs at the right time before my_options() is executed is it exec_before_process?
best, leandro
On Fri, Apr 15, 2011 at 8:03 PM, Leandro Hermida <
softdev@leandrohermida.com <mailto:softdev@leandrohermida.com>> wrote:
On Fri, Apr 15, 2011 at 7:27 PM, Peter Cock <
p.j.a.cock@googlemail.com <mailto:p.j.a.cock@googlemail.com>> wrote:
On Thu, Apr 14, 2011 at 2:56 PM, Leandro Hermida <softdev@leandrohermida.com <mailto:softdev@leandrohermida.com>>
wrote:
> On Thu, Apr 14, 2011 at 3:17 PM, Peter Cock <
p.j.a.cock@googlemail.com <mailto:p.j.a.cock@googlemail.com>>
> wrote: >> >> For standard Python tools in Galaxy, I'm using >> os.path.split(sys.argv[0])[0] >> to get the path, which on reflection probably should be
written as
>> os.path.dirname(sys.argv[0]) as you suggest. >> >> What do __file__ and sys.argv[0] give you? The simplest way to
debug
>> this is to add a print statement, since Galaxy will show the
stdout.
>> > > Hi Peter, > > __file__ throws an error: global name '__file__' is not defined
I guess the script is being loaded as a string, and run with
eval(...)
or something like that. It would also explain why sys.argv[0]
would
be one of the Galaxy script files.
> os.path.abspath(os.path.dirname(sys.argv[0])) gives me > /path/to/galaxy/scripts directory which is two levels up from
what the tool
> directory I want for example /path/to/galaxy/tools/mytool
So combine that with ../tools/mytool/ and you're done? OK, you
have
to know the name of the folder your tool *should* be in... so not
a
perfect solution.
Thanks Peter, yes that's the same idea I did as a quick fix.... also
don't like the fact that my tool directory is hard-coded but oh well. There must be a way within Python in Galaxy importing something from Galaxy that has the current tool directory path, it would seem that Galaxy needs to know this and would store it anyway?
best, Leandro
Hi Leandro, Leandro Hermida wrote, On 04/29/2011 06:17 AM:
But I have one complication I guess. My tool is already running a Python code file to dynamically create a drop-down menu using the dynamic_options attribute a la:
<param type="select" dynamic_options="my_options()"> ... <code file="my_options.py"/>
In this function my_options I would need the tool dir path. How would I be able to combine what you did with this? Which function runs at the right time before my_options() is executed is it exec_before_process?
This just gets messier and messier :) The following hack will get you the tool's path inside the "dynamic option" code, but it is SO ugly, I would highly recommend against it. A better way would be to add some code to "./galaxy/lib/parameters/basic.py" inside "get_options()" to add the 'tool' object to the "other_values" object, so that custom code functions would be able to access it (or something similar and as clean). That being said, here goes: ========= dynamic_options_path.xml ================== <tool id="cshl_dynamic_optinos_path_test" name="dynamic_options_path" description="" > <command>echo '$input1' > '$output'</command> <inputs> <param name="input1" type="text" value="10" label="Dummy"/> <param name="mylist" type="select" label="Dynamic Options" dynamic_options="get_my_options(code_ref=get_my_options)" display="radio" /> </inputs> <code file="dynamic_options_code.py" /> <outputs> <data name="output" format="txt" /> </outputs> </tool> =========================== ====== dynamic_options_code.py ============== import os import os.path import sys def get_my_options(code_ref): root_dir=os.getcwd() sys.stderr.write("Galaxy root dir = %s\n" % ( root_dir ) ) sys.stderr.write("Custom code file = %s\n" % ( code_ref.__code__.co_filename) ) tool_path = root_dir + '/' + os.path.dirname( code_ref.__code__.co_filename) sys.stderr.write("tool path = %s\n" % ( tool_path ) ) res = [('Hello','World',False),] return res ========================== The output (STDERR) when viewing the tool's form is: Galaxy root dir = /home/gordon/projects/galaxy_dev Custom code file = ./tools/cshl_tests/dynamic_options_code.py tool path = /home/gordon/projects/galaxy_dev/./tools/cshl_tests The trick is that the dynamic function itself ("get_my_options()") is a valid object (which is luckily accessible from inside the code), and python keeps track of the file that contained the function. Good luck (and don't try this at home :) ), -gordon
participants (3)
-
Assaf Gordon
-
Leandro Hermida
-
Peter Cock