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