/
utils.py
169 lines (140 loc) · 4.83 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import re
import socket
import time
import os
import sys
# various utilities that are handy
def getAllSourceFiles( arr=None , prefix="." ):
if arr is None:
arr = []
for x in os.listdir( prefix ):
if x.startswith( "." ) or x.startswith( "pcre-" ) or x.startswith( "32bit" ) or x.startswith( "mongodb-" ) or x.startswith("debian") or x.startswith( "mongo-cxx-driver" ):
continue
full = prefix + "/" + x
if os.path.isdir( full ) and not os.path.islink( full ):
getAllSourceFiles( arr , full )
else:
if full.endswith( ".cpp" ) or full.endswith( ".h" ) or full.endswith( ".c" ):
arr.append( full )
return arr
def getGitBranch():
if not os.path.exists( ".git" ):
return None
version = open( ".git/HEAD" ,'r' ).read().strip()
if not version.startswith( "ref: " ):
return version
version = version.split( "/" )
version = version[len(version)-1]
return version
def getGitBranchString( prefix="" , postfix="" ):
t = re.compile( '[/\\\]' ).split( os.getcwd() )
if len(t) > 2 and t[len(t)-1] == "mongo":
par = t[len(t)-2]
m = re.compile( ".*_([vV]\d+\.\d+)$" ).match( par )
if m is not None:
return prefix + m.group(1).lower() + postfix
if par.find("Nightly") > 0:
return ""
b = getGitBranch()
if b == None or b == "master":
return ""
return prefix + b + postfix
def getGitVersion():
if not os.path.exists( ".git" ):
return "nogitversion"
version = open( ".git/HEAD" ,'r' ).read().strip()
if not version.startswith( "ref: " ):
return version
version = version[5:]
f = ".git/" + version
if not os.path.exists( f ):
return version
return open( f , 'r' ).read().strip()
def execsys( args ):
import subprocess
if isinstance( args , str ):
r = re.compile( "\s+" )
args = r.split( args )
p = subprocess.Popen( args , stdout=subprocess.PIPE , stderr=subprocess.PIPE )
r = p.communicate()
return r;
def getprocesslist():
raw = ""
try:
raw = execsys( "/bin/ps -ax" )[0]
except Exception,e:
print( "can't get processlist: " + str( e ) )
r = re.compile( "[\r\n]+" )
return r.split( raw )
def removeIfInList( lst , thing ):
if thing in lst:
lst.remove( thing )
def findVersion( root , choices ):
for c in choices:
if ( os.path.exists( root + c ) ):
return root + c
raise "can't find a version of [" + root + "] choices: " + choices
def choosePathExist( choices , default=None):
for c in choices:
if c != None and os.path.exists( c ):
return c
return default
def filterExists(paths):
return filter(os.path.exists, paths)
def ensureDir( name ):
d = os.path.dirname( name )
if not os.path.exists( d ):
print( "Creating dir: " + name );
os.makedirs( d )
if not os.path.exists( d ):
raise "Failed to create dir: " + name
def distinctAsString( arr ):
s = set()
for x in arr:
s.add( str(x) )
return list(s)
def checkMongoPort( port=27017 ):
sock = socket.socket()
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.settimeout(1)
sock.connect(("localhost", port))
sock.close()
def didMongodStart( port=27017 , timeout=20 ):
while timeout > 0:
time.sleep( 1 )
try:
checkMongoPort( port )
return True
except Exception,e:
print( e )
timeout = timeout - 1
return False
def smoke_python_name():
# if this script is being run by py2.5 or greater,
# then we assume that "python" points to a 2.5 or
# greater python VM. otherwise, explicitly use 2.5
# which we assume to be installed.
min_version_tuple = (2, 5)
try:
if sys.version_info >= min_version_tuple:
return sys.executable
except AttributeError:
# In case the version of Python is somehow missing sys.version_info or sys.executable.
pass
import subprocess
version = re.compile(r'[Pp]ython ([\d\.]+)', re.MULTILINE)
binaries = ['python2.5', 'python2.6', 'python2.7', 'python25', 'python26', 'python27', 'python']
for binary in binaries:
try:
# py-2.4 compatible replacement for shell backticks
out, err = subprocess.Popen([binary, '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
for stream in (out, err):
match = version.search(stream)
if match:
versiontuple = tuple(map(int, match.group(1).split('.')))
if versiontuple >= min_version_tuple:
return binary
except:
pass
# if that all fails, fall back to "python"
return "python"