diff build_ctb_gene.py @ 37:8f16164019bd draft

planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc.git commit 3cb86dfb85e313a464efbce8529442c8fc4630a5
author sanbi-uwc
date Tue, 31 May 2016 03:04:08 -0400
parents 25caec56f66b
children 65df0b64db15
line wrap: on
line diff
--- a/build_ctb_gene.py	Wed May 25 05:37:46 2016 -0400
+++ b/build_ctb_gene.py	Tue May 31 03:04:08 2016 -0400
@@ -10,7 +10,11 @@
 import time
 import random
 from subprocess import check_call, check_output, CalledProcessError
-
+import socket
+try:
+    from urllib.parse import urlparse
+except ImportError:
+    from urlparse import urlparse
 import logging
 
 log = logging.getLogger(__name__)
@@ -22,6 +26,7 @@
         output = check_output(cmd_str, shell=True)
     except CalledProcessError:
         print("Error running get_docker_port by build_ctb_gene", file=sys.stderr)
+        return None
     return output
 
 
@@ -39,16 +44,16 @@
     def build_ctb_gene(self):
         cmdline_str = "build_ctb_gene test {}".format(self.args.input_file)
         cmdline_str = self.newSplit(cmdline_str)
-        build_ctb_run = False
         try:
             check_call(cmdline_str)
-            build_ctb_run = True
         except CalledProcessError:
             print("Error running the build_ctb_gene goterms", file=sys.stderr)
-        if build_ctb_run:
+            return None
+        else:
             self.copy_output_file_to_dataset()
             print("Building a new DB, current time: %s" % str(datetime.date.today()))
             print("GFF File - Input: %s" % str(self.args.input_file))
+            return True
 
     def newSplit(self, value):
         lex = shlex.shlex(value)
@@ -79,9 +84,16 @@
             check_call(stop_cmd_str)
         except CalledProcessError:
             print("Error running docker stop build_ctb_gene", file=sys.stderr)
+            return None
+        else:
+            return True
 
     def docker_run(self):
         self.mount_point = "{}/neo4j/data".format(os.getcwd())
+        try:
+            os.makedirs(self.mount_point)
+        except os.error as e:
+            print("Error creating mount point {mount_point}: {error}".format(mount_point=self.mount_point, error=e.strerror))
 
         cmd_str = "docker run -d -P -v {}:/data -e NEO4J_AUTH=none --name {} thoba/neo4j_galaxy_ie".format(
             self.mount_point, self.docker_instance_name)
@@ -107,8 +119,12 @@
     cmd_str = "docker inspect --format='{{(index (index .NetworkSettings.Ports \"7474/tcp\") 0).HostPort}}' %s" % ctb_gene_runner.docker_instance_name
 
     # TODO: randomise the ports/names/mount_point and use the auto kill image
-    neo4j_url = 'http://localhost:{}/db/data/'.format(
-              inspect_docker(cmd_str)[:-1])
+    neo4j_container_info = inspect_docker(cmd_str)
+    if neo4j_container_info is None:
+        exit(1)
+    else:
+        neo4j_port = neo4j_container_info[:-1]
+    neo4j_url = 'http://localhost:{}/db/data/'.format(neo4j_port)
     try:
         os.environ["NEO4J_REST_URL"] = neo4j_url
     except (OSError, ValueError), e:
@@ -118,8 +134,29 @@
     if not os.path.exists(args.outputdir):
         os.makedirs(args.outputdir)
 
-    time.sleep(60)
-    ctb_gene_runner.build_ctb_gene()
+    url = urlparse(neo4j_url)
+    if '@' in url.netloc:
+        (host, port) = url.netloc.split('@')[1].split(':')
+    else:
+        (host, port) = url.netloc.split(':')
+    timeout = int(os.environ.get('NEO4J_WAIT_TIMEOUT', 30)) # time to wait till neo4j
+    connected = False
+    #print('host, port', host, port)
+    while timeout > 0:
+        try:
+            socket.create_connection((host, port), 1)
+        except socket.error:
+            timeout -= 1
+            time.sleep(1)
+        else:
+            connected = True
+            break
+    if not connected:
+        sys.exit('timed out trying to connect to {}'.format(neo4j_url))        
+
+    status = ctb_gene_runner.build_ctb_gene()
+    if status is None:
+        exit(1)
 
 
 if __name__ == "__main__": main()