diff toolfactory/rgToolFactory2.py @ 38:a30536c100bf draft

Updated history outputs
author fubar
date Wed, 12 Aug 2020 01:43:46 -0400
parents ce2b1f8ea68d
children 2cd6555baf44
line wrap: on
line diff
--- a/toolfactory/rgToolFactory2.py	Mon Aug 10 23:25:51 2020 -0400
+++ b/toolfactory/rgToolFactory2.py	Wed Aug 12 01:43:46 2020 -0400
@@ -1,3 +1,4 @@
+#!/usr/bin/env python
 # rgToolFactory.py
 # see https://github.com/fubar2/toolfactory
 #
@@ -33,40 +34,45 @@
 
 import galaxyxml.tool as gxt
 import galaxyxml.tool.parameters as gxtp
+
 import lxml
 
-foo = lxml.__name__   # fug you, flake8. Say my name! Please accept the PR, Helena!
-
-progname = os.path.split(sys.argv[0])[1]
 myversion = "V2.1 July 2020"
 verbose = True
 debug = True
 toolFactoryURL = "https://github.com/fubar2/toolfactory"
 ourdelim = "~~~"
 
-# --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label~~~$input_help"
+# --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label
+# ~~~$input_help"
 IPATHPOS = 0
 ICLPOS = 1
 IFMTPOS = 2
 ILABPOS = 3
 IHELPOS = 4
 IOCLPOS = 5
+
 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.CL
 ONAMEPOS = 0
 OFMTPOS = 1
 OCLPOS = 2
 OOCLPOS = 3
 
-# --additional_parameters="$i.param_name~~~$i.param_value~~~$i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL"
+# --additional_parameters="$i.param_name~~~$i.param_value~~~
+# $i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL~~~i$.param_CLoverride"
 ANAMEPOS = 0
 AVALPOS = 1
 ALABPOS = 2
 AHELPPOS = 3
 ATYPEPOS = 4
 ACLPOS = 5
-AOCLPOS = 6
+AOVERPOS = 6
+AOCLPOS = 7
 
 
+foo = len(lxml.__version__)
+# fug you, flake8. Say my name!
+
 def timenow():
     """return current time as a string
     """
@@ -108,9 +114,11 @@
     citation_tuples = []
     for citation in citations:
         if citation.startswith("doi"):
-            citation_tuples.append(("doi", citation[len("doi"):].strip()))
+            citation_tuples.append(("doi", citation[len("doi") :].strip()))
         else:
-            citation_tuples.append(("bibtex", citation[len("bibtex"):].strip()))
+            citation_tuples.append(
+                ("bibtex", citation[len("bibtex") :].strip())
+            )
     return citation_tuples
 
 
@@ -144,7 +152,6 @@
         ], 'Parameter passing in args.parampass must be "0","positional" or "argparse"'
         self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name)
         self.tool_id = self.tool_name
-        self.xmlfile = "%s.xml" % self.tool_name
         if self.args.interpreter_name:
             exe = "$runMe"
         else:
@@ -177,25 +184,41 @@
             clsuffix = []
             xclsuffix = []
             for i, p in enumerate(self.infiles):
-                appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS]]
+                if p[IOCLPOS] == "STDIN":
+                    appendme = [
+                        p[IOCLPOS],
+                        p[ICLPOS],
+                        p[IPATHPOS],
+                        "< %s" % p[IPATHPOS],
+                    ]
+                    xappendme = [
+                        p[IOCLPOS],
+                        p[ICLPOS],
+                        p[IPATHPOS],
+                        "< $%s" % p[ICLPOS],
+                    ]
+                else:
+                    appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS], ""]
+                    xappendme = [p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS], ""]
                 clsuffix.append(appendme)
-                xclsuffix.append([p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS]])
+                xclsuffix.append(xappendme)
                 # print('##infile i=%d, appendme=%s' % (i,appendme))
             for i, p in enumerate(self.outfiles):
                 if p[OOCLPOS] == "STDOUT":
                     self.lastclredirect = [">", p[ONAMEPOS]]
                     self.lastxclredirect = [">", "$%s" % p[OCLPOS]]
-                    # print('##outfiles i=%d lastclredirect = %s' % (i,self.lastclredirect))
                 else:
-                    appendme = [p[OOCLPOS], p[OCLPOS], p[ONAMEPOS]]
-                    clsuffix.append(appendme)
-                    xclsuffix.append([p[OOCLPOS], p[OCLPOS], "$%s" % p[ONAMEPOS]])
-                    # print('##outfiles i=%d' % i,'appendme',appendme)
+                    clsuffix.append([p[OOCLPOS], p[OCLPOS], p[ONAMEPOS], ""])
+                    xclsuffix.append(
+                        [p[OOCLPOS], p[OCLPOS], "$%s" % p[ONAMEPOS], ""]
+                    )
             for p in self.addpar:
