annotate xena_utils.py @ 26:8666fd3e8e4f

Uploaded, to try to fix a merge problem.
author melissacline
date Tue, 02 Jun 2015 19:18:14 -0400
parents 8bb037f88ed2
children a3fbe077a14c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
1 #!/usr/bin/env python
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
2 """
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
3 xenaUtils: a set of python utilities for the Galaxy / Xena interface
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
4 """
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
5
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
6 import os
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
7 import socket
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
8 import subprocess
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
9
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
10 def jarPath():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
11 """Return the full pathname of the xena jar file"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
12 jarPath = os.getenv("XENA_JAR_PATH", "~")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
13 return(os.path.join(jarPath, "xena.jar"))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
14
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
15
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
16 def baseDir():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
17 return(os.getenv("XENA_BASE_DIR", "/tmp"))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
18
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
19 def fileDir():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
20 return(baseDir() + "/files")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
21
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
22 def isRunning(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
23 """Determine if Xena is running on the specified port"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
24 query = "wget -q -O- http://localhost:%s/data/'(+ 1 2)'" % xenaPort
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
25 try:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
26 result = subprocess.check_output(query, shell=True)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
27 except:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
28 return False
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
29 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
30 return(result == "3.0")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
31
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
32
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
33 def findUnusedPort():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
34 """Find a random port that is available on the local system, and return
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
35 the port number.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
36 """
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
37 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
38 ss.bind(('', 0))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
39 portNumber = ss.getsockname()[1]
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
40 ss.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
41 return(portNumber)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
42
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
43 def isPortAvailable(port):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
44 """Test if a given port is available"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
45 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
46 try:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
47 ss.bind(('', port))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
48 except:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
49 return False
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
50 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
51 ss.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
52 return True
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
53
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
54
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
55 def portFilename():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
56 """ Return the name of the file with the port of the running Xena,
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
57 if any
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
58 """
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
59 xenaBaseDir = os.getenv("XENA_BASE_DIR", "~")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
60 xenaPortFilename = xenaBaseDir + "/xena.port"
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
61 return(xenaPortFilename)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
62
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
63
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
64
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
65
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
66 def port(testIfAvailable=False, findNewPort=False):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
67 preferredXenaPort = 7220
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
68 xenaPort = None
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
69 xenaPortFname = portFilename()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
70 if os.path.exists(xenaPortFname):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
71 fp = open(xenaPortFname)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
72 line = fp.readline()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
73 xenaPort = int(line.rstrip())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
74 if testIfAvailable and not isRunning(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
75 # Xena is not running on the port. Make sure that
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
76 # the port is not occupied by some other process
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
77 if not isPortAvailable(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
78 #cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
79 #pid = subprocess.check_output(cmd, shell=True).rstrip()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
80 #print "not available, used by",pid
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
81 xenaPort = None
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
82 if findNewPort and xenaPort == None:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
83 if isPortAvailable(preferredXenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
84 xenaPort = preferredXenaPort
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
85 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
86 xenaPort = findUnusedPort()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
87 fp = open(portFilename(), "w")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
88 fp.write("%d\n" % xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
89 fp.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
90 return(xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
91
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
92 def cleanUpPort():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
93 """ Clean up the port file after Xena has stopped running"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
94 os.unlink(portFilename())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
95
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
96
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
97