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