-                appendme = [p[AOCLPOS], p[ACLPOS], p[AVALPOS]]
-                clsuffix.append(appendme)
-                xclsuffix.append([p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS]])
-                # print('##adpar %d' % i,'appendme=',appendme)
+                clsuffix.append(
+                    [p[AOCLPOS], p[ACLPOS], p[AVALPOS], p[AOVERPOS]]
+                )
+                xclsuffix.append(
+                    [p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS], p[AOVERPOS]]
+                )
             clsuffix.sort()
             xclsuffix.sort()
             self.xclsuffix = xclsuffix
@@ -213,13 +236,17 @@
         assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
         self.script = "\n".join(rx)
         fhandle, self.sfile = tempfile.mkstemp(
-            prefix=self.tool_name, suffix=".%s" % (self.args.interpreter_name)
+            prefix=self.tool_name, suffix="_%s" % (self.args.interpreter_name)
         )
         tscript = open(self.sfile, "w")
         tscript.write(self.script)
         tscript.close()
-        self.indentedScript = "  %s" % "\n".join([" %s" % html_escape(x) for x in rx])
-        self.escapedScript = "%s" % "\n".join([" %s" % html_escape(x) for x in rx])
+        self.indentedScript = "  %s" % "\n".join(
+            [" %s" % html_escape(x) for x in rx]
+        )
+        self.escapedScript = "%s" % "\n".join(
+            [" %s" % html_escape(x) for x in rx]
+        )
         art = "%s.%s" % (self.tool_name, self.args.interpreter_name)
         artifact = open(art, "wb")
         artifact.write(bytes(self.script, "utf8"))
@@ -282,13 +309,13 @@
     def clpositional(self):
         # inputs in order then params
         aCL = self.cl.append
-        for (o_v, k, v) in self.clsuffix:
+        for (o_v, k, v, koverride) in self.clsuffix:
             if " " in v:
                 aCL("%s" % v)
             else:
                 aCL(v)
         aXCL = self.xmlcl.append
-        for (o_v, k, v) in self.xclsuffix:
+        for (o_v, k, v, koverride) in self.xclsuffix:
             aXCL(v)
         if self.lastxclredirect:
             aXCL(self.lastxclredirect[0])
@@ -300,15 +327,19 @@
         aCL = self.cl.append
         aXCL = self.xmlcl.append
         # inputs then params in argparse named form
-        for (o_v, k, v) in self.xclsuffix:
-            if len(k.strip()) == 1:
+        for (o_v, k, v, koverride) in self.xclsuffix:
+            if koverride > "":
+                k = koverride
+            elif len(k.strip()) == 1:
                 k = "-%s" % k
             else:
                 k = "--%s" % k
             aXCL(k)
             aXCL(v)
-        for (o_v, k, v) in self.clsuffix:
-            if len(k.strip()) == 1:
+        for (o_v, k, v, koverride) in self.clsuffix:
+            if koverride > "":
+                k = koverride
+            elif len(k.strip()) == 1:
                 k = "-%s" % k
             else:
                 k = "--%s" % k
@@ -339,7 +370,9 @@
                     aparm.positional = int(oldcl)
                     aparm.command_line_override = "$%s" % newcl
             self.toutputs.append(aparm)
-            tp = gxtp.TestOutput(name=newcl, value="%s_sample" % newcl, format=newfmt)
+            tp = gxtp.TestOutput(
+                name=newcl, value="%s_sample" % newcl, format=newfmt
+            )
             self.testparam.append(tp)
         for p in self.infiles:
             newname = p[ICLPOS]
@@ -363,7 +396,7 @@
             tparm = gxtp.TestParam(name=newname, value="%s_sample" % newname)
             self.testparam.append(tparm)
         for p in self.addpar:
-            newname, newval, newlabel, newhelp, newtype, newcl, oldcl = p
+            newname, newval, newlabel, newhelp, newtype, newcl, override, oldcl = p
             if not len(newlabel) > 0:
                 newlabel = newname
             ndash = self.getNdash(newname)
@@ -434,7 +467,9 @@
         anout.command_line_override = "> $%s" % newname
         anout.positional = self.is_positional
         self.toutputs.append(anout)
-        tp = gxtp.TestOutput(name=newname, value="%s_sample" % newname, format=newfmt)
+        tp = gxtp.TestOutput(
+            name=newname, value="%s_sample" % newname, format=newfmt
+        )
         self.testparam.append(tp)
 
     def makeXML(self):
