Mercurial > repos > fubar > tool_factory_2
comparison toolfactory/rgToolFactory2.py @ 99:d4d88d393285 draft
Uploaded
author | fubar |
---|---|
date | Mon, 23 Nov 2020 02:22:01 +0000 |
parents | 67628c7dc9f3 |
children | c749364c2283 |
comparison
equal
deleted
inserted
replaced
98:67628c7dc9f3 | 99:d4d88d393285 |
---|---|
21 # one to run the executable with the supplied test data and settings | 21 # one to run the executable with the supplied test data and settings |
22 # Be simpler to write the tool, then run it with planemo and soak up the test outputs. | 22 # Be simpler to write the tool, then run it with planemo and soak up the test outputs. |
23 # well well. sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox | 23 # well well. sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox |
24 # does the needful. Use GALAXY_TEST_SAVE /foo to save outputs - only the tar.gz - not the rest sadly | 24 # does the needful. Use GALAXY_TEST_SAVE /foo to save outputs - only the tar.gz - not the rest sadly |
25 # GALAXY_TEST_NO_CLEANUP GALAXY_TEST_TMP_DIR=wherever | 25 # GALAXY_TEST_NO_CLEANUP GALAXY_TEST_TMP_DIR=wherever |
26 # planemo test --engine docker_galaxy --test_data ./test-data/ --docker_extra_volume ./test-data rgToolFactory2.xml | |
26 | 27 |
27 import argparse | 28 import argparse |
28 import datetime | 29 import datetime |
29 import json | 30 import json |
30 import logging | 31 import logging |
668 if retval != 0 and err: # problem | 669 if retval != 0 and err: # problem |
669 sys.stderr.write(err) | 670 sys.stderr.write(err) |
670 logging.debug("run done") | 671 logging.debug("run done") |
671 return retval | 672 return retval |
672 | 673 |
674 | |
675 def gal_tool_test(self): | |
676 """ | |
677 This handy script writes test outputs even if they don't exist | |
678 galaxy-tool-test [-h] [-u GALAXY_URL] [-k KEY] [-a ADMIN_KEY] [--force_path_paste] [-t TOOL_ID] [--tool-version TOOL_VERSION] | |
679 [-i TEST_INDEX] [-o OUTPUT] [--append] [-j OUTPUT_JSON] [--verbose] [-c CLIENT_TEST_CONFIG] | |
680 galaxy-tool-test -u http://localhost:8080 -a 3c9afe09f1b7892449d266109639c104 -o /tmp/foo -t hello -j /tmp/foo/hello.json --verbose | |
681 handy - just leaves outputs in -o | |
682 """ | |
683 if os.path.exists(self.tlog): | |
684 tout = open(self.tlog, "a") | |
685 else: | |
686 tout = open(self.tlog, "w") | |
687 testouts = tempfile.mkdtemp(suffix=None, prefix="tftemp") | |
688 dummy, tfile = tempfile.mkstemp() | |
689 cll = [ | |
690 os.path.join(self.args.tool_dir,"galaxy-tool-test"), | |
691 "-u", | |
692 self.args.galaxy_url, | |
693 "-k", | |
694 self.args.galaxy_api_key, | |
695 "-t", | |
696 self.args.tool_name, | |
697 "-o", | |
698 testouts, | |
699 ] | |
700 subp = subprocess.run( | |
701 cll, shell=False, stderr=dummy, stdout=dummy | |
702 ) | |
703 outfiles = [] | |
704 for p in self.outfiles: | |
705 oname = p[ONAMEPOS] | |
706 outfiles.append(oname) | |
707 with os.scandir(testouts) as outs: | |
708 for entry in outs: | |
709 if not entry.is_file(): | |
710 continue | |
711 dest = os.path.join(self.tooloutdir, entry.name) | |
712 src = os.path.join(testouts, entry.name) | |
713 shutil.copyfile(src, dest) | |
714 dest = os.path.join(self.testdir, entry.name) | |
715 src = os.path.join(testouts, entry.name) | |
716 shutil.copyfile(src, dest) | |
717 dest = os.path.join(self.repdir,f"{entry.name}_sample") | |
718 tout.write(f"## found and moved output {entry.name} to {dest}\n") | |
719 tout.close() | |
720 shutil.rmtree(testouts) | |
721 return subp.returncode | |
722 | |
723 def gal_test(self): | |
724 """ | |
725 Uses the built in galaxy tool tester run_test.sh | |
726 | |
727 export GALAXY_TEST_SAVE="./foo" && export GALAXY_TEST_NO_CLEANUP="1" \ | |
728 && export GALAXY_TEST_TMP_DIR=./foo && sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox | |
729 | |
730 """ | |
731 testdir = tempfile.mkdtemp(suffix=None, prefix="tftemp") | |
732 tool_test_rep = f"{self.tool_name}_galaxy_test_report_html.html" | |
733 if os.path.exists(self.tlog): | |
734 tout = open(self.tlog, "a") | |
735 else: | |
736 tout = open(self.tlog, "w") | |
737 | |
738 ourenv = os.environ | |
739 ourenv["GALAXY_TEST_SAVE"] = testdir | |
740 ourenv["GALAXY_TEST_NO_CLEANUP"] = "1" | |
741 ourenv["GALAXY_TEST_TMP_DIR"] = testdir | |
742 | |
743 cll = [ | |
744 "sh", f"{self.args.galaxy_root}/run_tests.sh", "--id", self.args.tool_name, | |
745 "--report_file", os.path.join(testdir,tool_test_rep), "functional.test_toolbox", | |
746 ] | |
747 subp = subprocess.run( | |
748 cll, env = ourenv, | |
749 shell=False, cwd=self.args.galaxy_root, stderr=tout, stdout=tout | |
750 ) | |
751 src = os.path.join(testdir, tool_test_rep) | |
752 if os.path.isfile(src): | |
753 dest = os.path.join(self.repdir, tool_test_rep) | |
754 shutil.copyfile(src, dest) | |
755 else: | |
756 tout.write(f"### {src} not found\n") | |
757 tout.close() | |
758 return subp.returncode | |
759 | |
760 | |
673 def shedLoad(self): | 761 def shedLoad(self): |
674 """ | 762 """ |
675 {'deleted': False, | 763 {'deleted': False, |
676 'description': 'Tools for manipulating data', | 764 'description': 'Tools for manipulating data', |
677 'id': '175812cd7caaf439', | 765 'id': '175812cd7caaf439', |
751 "installed %s - got retcode %d\n" % (self.args.tool_name, subp.returncode) | 839 "installed %s - got retcode %d\n" % (self.args.tool_name, subp.returncode) |
752 ) | 840 ) |
753 tout.close() | 841 tout.close() |
754 return subp.returncode | 842 return subp.returncode |
755 | 843 |
756 def planemo_shedload(self): | 844 def planemo_shedLoad(self): |
757 """ | 845 """ |
758 planemo shed_create --shed_target testtoolshed | 846 planemo shed_create --shed_target testtoolshed |
759 planemo shed_init --name=<name> | 847 planemo shed_init --name=<name> |
760 --owner=<shed_username> | 848 --owner=<shed_username> |
761 --description=<short description> | 849 --description=<short description> |
997 ) | 1085 ) |
998 tout.close() | 1086 tout.close() |
999 return subp.returncode | 1087 return subp.returncode |
1000 | 1088 |
1001 | 1089 |
1002 def gal_test(self, genoutputs=True): | |
1003 """ | |
1004 export GALAXY_TEST_SAVE="./foo" && export GALAXY_TEST_NO_CLEANUP="1" \ | |
1005 && export GALAXY_TEST_TMP_DIR=./foo && sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox | |
1006 | |
1007 """ | |
1008 tool_test_rep_path = os.path.join(self.repdir,f"{self.tool_name}_galaxy_test_report_html.html") | |
1009 if os.path.exists(self.tlog): | |
1010 tout = open(self.tlog, "a") | |
1011 else: | |
1012 tout = open(self.tlog, "w") | |
1013 testdir = '/tmp/testdir' | |
1014 ourenv = os.environ | |
1015 ourenv["GALAXY_TEST_SAVE"] = os.abspath(testdir) | |
1016 ourenv["GALAXY_TEST_NO_CLEANUP"] = "1" | |
1017 ourenv["GALAXY_TEST_TMP_DIR"] = os.abspath(testdir) | |
1018 if genoutputs: | |
1019 | |
1020 if not os.path.exists(testdir): | |
1021 os.mkdir(testdir) | |
1022 dummy = open('failedtest.log','w') | |
1023 cll = [ | |
1024 "sh", f"{self.args.galaxy_root}/run_tests.sh", "--id", self.args.tool_name, | |
1025 "--report_file", tool_test_rep_path, "functional.test_toolbox", | |
1026 ] | |
1027 subp = subprocess.run( | |
1028 cll, | |
1029 shell=False, | |
1030 env = ourenv, | |
1031 cwd=self.testdir, | |
1032 stderr=dummy, | |
1033 stdout=dummy, | |
1034 ) | |
1035 dummy.close() | |
1036 # if all went well, tgz is in ./test and down a nest of tmp directories lurk the output files | |
1037 outfiles = [] | |
1038 for p in self.outfiles: | |
1039 oname = p[ONAMEPOS] | |
1040 outfiles.append(oname) | |
1041 paths = [] | |
1042 for root, dirs, files in os.walk(testdir): | |
1043 for f in files: | |
1044 tout.write(f"{root} {f}\n") | |
1045 if f in outfiles: | |
1046 paths.append([root,f]) | |
1047 tout.write(f"## found output {f}") | |
1048 | |
1049 if len(paths) != len(self.outfiles): | |
1050 tout.write(f"## problem - outfiles not all found") | |
1051 for path in paths: | |
1052 fname = path[1] | |
1053 src = os.path.join(path[0],fname) | |
1054 dest = os.path.join(self.testdir,f"{fname}_sample") | |
1055 shutil.copyfile(src,dest) | |
1056 else: | |
1057 cll = [ | |
1058 "sh", f"{self.args.galaxy_root}/run_tests.sh", "--id", self.args.tool_name, | |
1059 "--report_file", tool_test_rep_path, "functional.test_toolbox", | |
1060 ] | |
1061 subp = subprocess.run( | |
1062 cll, env = ourenv, | |
1063 shell=False, cwd=self.testdir, stderr=tout, stdout=tout | |
1064 ) | |
1065 tout.close() | |
1066 return subp.returncode | |
1067 | |
1068 | |
1069 | |
1070 def writeShedyml(self): | 1090 def writeShedyml(self): |
1071 """for planemo""" | 1091 """for planemo""" |
1072 yuser = self.args.user_email.split("@")[0] | 1092 yuser = self.args.user_email.split("@")[0] |
1073 yfname = os.path.join(self.tooloutdir, ".shed.yml") | 1093 yfname = os.path.join(self.tooloutdir, ".shed.yml") |
1074 yamlf = open(yfname, "w") | 1094 yamlf = open(yfname, "w") |
1110 else tarinfo | 1130 else tarinfo |
1111 ) | 1131 ) |
1112 | 1132 |
1113 for p in self.outfiles: | 1133 for p in self.outfiles: |
1114 oname = p[ONAMEPOS] | 1134 oname = p[ONAMEPOS] |
1115 src = os.path.join(self.testdir,oname) | 1135 tdest = os.path.join(self.testdir, "%s_sample" % oname) |
1116 if os.path.isfile(src): | 1136 if not os.path.isfile(tdest): |
1117 dest = os.path.join(self.testdir, "%s_sample" % oname) | 1137 src = os.path.join(self.testdir,oname) |
1118 shutil.copyfile(src, dest) | 1138 if os.path.isfile(src): |
1119 dest = os.path.join(self.repdir, "%s.sample" % (oname)) | 1139 shutil.copyfile(src, tdest) |
1120 shutil.copyfile(src, dest) | 1140 dest = os.path.join(self.repdir, "%s.sample" % (oname)) |
1121 else: | 1141 shutil.copyfile(src, dest) |
1122 print( | 1142 else: |
1123 "### problem - output file %s not found in tooloutdir %s" | 1143 print( |
1124 % (src, self.tooloutdir) | 1144 "### problem - output file %s not found in testdir %s" |
1125 ) | 1145 % (tdest, self.testdir) |
1146 ) | |
1126 tf = tarfile.open(self.newtarpath, "w:gz") | 1147 tf = tarfile.open(self.newtarpath, "w:gz") |
1127 tf.add(name=self.tooloutdir, arcname=self.tool_name, filter=exclude_function) | 1148 tf.add(name=self.tooloutdir, arcname=self.tool_name, filter=exclude_function) |
1128 tf.close() | 1149 tf.close() |
1129 shutil.copyfile(self.newtarpath, self.args.new_tool) | 1150 shutil.copyfile(self.newtarpath, self.args.new_tool) |
1130 | |
1131 def fakeToolTar(self): | |
1132 """move fake outputs into test-data and prepare the tarball""" | |
1133 excludeme = "tool_test_output" | |
1134 | |
1135 def exclude_function(tarinfo): | |
1136 filename = tarinfo.name | |
1137 return ( | |
1138 None | |
1139 if filename.startswith(excludeme) | |
1140 else tarinfo | |
1141 ) | |
1142 | |
1143 for p in self.outfiles: | |
1144 oname = p[ONAMEPOS] | |
1145 src = os.path.join(self.testdir,oname) | |
1146 if os.path.isfile(src): | |
1147 dest = os.path.join(self.testdir, "%s_sample" % oname) | |
1148 shutil.copyfile(src, dest) | |
1149 dest = os.path.join(self.repdir, "%s.sample" % (oname)) | |
1150 shutil.copyfile(src, dest) | |
1151 else: | |
1152 with open(src,'w') as fayk: | |
1153 fayk.write('fake!\n') | |
1154 dest = os.path.join(self.testdir, "%s_sample" % oname) | |
1155 shutil.copyfile(src, dest) | |
1156 print( | |
1157 "### problem - output file %s not found in tooloutdir %s so faked" | |
1158 % (src, self.tooloutdir) | |
1159 ) | |
1160 tf = tarfile.open(self.newtarpath, "w:gz") | |
1161 tf.add(name=self.tooloutdir, arcname=self.tool_name, filter=exclude_function) | |
1162 tf.close() | |
1163 shutil.copyfile(self.newtarpath, self.args.new_tool) | |
1164 | |
1165 | 1151 |
1166 def moveRunOutputs(self): | 1152 def moveRunOutputs(self): |
1167 """need to move planemo or run outputs into toolfactory collection""" | 1153 """need to move planemo or run outputs into toolfactory collection""" |
1168 with os.scandir(self.tooloutdir) as outs: | 1154 with os.scandir(self.tooloutdir) as outs: |
1169 for entry in outs: | 1155 for entry in outs: |
1252 if args.make_Tool == "generate": | 1238 if args.make_Tool == "generate": |
1253 retcode = r.run() | 1239 retcode = r.run() |
1254 r.moveRunOutputs() | 1240 r.moveRunOutputs() |
1255 r.makeToolTar() | 1241 r.makeToolTar() |
1256 else: | 1242 else: |
1257 r.fakeToolTar() | 1243 r.makeToolTar() |
1244 #r.planemo_shedLoad() | |
1258 r.shedLoad() | 1245 r.shedLoad() |
1259 r.eph_galaxy_load() | 1246 r.eph_galaxy_load() |
1260 retcode = r.gal_test(genoutputs=True) # this fails | 1247 retcode = r.gal_tool_test() # writes outputs |
1261 r.makeToolTar() | 1248 r.makeToolTar() |
1262 retcode = r.gal_test(genoutputs=False) | 1249 #r.planemo_shedLoad() |
1250 r.shedLoad() | |
1251 r.eph_galaxy_load() | |
1252 retcode = r.gal_test() | |
1263 r.moveRunOutputs() | 1253 r.moveRunOutputs() |
1264 r.makeToolTar() | 1254 r.makeToolTar() |
1265 print(f"second galaxy_test returned {retcode}") | 1255 print(f"second galaxy_test returned {retcode}") |
1266 | 1256 |
1267 | 1257 |