Mercurial > repos > melissacline > ucsc_xena_platform
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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 xenaUtils: a set of python utilities for the Galaxy / Xena interface | |
4 """ | |
5 | |
6 import os | |
7 import socket | |
8 import subprocess | |
34
a3fbe077a14c
replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents:
0
diff
changeset
|
9 import httplib |
0 | 10 |
11 def jarPath(): | |
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 | 14 return(os.path.join(jarPath, "xena.jar")) |
15 | |
16 | |
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 | 19 |
20 def fileDir(): | |
21 return(baseDir() + "/files") | |
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 | 27 def isRunning(xenaPort): |
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 | 44 |
45 def findUnusedPort(): | |
46 """Find a random port that is available on the local system, and return | |
47 the port number. | |
48 """ | |
49 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
50 ss.bind(('', 0)) | |
51 portNumber = ss.getsockname()[1] | |
52 ss.close() | |
53 return(portNumber) | |
54 | |
55 def isPortAvailable(port): | |
56 """Test if a given port is available""" | |
57 ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
58 try: | |
59 ss.bind(('', port)) | |
60 except: | |
61 return False | |
62 else: | |
63 ss.close() | |
64 return True | |
65 | |
66 def portFilename(): | |
67 """ Return the name of the file with the port of the running Xena, | |
68 if any | |
69 """ | |
34
a3fbe077a14c
replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents:
0
diff
changeset
|
70 xenaBaseDir = os.getenv('XENA_BASE_DIR', "~") |
0 | 71 xenaPortFilename = xenaBaseDir + "/xena.port" |
72 return(xenaPortFilename) | |
73 | |
74 def port(testIfAvailable=False, findNewPort=False): | |
75 preferredXenaPort = 7220 | |
76 xenaPort = None | |
77 xenaPortFname = portFilename() | |
78 if os.path.exists(xenaPortFname): | |
79 fp = open(xenaPortFname) | |
80 line = fp.readline() | |
81 xenaPort = int(line.rstrip()) | |
82 if testIfAvailable and not isRunning(xenaPort): | |
83 # Xena is not running on the port. Make sure that | |
84 # the port is not occupied by some other process | |
85 if not isPortAvailable(xenaPort): | |
86 #cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID | |
87 #pid = subprocess.check_output(cmd, shell=True).rstrip() | |
88 #print "not available, used by",pid | |
89 xenaPort = None | |
34
a3fbe077a14c
replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents:
0
diff
changeset
|
90 |
0 | 91 if findNewPort and xenaPort == None: |
92 if isPortAvailable(preferredXenaPort): | |
93 xenaPort = preferredXenaPort | |
94 else: | |
95 xenaPort = findUnusedPort() | |
96 fp = open(portFilename(), "w") | |
97 fp.write("%d\n" % xenaPort) | |
98 fp.close() | |
99 return(xenaPort) | |
100 | |
101 def cleanUpPort(): | |
102 """ Clean up the port file after Xena has stopped running""" | |
103 os.unlink(portFilename()) | |
104 | |
105 | |
106 |