comparison toolfactory/rgToolFactory2.py @ 1:48458b0369aa draft

Uploaded
author fubar
date Sat, 17 Apr 2021 22:50:25 +0000
parents fc50a3f507ab
children
comparison
equal deleted inserted replaced
0:fc50a3f507ab 1:48458b0369aa
26 import sys 26 import sys
27 import tarfile 27 import tarfile
28 import tempfile 28 import tempfile
29 import time 29 import time
30 30
31 from bioblend import ConnectionError
32 from bioblend import toolshed
33
31 import galaxyxml.tool as gxt 34 import galaxyxml.tool as gxt
32 import galaxyxml.tool.parameters as gxtp 35 import galaxyxml.tool.parameters as gxtp
36
33 import lxml 37 import lxml
38
34 import yaml 39 import yaml
35 from bioblend import ConnectionError
36 from bioblend import toolshed
37 40
38 myversion = "V2.2 February 2021" 41 myversion = "V2.2 February 2021"
39 verbose = True 42 verbose = True
40 debug = True 43 debug = True
41 toolFactoryURL = "https://github.com/fubar2/toolfactory" 44 toolFactoryURL = "https://github.com/fubar2/toolfactory"
74 """Wrapper for an arbitrary script 77 """Wrapper for an arbitrary script
75 uses galaxyxml 78 uses galaxyxml
76 79
77 """ 80 """
78 81
79 def __init__(self, args=None): 82 def __init__(self, args=None): # noqa
80 """ 83 """
81 prepare command line cl for running the tool here 84 prepare command line cl for running the tool here
82 and prepare elements needed for galaxyxml tool generation 85 and prepare elements needed for galaxyxml tool generation
83 """ 86 """
84 self.ourcwd = os.getcwd() 87 self.ourcwd = os.getcwd()
227 230
228 def prepargp(self): 231 def prepargp(self):
229 clsuffix = [] 232 clsuffix = []
230 xclsuffix = [] 233 xclsuffix = []
231 for i, p in enumerate(self.infiles): 234 for i, p in enumerate(self.infiles):
235 nam = p["infilename"]
232 if p["origCL"].strip().upper() == "STDIN": 236 if p["origCL"].strip().upper() == "STDIN":
233 appendme = [ 237 appendme = [
234 p["infilename"], 238 nam,
235 p["infilename"], 239 nam,
236 "< %s" % p["infilename"], 240 "< %s" % nam,
237 ] 241 ]
238 xappendme = [ 242 xappendme = [
239 p["infilename"], 243 nam,
240 p["infilename"], 244 nam,
241 "< $%s" % p["infilename"], 245 "< $%s" % nam,
242 ] 246 ]
243 else: 247 else:
248 rep = p["repeat"] == "1"
249 over = ""
250 if rep:
251 over = f'#for $rep in $R_{nam}:\n--{nam} "$rep.{nam}"\n#end for'
244 appendme = [p["CL"], p["CL"], ""] 252 appendme = [p["CL"], p["CL"], ""]
245 xappendme = [p["CL"], "$%s" % p["CL"], ""] 253 xappendme = [p["CL"], "$%s" % p["CL"], over]
246 clsuffix.append(appendme) 254 clsuffix.append(appendme)
247 xclsuffix.append(xappendme) 255 xclsuffix.append(xappendme)
248 for i, p in enumerate(self.outfiles): 256 for i, p in enumerate(self.outfiles):
249 if p["origCL"].strip().upper() == "STDOUT": 257 if p["origCL"].strip().upper() == "STDOUT":
250 self.lastclredirect = [">", p["name"]] 258 self.lastclredirect = [">", p["name"]]
254 xclsuffix.append([p["name"], "$%s" % p["name"], ""]) 262 xclsuffix.append([p["name"], "$%s" % p["name"], ""])
255 for p in self.addpar: 263 for p in self.addpar:
256 nam = p["name"] 264 nam = p["name"]
257 rep = p["repeat"] == "1" 265 rep = p["repeat"] == "1"
258 if rep: 266 if rep:
259 over = f" #for $rep in $R_{nam}:\n--{nam} $rep.{nam}\n#end for" 267 over = f'#for $rep in $R_{nam}:\n--{nam} "$rep.{nam}"\n#end for'
260 else: 268 else:
261 over = p["override"] 269 over = p["override"]
262 clsuffix.append([p["CL"], nam, over]) 270 clsuffix.append([p["CL"], nam, over])
263 xclsuffix.append([p["CL"], nam, over]) 271 xclsuffix.append([p["CL"], nam, over])
264 for p in self.selpar: 272 for p in self.selpar:
421 if len(newname) < 2: 429 if len(newname) < 2:
422 ndash = 1 430 ndash = 1
423 return ndash 431 return ndash
424 432
425 def doXMLparam(self): 433 def doXMLparam(self):
426 """flake8 made me do this...""" 434 """Add all needed elements to tool""" # noqa
427 for p in self.outfiles: 435 for p in self.outfiles:
428 newname = p["name"] 436 newname = p["name"]
429 newfmt = p["format"] 437 newfmt = p["format"]
430 newcl = p["CL"] 438 newcl = p["CL"]
431 test = p["test"] 439 test = p["test"]
484 self.testparam.append(tp) 492 self.testparam.append(tp)
485 for p in self.infiles: 493 for p in self.infiles:
486 newname = p["infilename"] 494 newname = p["infilename"]
487 newfmt = p["format"] 495 newfmt = p["format"]
488 ndash = self.getNdash(newname) 496 ndash = self.getNdash(newname)
489 reps = p.get("repeat", 0) == 1 497 reps = p.get("repeat", "0") == "1"
490 if not len(p["label"]) > 0: 498 if not len(p["label"]) > 0:
491 alab = p["CL"] 499 alab = p["CL"]
492 else: 500 else:
493 alab = p["label"] 501 alab = p["label"]
494 aninput = gxtp.DataParam( 502 aninput = gxtp.DataParam(
672 anout.positional = self.is_positional 680 anout.positional = self.is_positional
673 self.toutputs.append(anout) 681 self.toutputs.append(anout)
674 tp = gxtp.TestOutput(name=newname, value="%s_sample" % newname) 682 tp = gxtp.TestOutput(name=newname, value="%s_sample" % newname)
675 self.testparam.append(tp) 683 self.testparam.append(tp)
676 684
677 def makeXML(self): 685 def makeXML(self): # noqa
678 """ 686 """
679 Create a Galaxy xml tool wrapper for the new script 687 Create a Galaxy xml tool wrapper for the new script
680 Uses galaxyhtml 688 Uses galaxyhtml
681 Hmmm. How to get the command line into correct order... 689 Hmmm. How to get the command line into correct order...
682 """ 690 """
710 ) 718 )
711 scr.append("\n") 719 scr.append("\n")
712 safertext = safertext + "\n".join(scr) 720 safertext = safertext + "\n".join(scr)
713 self.newtool.help = safertext 721 self.newtool.help = safertext
714 self.newtool.version_command = f'echo "{self.args.tool_version}"' 722 self.newtool.version_command = f'echo "{self.args.tool_version}"'
723 std = gxtp.Stdios()
724 std1 = gxtp.Stdio()
725 std.append(std1)
726 self.newtool.stdios = std
715 requirements = gxtp.Requirements() 727 requirements = gxtp.Requirements()
716 if self.args.packages: 728 if self.args.packages:
717 for d in self.args.packages.split(","): 729 for d in self.args.packages.split(","):
718 ver = "" 730 ver = ""
719 d = d.replace("==", ":") 731 d = d.replace("==", ":")
723 else: 735 else:
724 packg = d 736 packg = d
725 requirements.append( 737 requirements.append(
726 gxtp.Requirement("package", packg.strip(), ver.strip()) 738 gxtp.Requirement("package", packg.strip(), ver.strip())
727 ) 739 )
728 self.newtool.requirements = requirements 740 self.newtool.requirements = requirements
729 if self.args.parampass == "0": 741 if self.args.parampass == "0":
730 self.doNoXMLparam() 742 self.doNoXMLparam()
731 else: 743 else:
732 self.doXMLparam() 744 self.doXMLparam()
733 self.newtool.outputs = self.toutputs 745 self.newtool.outputs = self.toutputs
1040 else: 1052 else:
1041 tout = open(self.tlog, "w") 1053 tout = open(self.tlog, "w")
1042 cll = [ 1054 cll = [
1043 "planemo", 1055 "planemo",
1044 "test", 1056 "test",
1057 "--galaxy_python_version",
1058 self.args.python_version,
1045 "--conda_auto_init", 1059 "--conda_auto_init",
1046 "--test_data", 1060 "--test_data",
1047 os.path.abspath(self.testdir), 1061 os.path.abspath(self.testdir),
1048 "--test_output", 1062 "--test_output",
1049 os.path.abspath(tool_test_path), 1063 os.path.abspath(tool_test_path),
1059 stderr=tout, 1073 stderr=tout,
1060 stdout=tout, 1074 stdout=tout,
1061 ) 1075 )
1062 tout.close() 1076 tout.close()
1063 return p.returncode 1077 return p.returncode
1078
1079 def set_planemo_galaxy_root(self, galaxyroot='/galaxy-central', config_path=".planemo.yml"):
1080 # bug in planemo - bogus '--dev-wheels' passed to run_tests.sh as at april 2021 - need a fiddled copy so it is ignored until fixed
1081 CONFIG_TEMPLATE = """## Planemo Global Configuration File.
1082 ## Everything in this file is completely optional - these values can all be
1083 ## configured via command line options for the corresponding commands.
1084
1085 ## Specify a default galaxy_root for test and server commands here.
1086 galaxy_root: %s
1087 ## Username used with toolshed(s).
1088 #shed_username: "<TODO>"
1089 sheds:
1090 # For each tool shed you wish to target, uncomment key or both email and
1091 # password.
1092 toolshed:
1093 #key: "<TODO>"
1094 #email: "<TODO>"
1095 #password: "<TODO>"
1096 testtoolshed:
1097 #key: "<TODO>"
1098 #email: "<TODO>"
1099 #password: "<TODO>"
1100 local:
1101 #key: "<TODO>"
1102 #email: "<TODO>"
1103 #password: "<TODO>"
1104 """
1105 if not os.path.exists(config_path):
1106 with open(config_path, "w") as f:
1107 f.write(CONFIG_TEMPLATE % galaxyroot)
1064 1108
1065 1109
1066 def main(): 1110 def main():
1067 """ 1111 """
1068 This is a Galaxy wrapper. 1112 This is a Galaxy wrapper.
1103 a("--galaxy_api_key", default="fakekey") 1147 a("--galaxy_api_key", default="fakekey")
1104 a("--galaxy_root", default="/galaxy-central") 1148 a("--galaxy_root", default="/galaxy-central")
1105 a("--galaxy_venv", default="/galaxy_venv") 1149 a("--galaxy_venv", default="/galaxy_venv")
1106 a("--collection", action="append", default=[]) 1150 a("--collection", action="append", default=[])
1107 a("--include_tests", default=False, action="store_true") 1151 a("--include_tests", default=False, action="store_true")
1152 a("--python_version", default="3.9")
1108 args = parser.parse_args() 1153 args = parser.parse_args()
1109 assert not args.bad_user, ( 1154 assert not args.bad_user, (
1110 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy \ 1155 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy \
1111 admin adds %s to "admin_users" in the galaxy.yml Galaxy configuration file' 1156 admin adds %s to "admin_users" in the galaxy.yml Galaxy configuration file'
1112 % (args.bad_user, args.bad_user) 1157 % (args.bad_user, args.bad_user)
1122 if args.make_Tool == "generate": 1167 if args.make_Tool == "generate":
1123 r.run() 1168 r.run()
1124 r.moveRunOutputs() 1169 r.moveRunOutputs()
1125 r.makeToolTar() 1170 r.makeToolTar()
1126 else: 1171 else:
1127 # r.planemo_test(genoutputs=True) # this fails :( - see PR
1128 # r.moveRunOutputs()
1129 # r.makeToolTar(report_fail=False)
1130 r.planemo_test_once() 1172 r.planemo_test_once()
1131 r.moveRunOutputs() 1173 r.moveRunOutputs()
1132 r.makeToolTar(report_fail=True) 1174 r.makeToolTar(report_fail=True)
1133 if args.make_Tool == "gentestinstall": 1175 if args.make_Tool == "gentestinstall":
1134 r.shedLoad() 1176 r.shedLoad()