@@ -445,7 +480,7 @@
         """
         self.tool.command_line_override = self.xmlcl
         if self.args.interpreter_name:
-            self.tool.interpreter = self.interp
+            self.tool.interpreter = self.args.interpreter_name
         if self.args.help_text:
             helptext = open(self.args.help_text, "r").readlines()
             helptext = [html_escape(x) for x in helptext]
@@ -462,7 +497,9 @@
         if self.args.interpreter_name:
             if self.args.interpreter_name == "python":
                 requirements.append(
-                    gxtp.Requirement("package", "python", self.args.interpreter_version)
+                    gxtp.Requirement(
+                        "package", "python", self.args.interpreter_version
+                    )
                 )
             elif self.args.interpreter_name not in ["bash", "sh"]:
                 requirements.append(
@@ -476,14 +513,16 @@
             if self.args.exe_package and self.args.parampass != "system":
                 requirements.append(
                     gxtp.Requirement(
-                        "package", self.args.exe_package, self.args.exe_package_version
+                        "package",
+                        self.args.exe_package,
+                        self.args.exe_package_version,
                     )
                 )
         self.tool.requirements = requirements
         if self.args.parampass == "0":
-            self.doXMLNoparam()
+            self.doNoXMLparam()
         else:
-            self.doXMLParam()
+            self.doXMLparam()
         self.tool.outputs = self.toutputs
         self.tool.inputs = self.tinputs
         if self.args.runmode not in ["Executable", "system"]:
@@ -505,7 +544,7 @@
             "Cite: Creating re-usable tools from scripts doi: 10.1093/bioinformatics/bts573"
         )
         exml = self.tool.export()
-        xf = open(self.xmlfile, "w")
+        xf = open('%s.xml' % self.tool_name, "w")
         xf.write(exml)
         xf.write("\n")
         xf.close()
@@ -520,7 +559,9 @@
         """
         retval = self.run()
         if retval:
-            sys.stderr.write("## Run failed. Cannot build yet. Please fix and retry")
+            sys.stderr.write(
+                "## Run failed. Cannot build yet. Please fix and retry"
+            )
             sys.exit(1)
         tdir = "tfout"
         if not os.path.exists(tdir):
@@ -549,19 +590,32 @@
                 shutil.copyfile(pth, dest)
 
         if os.path.exists(self.tlog) and os.stat(self.tlog).st_size > 0:
-            shutil.copyfile(self.tlog, os.path.join(testdir, "test1_log.txt"))
+            shutil.copyfile(self.tlog, os.path.join(testdir, "test1_log_outfiletxt"))
         if self.args.runmode not in ["Executable", "system"]:
             stname = os.path.join(tdir, "%s" % (self.sfile))
             if not os.path.exists(stname):
                 shutil.copyfile(self.sfile, stname)
-        xtname = os.path.join(tdir, self.xmlfile)
-        if not os.path.exists(xtname):
-            shutil.copyfile(self.xmlfile, xtname)
+        xreal = '%s.xml' % self.tool_name
+        xout = os.path.join(tdir,xreal)
+        shutil.copyfile(xreal, xout)
         tarpath = "toolfactory_%s.tgz" % self.tool_name
         tf = tarfile.open(tarpath, "w:gz")
         tf.add(name=tdir, arcname=self.tool_name)
         tf.close()
         shutil.copyfile(tarpath, self.args.new_tool)
+        shutil.copyfile(xreal,"tool_xml.txt")
+        repdir = "TF_run_report_tempdir"
+        if not os.path.exists(repdir):
+            os.mkdir(repdir)
+        repoutnames = [x[OCLPOS] for x in self.outfiles]
+        with os.scandir('.') as outs:
+            for entry in outs:
+                if entry.name.endswith('.tgz') or not entry.is_file():
+                    continue
+                if entry.name in repoutnames:
+                    shutil.copyfile(entry.name,os.path.join(repdir,entry.name))
+                elif entry.name == "%s.xml" % self.tool_name:
+                    shutil.copyfile(entry.name,os.path.join(repdir,"new_tool_xml"))
         return retval
 
     def run(self):
@@ -577,12 +631,15 @@
         if self.args.parampass != "0":
             ste = open(self.elog, "wb")
             if self.lastclredirect:
-                sto = open(self.lastclredirect[1], "wb")  # is name of an output file
+                sto = open(
+                    self.lastclredirect[1], "wb"
+                )  # is name of an output file
             else:
                 sto = open(self.tlog, "wb")
                 sto.write(
                     bytes(
-                        "## Executing Toolfactory generated command line = %s\n" % scl,
+                        "## Executing Toolfactory generated command line = %s\n"
+                        % scl,
                         "utf8",
                     )
                 )
@@ -654,17 +711,23 @@
         'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the Galaxy configuration file'
         % (args.bad_user, args.bad_user)
     )
-    assert args.tool_name, "## Tool Factory expects a tool name - eg --tool_name=DESeq"
+    assert (
+        args.tool_name
+    ), "## Tool Factory expects a tool name - eg --tool_name=DESeq"
     assert (
         args.interpreter_name or args.exe_package
     ), "## Tool Factory wrapper expects an interpreter or an executable package"
     assert args.exe_package or (
         len(args.script_path) > 0 and os.path.isfile(args.script_path)
     ), "## Tool Factory wrapper expects a script path - eg --script_path=foo.R if no executable"
-    args.input_files = [x.replace('"', "").replace("'", "") for x in args.input_files]
+    args.input_files = [
+        x.replace('"', "").replace("'", "") for x in args.input_files
+    ]
     # remove quotes we need to deal with spaces in CL params
     for i, x in enumerate(args.additional_parameters):
-        args.additional_parameters[i] = args.additional_parameters[i].replace('"', "")
+        args.additional_parameters[i] = args.additional_parameters[i].replace(
+            '"', ""
+        )
     r = ScriptRunner(args)
     if args.make_Tool:
         retcode = r.makeTooltar()