From d2cf6eb45a94ec0f14fd03ba21f558ac669d7e5d Mon Sep 17 00:00:00 2001
From: "J.F.J. Laros" <j.f.j.laros@lumc.nl>
Date: Fri, 2 Jan 2015 14:55:57 +0100
Subject: [PATCH] New batch script

---
 doc/testing.rst                    | 30 +++++++++++++
 extras/soap-tools/batchjob.py      | 71 ------------------------------
 extras/soap-tools/run_batch_job.py | 68 ++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+), 71 deletions(-)
 delete mode 100755 extras/soap-tools/batchjob.py
 create mode 100755 extras/soap-tools/run_batch_job.py

diff --git a/doc/testing.rst b/doc/testing.rst
index 99d3b1f3..f18e92ca 100644
--- a/doc/testing.rst
+++ b/doc/testing.rst
@@ -5,6 +5,10 @@
 Testing
 =======
 
+
+Unit tests
+----------
+
 We use `pytest`_ for the unit tests. To run them, just type ``py.test`` from
 the Mutalyzer source directory.
 
@@ -16,4 +20,30 @@ Tests are `run automatically on Travis CI
 GitHub.
 
 
+Testing the web services
+------------------------
+
+To ease testing the web services during development, some simple web service
+client scripts are included in the Mutalyzer source tree::
+
+    $ cd extras/soap-tools
+    $ ./info.py
+    Version: 2.0.5
+    Version parts: 2, 0, 5
+    Release date: 16 Dec 2014
+    Nomenclature version: 2.0
+    Nomenclature version parts: 2, 0
+    Server name: res-muta-app01
+    Contact e-mail: humgen@lumc.nl
+    $
+
+They simply call one of the web service functions and print the result. You
+may have to change the server location defined at the top of these scripts.
+
+.. note:: One of the scripts, ``run_batch_job.py``, provides an easy way to
+          run a batch job from the command line. Some additional notes are
+          available for `running this on a Windows machine
+          <https://gist.github.com/martijnvermaat/73271252775867f91821>`_.
+
+
 .. _pytest: http://pytest.org/
diff --git a/extras/soap-tools/batchjob.py b/extras/soap-tools/batchjob.py
deleted file mode 100755
index de11bc2a..00000000
--- a/extras/soap-tools/batchjob.py
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/usr/bin/env python
-"""
-Submit a batch job to a Mutalyzer installation.
-
-Usage:
-  {command} file [type] [argument]
-
-  file: Batch job input file to upload.
-  type: Optional type of the batch job, choose from NameChecker (default),
-        SyntaxChecker, PositionConverter, SnpConverter.
-  argument: Additional argument. Currently only used if batch_type is
-            PositionConverter, denoting the human genome build.
-
-The file is uploaded to the Mutalyzer SOAP API, which is then polled for
-completeness of the batch job after which the result is retrieved and printed
-to standard output.
-"""
-
-
-from __future__ import unicode_literals
-
-from mutalyzer.util import monkey_patch_suds; monkey_patch_suds()
-
-import sys
-from suds import WebFault
-from suds.client import Client
-import time
-
-from mutalyzer.util import format_usage
-
-
-WSDL_LOCATION = 'http://localhost/mutalyzer/services/?wsdl'
-
-MAX_TRIES = 10
-TRY_WAIT = 5
-
-
-def main(batch_input, batch_type='NameChecker', batch_argument=''):
-    """
-    Submit a batch job, poll for completeness, and retrieve the result.
-    """
-    service = Client(WSDL_LOCATION, cache=None).service
-
-    data = open(batch_input, 'rb').read().encode('base64')
-    result = service.submitBatchJob(data, batch_type, batch_argument)
-
-    job_id = int(result)
-
-    for _ in range(MAX_TRIES):
-        try:
-            result = service.getBatchJob(job_id)
-            break
-        except WebFault:
-            # Note that calling getBatchJob *and* monitorBatchJob is a bit
-            # superfluous, but we like to illustrate the use of both of them
-            # here.
-            result = service.monitorBatchJob(job_id)
-            sys.stderr.write('Waiting... (%d entries left)\n' % int(result))
-            time.sleep(TRY_WAIT)
-    else:
-        sys.stderr.write('No result after trying %d times.\n' % MAX_TRIES)
-        sys.exit(1)
-
-    sys.stdout.write(result.decode('base64'))
-
-
-if __name__ == '__main__':
-    if len(sys.argv) < 2:
-        print format_usage()
-        sys.exit(1)
-    main(*sys.argv[1:])
diff --git a/extras/soap-tools/run_batch_job.py b/extras/soap-tools/run_batch_job.py
new file mode 100755
index 00000000..bda04032
--- /dev/null
+++ b/extras/soap-tools/run_batch_job.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+"""
+Mutalyzer batch job automation.
+
+
+Copyright (c) 2015 Leiden University Medical Center <humgen@lumc.nl>
+Copyright (c) 2015 Jeroen F.J. Laros <j.f.j.laros@lumc.nl>
+
+Licensed under the MIT license, see the LICENSE file.
+"""
+
+
+import argparse
+import time
+
+from suds.client import Client
+
+
+URL = "https://mutalyzer.nl/services/?wsdl"
+job_types = ["NameChecker", "SyntaxChecker", "PositionConverter",
+             "SnpConverter"]
+RETRY_WAIT = 1
+
+
+def run_batchjob(input_handle, output_handle, job_type, build="hg19"):
+    """
+    Run a Mutalyzer batch job.
+
+    :arg stream input_handle: Open readable handle to a batch job file.
+    :arg stream output_handle: Opren writable handle for the results.
+    :arg str job_type: Type of the job.
+    :arg str build: Optional build for the Position Converter.
+    """
+    client = Client(URL, cache=None)
+
+    batchfile = input_handle.read().encode("base64")
+    job_id = client.service.submitBatchJob(batchfile, job_type, build)
+    while client.service.monitorBatchJob(job_id):
+        time.sleep(RETRY_WAIT)
+    result = client.service.getBatchJob(job_id).decode("base64")
+    output_handle.write(result)
+
+
+def main():
+    """
+    Main entry point.
+    """
+    usage = __doc__.split("\n\n\n")
+    parser = argparse.ArgumentParser(
+        formatter_class=argparse.RawDescriptionHelpFormatter,
+        description=usage[0], epilog=usage[1])
+
+    parser.add_argument("-v", action='version', version="0.1")
+    parser.add_argument("input_handle", metavar="INPUT",
+        type=argparse.FileType("r"), help="input file")
+    parser.add_argument("output_handle", metavar="OUTPUT",
+        type=argparse.FileType("w"), help="output file")
+    parser.add_argument("job_type", metavar="TYPE", choices=job_types,
+        help="batch job type ({})".format(", ".join(job_types)))
+    parser.add_argument("-b", dest="build", type=str,
+        help="genome build (only valid for the Position Converter)")
+
+    args = parser.parse_args()
+    run_batchjob(args.input_handle, args.output_handle, args.job_type)
+
+
+if __name__ == "__main__":
+    main()
-- 
GitLab