annotate xena_utils.py @ 34:a3fbe077a14c

replace wget with python method
author jingchunzhu <jingchunzhu@gmail.com>
date Thu, 23 Jul 2015 01:02:24 -0700
parents 8bb037f88ed2
children d64a002c3b0c
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")
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
44 """
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
45 query = "wget -q -O- http://%s:%s/data/'(+ 1 2)'" % (hostname, xenaPort)
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
46 try:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
47 result = subprocess.check_output(query, shell=True)
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 return(result == "3.0")
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
52 """
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
53
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
54 def findUnusedPort():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
55 """Find a random port that is available on the local system, and return
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
56 the port number.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
57 """
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
58 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
59 ss.bind(('', 0))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
60 portNumber = ss.getsockname()[1]
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
61 ss.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
62 return(portNumber)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
63
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
64 def isPortAvailable(port):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
65 """Test if a given port is available"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
66 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
67 try:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
68 ss.bind(('', port))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
69 except:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
70 return False
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
71 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
72 ss.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
73 return True
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
74
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
75 def portFilename():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
76 """ Return the name of the file with the port of the running Xena,
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
77 if any
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
78 """
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
79 xenaBaseDir = os.getenv('XENA_BASE_DIR', "~")
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
80 xenaPortFilename = xenaBaseDir + "/xena.port"
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
81 return(xenaPortFilename)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
82
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
83 def port(testIfAvailable=False, findNewPort=False):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
84 preferredXenaPort = 7220
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
85 xenaPort = None
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
86 xenaPortFname = portFilename()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
87 if os.path.exists(xenaPortFname):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
88 fp = open(xenaPortFname)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
89 line = fp.readline()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
90 xenaPort = int(line.rstrip())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
91 if testIfAvailable and not isRunning(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
92 # Xena is not running on the port. Make sure that
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
93 # the port is not occupied by some other process
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
94 if not isPortAvailable(xenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
95 #cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
96 #pid = subprocess.check_output(cmd, shell=True).rstrip()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
97 #print "not available, used by",pid
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
98 xenaPort = None
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 0
diff changeset
99
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
100 if findNewPort and xenaPort == None:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
101 if isPortAvailable(preferredXenaPort):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
102 xenaPort = preferredXenaPort
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
103 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
104 xenaPort = findUnusedPort()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
105 fp = open(portFilename(), "w")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
106 fp.write("%d\n" % xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
107 fp.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
108 return(xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
109
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
110 def cleanUpPort():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
111 """ Clean up the port file after Xena has stopped running"""
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
112 os.unlink(portFilename())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
113
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
114
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
115