#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import argparse
import re
from bioblend import galaxy
from bioblend import toolshed


def retrieve_changeset_revision(ts_url, name, owner):
    ts = toolshed.ToolShedInstance(url=ts_url)
    ts_repositories = ts.repositories.get_repositories()

    ts_id = None
    for repo in ts_repositories:
        if str(repo['name']) == name and str(repo['owner']) == owner:
            ts_id = repo['id']

    if ts_id == None:
        string = "No repository found for " + name + " (" + owner + ")"
        string += " in toolshed at " + ts_url
        raise ValueError(string)

    return ts.repositories.show_repository_revision(ts_id)['changeset_revision']

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gi_url', required=True)
    parser.add_argument('--ts_url', required=True)
    parser.add_argument('--api_key', required=True)
    parser.add_argument('--tool_owner', required=True)
    parser.add_argument('--tool_name', required=True)
    parser.add_argument('--tool_panel_section_id', required=True)
    args = parser.parse_args()

    gi = galaxy.GalaxyInstance(url=args.gi_url, key=args.api_key)
    
    changeset_revision = retrieve_changeset_revision(args.ts_url, args.tool_name, 
        args.tool_owner)
    print changeset_revision

    #gi.toolShed.install_repository_revision(ts_url, args.tool_name, args.tool_owner, 
    #    changeset_revision, install_tool_dependencies=True, 
    #    install_repository_dependencies=True, 
    #    tool_panel_section_id=args.tool_panel_section_id)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bioblend import galaxy
from bioblend import toolshed


if __name__ == '__main__':
    gi_url = "http://172.21.23.6:8080/"
    ts_url = "http://172.21.23.6:9009/"
    name = "qiime"
    owner = "iuc"
    tool_panel_section_id = "qiime_rRNA_taxonomic_assignation"

    gi = galaxy.GalaxyInstance(url=gi_url, key='8a099e97b0a83c73ead9f5b0fe19f4be')

    ts = toolshed.ToolShedInstance(url=ts_url)

    changeset_revision = str(ts.repositories.get_ordered_installable_revisions(name,
        owner)[-1])

    gi.toolShed.install_repository_revision(ts_url, name, owner, changeset_revision,
        install_tool_dependencies=True, install_repository_dependencies=True, 
        tool_panel_section_id=tool_panel_section_id)


--- 
+++ 
@@ -1,25 +1,48 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+import sys
+import os
+import argparse
+import re
 from bioblend import galaxy
 from bioblend import toolshed
 
 
+def retrieve_changeset_revision(ts_url, name, owner):
+    ts = toolshed.ToolShedInstance(url=ts_url)
+    ts_repositories = ts.repositories.get_repositories()
+
+    ts_id = None
+    for repo in ts_repositories:
+        if str(repo['name']) == name and str(repo['owner']) == owner:
+            ts_id = repo['id']
+
+    if ts_id == None:
+        string = "No repository found for " + name + " (" + owner + ")"
+        string += " in toolshed at " + ts_url
+        raise ValueError(string)
+
+    return ts.repositories.show_repository_revision(ts_id)['changeset_revision']
+
 if __name__ == '__main__':
-    gi_url = "http://172.21.23.6:8080/"
-    ts_url = "http://172.21.23.6:9009/"
-    name = "qiime"
-    owner = "iuc"
-    tool_panel_section_id = "qiime_rRNA_taxonomic_assignation"
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--gi_url', required=True)
+    parser.add_argument('--ts_url', required=True)
+    parser.add_argument('--api_key', required=True)
+    parser.add_argument('--tool_owner', required=True)
+    parser.add_argument('--tool_name', required=True)
+    parser.add_argument('--tool_panel_section_id', required=True)
+    args = parser.parse_args()
 
-    gi = galaxy.GalaxyInstance(url=gi_url, key='8a099e97b0a83c73ead9f5b0fe19f4be')
+    gi = galaxy.GalaxyInstance(url=args.gi_url, key=args.api_key)
+    
+    changeset_revision = retrieve_changeset_revision(args.ts_url, args.tool_name, 
+        args.tool_owner)
+    print changeset_revision
 
-    ts = toolshed.ToolShedInstance(url=ts_url)
+    #gi.toolShed.install_repository_revision(ts_url, args.tool_name, args.tool_owner, 
+    #    changeset_revision, install_tool_dependencies=True, 
+    #    install_repository_dependencies=True, 
+    #    tool_panel_section_id=args.tool_panel_section_id)
 
-    changeset_revision = str(ts.repositories.get_ordered_installable_revisions(name,
-        owner)[-1])
-
-    gi.toolShed.install_repository_revision(ts_url, name, owner, changeset_revision,
-        install_tool_dependencies=True, install_repository_dependencies=True, 
-        tool_panel_section_id=tool_panel_section_id)
-
Improve integrate tool wrapper with arguments
