annotate xena_utils.py @ 55:421b18a0b659 default tip

update v17 step 2, add xena.jar
author jingchunzhu
date Tue, 22 Sep 2015 10:07:51 -0700
parents d64a002c3b0c
children
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
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
9 import httplib
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
10
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
11 def jarPath():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
12 """Return the full pathname of the xena jar file"""
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
13 jarPath = os.getenv('XENA_JAR_PATH', "~")
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
14 return(os.path.join(jarPath, "xena.jar"))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
15
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
16
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
17 def baseDir():
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
18 return(os.getenv('XENA_BASE_DIR', "/tmp"))
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
19
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
20 def fileDir():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
21 return(baseDir() + "/files")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
22
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
23 def hostname():
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
24 hostname = subprocess.check_output("hostname -f", shell=True).rstrip()
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
25 return hostname
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
26
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
27 def isRunning(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
28 """Determine if Xena is running on the specified port"""
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
29
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
30 host = hostname()
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
31 try:
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
32 httpServ = httplib.HTTPConnection(host, xenaPort)
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
33 httpServ.connect()
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
35 data = "(+ 1 2)"
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
36 httpServ.request('POST', '/data/', data)
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
37 response = httpServ.getresponse()
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
38 if response.status == httplib.OK:
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
39 content = response.read()
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
40 except:
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
41 return False
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
42
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
43 return (content == "3.0")
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
44
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
45 def findUnusedPort():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
46 """Find a random port that is available on the local system, and return
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
47 the port number.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
48 """
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
49 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
50 ss.bind(('', 0))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
51 portNumber = ss.getsockname()[1]
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
52 ss.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
53 return(portNumber)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
54
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
55 def isPortAvailable(port):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
56 """Test if a given port is available"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
57 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
58 try:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
59 ss.bind(('', port))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
60 except:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
61 return False
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
62 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
63 ss.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
64 return True
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
65
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
66 def portFilename():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
67 """ Return the name of the file with the port of the running Xena,
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
68 if any
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
69 """
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
70 xenaBaseDir = os.getenv('XENA_BASE_DIR', "~")
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
71 xenaPortFilename = xenaBaseDir + "/xena.port"
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
72 return(xenaPortFilename)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
73
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
74 def port(testIfAvailable=False, findNewPort=False):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
75 preferredXenaPort = 7220
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
76 xenaPort = None
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
77 xenaPortFname = portFilename()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
78 if os.path.exists(xenaPortFname):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
79 fp = open(xenaPortFname)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
80 line = fp.readline()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
81 xenaPort = int(line.rstrip())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
82 if testIfAvailable and not isRunning(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
83 # Xena is not running on the port. Make sure that
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
84 # the port is not occupied by some other process
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
85 if not isPortAvailable(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
86 #cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
87 #pid = subprocess.check_output(cmd, shell=True).rstrip()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
88 #print "not available, used by",pid
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
89 xenaPort = None
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
90
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
91 if findNewPort and xenaPort == None:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
92 if isPortAvailable(preferredXenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
93 xenaPort = preferredXenaPort
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
94 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
95 xenaPort = findUnusedPort()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
96 fp = open(portFilename(), "w")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
97 fp.write("%d\n" % xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
98 fp.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
99 return(xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
100
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
101 def cleanUpPort():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
102 """ Clean up the port file after Xena has stopped running"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
103 os.unlink(portFilename())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
104
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
105
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
106