comparison 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
comparison
equal deleted inserted replaced
33:7ceb967147c3 34:a3fbe077a14c
4 """ 4 """
5 5
6 import os 6 import os
7 import socket 7 import socket
8 import subprocess 8 import subprocess
9 import httplib
9 10
10 def jarPath(): 11 def jarPath():
11 """Return the full pathname of the xena jar file""" 12 """Return the full pathname of the xena jar file"""
12 jarPath = os.getenv("XENA_JAR_PATH", "~") 13 jarPath = os.getenv('XENA_JAR_PATH', "~")
13 return(os.path.join(jarPath, "xena.jar")) 14 return(os.path.join(jarPath, "xena.jar"))
14 15
15 16
16 def baseDir(): 17 def baseDir():
17 return(os.getenv("XENA_BASE_DIR", "/tmp")) 18 return(os.getenv('XENA_BASE_DIR', "/tmp"))
18 19
19 def fileDir(): 20 def fileDir():
20 return(baseDir() + "/files") 21 return(baseDir() + "/files")
21 22
23 def hostname():
24 hostname = subprocess.check_output("hostname -f", shell=True).rstrip()
25 return hostname
26
22 def isRunning(xenaPort): 27 def isRunning(xenaPort):
23 """Determine if Xena is running on the specified port""" 28 """Determine if Xena is running on the specified port"""
24 query = "wget -q -O- http://localhost:%s/data/'(+ 1 2)'" % xenaPort 29
30 host = hostname()
31 try:
32 httpServ = httplib.HTTPConnection(host, xenaPort)
33 httpServ.connect()
34
35 data = "(+ 1 2)"
36 httpServ.request('POST', '/data/', data)
37 response = httpServ.getresponse()
38 if response.status == httplib.OK:
39 content = response.read()
40 except:
41 return False
42
43 return (content == "3.0")
44 """
45 query = "wget -q -O- http://%s:%s/data/'(+ 1 2)'" % (hostname, xenaPort)
25 try: 46 try:
26 result = subprocess.check_output(query, shell=True) 47 result = subprocess.check_output(query, shell=True)
27 except: 48 except:
28 return False 49 return False
29 else: 50 else:
30 return(result == "3.0") 51 return(result == "3.0")
31 52 """
32 53
33 def findUnusedPort(): 54 def findUnusedPort():
34 """Find a random port that is available on the local system, and return 55 """Find a random port that is available on the local system, and return
35 the port number. 56 the port number.
36 """ 57 """
49 return False 70 return False
50 else: 71 else:
51 ss.close() 72 ss.close()
52 return True 73 return True
53 74
54
55 def portFilename(): 75 def portFilename():
56 """ Return the name of the file with the port of the running Xena, 76 """ Return the name of the file with the port of the running Xena,
57 if any 77 if any
58 """ 78 """
59 xenaBaseDir = os.getenv("XENA_BASE_DIR", "~") 79 xenaBaseDir = os.getenv('XENA_BASE_DIR', "~")
60 xenaPortFilename = xenaBaseDir + "/xena.port" 80 xenaPortFilename = xenaBaseDir + "/xena.port"
61 return(xenaPortFilename) 81 return(xenaPortFilename)
62
63
64
65 82
66 def port(testIfAvailable=False, findNewPort=False): 83 def port(testIfAvailable=False, findNewPort=False):
67 preferredXenaPort = 7220 84 preferredXenaPort = 7220
68 xenaPort = None 85 xenaPort = None
69 xenaPortFname = portFilename() 86 xenaPortFname = portFilename()
77 if not isPortAvailable(xenaPort): 94 if not isPortAvailable(xenaPort):
78 #cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID 95 #cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID
79 #pid = subprocess.check_output(cmd, shell=True).rstrip() 96 #pid = subprocess.check_output(cmd, shell=True).rstrip()
80 #print "not available, used by",pid 97 #print "not available, used by",pid
81 xenaPort = None 98 xenaPort = None
99
82 if findNewPort and xenaPort == None: 100 if findNewPort and xenaPort == None:
83 if isPortAvailable(preferredXenaPort): 101 if isPortAvailable(preferredXenaPort):
84 xenaPort = preferredXenaPort 102 xenaPort = preferredXenaPort
85 else: 103 else:
86 xenaPort = findUnusedPort() 104 xenaPort = findUnusedPort()