|
1 | 1 | #!/usr/bin/env python |
2 | 2 | import argparse |
3 | 3 | import json |
| 4 | +import os |
| 5 | +import sys |
4 | 6 |
|
5 | 7 | from bioblend import galaxy |
6 | 8 |
|
7 | 9 |
|
| 10 | +def import_workflow(gi, path): |
| 11 | + """ |
| 12 | + Given a connection to a Galaxy Instance (gi) and a path to a Galaxy workflow file, |
| 13 | + this function will import the worklfow into Galaxy. |
| 14 | + """ |
| 15 | + with open(path, 'r') as wf_file: |
| 16 | + import_uuid = json.load(wf_file).get('uuid') |
| 17 | + existing_uuids = [d.get('latest_workflow_uuid') for d in gi.workflows.get_workflows()] |
| 18 | + if import_uuid not in existing_uuids: |
| 19 | + gi.workflows.import_workflow_from_local_path(path) |
| 20 | + |
| 21 | + |
8 | 22 | def main(): |
9 | 23 | """ |
10 | 24 | This script uses bioblend to import .ga workflow files into a running instance of Galaxy |
11 | 25 | """ |
12 | 26 | parser = argparse.ArgumentParser() |
13 | | - parser.add_argument("-w", "--workflow_path", help='Path to workflow file') |
| 27 | + parser.add_argument("-w", "--workflow_path", |
| 28 | + help='Path to a workflow file or a directory with multiple workflow files ending with ".ga"') |
14 | 29 | parser.add_argument("-g", "--galaxy", |
15 | | - dest="galaxy_url", |
16 | | - help="Target Galaxy instance URL/IP address (required " |
17 | | - "if not defined in the tools list file)",) |
18 | | - parser.add_argument("-a", "--apikey", |
| 30 | + help="Target Galaxy instance URL/IP address.") |
| 31 | + parser.add_argument("-u", "--user", |
| 32 | + help="Galaxy user name") |
| 33 | + parser.add_argument("-p", "--password", |
| 34 | + help="Password for the Galaxy user") |
| 35 | + parser.add_argument("-a", "--api_key", |
19 | 36 | dest="api_key", |
20 | | - help="Galaxy admin user API key (required if not " |
21 | | - "defined in the tools list file)",) |
| 37 | + help="Galaxy admin user API key (required if not defined in the tools list file)") |
| 38 | + |
22 | 39 | args = parser.parse_args() |
23 | 40 |
|
24 | | - gi = galaxy.GalaxyInstance(url=args.galaxy_url, key=args.api_key) |
| 41 | + if args.user and args.password: |
| 42 | + gi = galaxy.GalaxyInstance(url=args.galaxy, email=args.user, password=args.password) |
| 43 | + elif args.api_key: |
| 44 | + gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.api_key) |
| 45 | + else: |
| 46 | + sys.exit('Please specify either a valid Galaxy username/password or an API key.') |
25 | 47 |
|
26 | | - with open(args.workflow_path, 'r') as wf_file: |
27 | | - import_uuid = json.load(wf_file).get('uuid') |
28 | | - existing_uuids = [d.get('latest_workflow_uuid') for d in gi.workflows.get_workflows()] |
29 | | - if import_uuid not in existing_uuids: |
30 | | - gi.workflows.import_workflow_from_local_path(args.workflow_path) |
| 48 | + if os.path.isdir(args.workflow_path): |
| 49 | + for file_path in os.listdir(args.workflow_path): |
| 50 | + if file_path.endswith('.ga'): |
| 51 | + import_workflow(gi, os.path.join(args.workflow_path, file_path)) |
| 52 | + else: |
| 53 | + import_workflow(gi, args.workflow_path) |
31 | 54 |
|
32 | 55 |
|
33 | 56 | if __name__ == '__main__': |
|
0 commit comments