This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NBsvnCommand.py
300 lines (262 loc) · 10.2 KB
/
NBsvnCommand.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#! /usr/bin/env python
#------------------------------------------------------------------------
# This file is distributed under the Common Public License.
# It is part of the TestTools project in COIN-OR (www.coin-or.org)
#------------------------------------------------------------------------
import os
import urllib2
import re
import sys
import time
import NBlogMessages
import NBemail
import NBosCommand
import NBcommandline
execfile('NBuserParametersDefault.py')
execfile(NBcommandline.getParameterFile())
#------------------------------------------------------------------------
# Function for executing svn commands
# svnCmd: String representing svn command
# dir: Directory where command is to be run from
# project: Coin project running the command (this is used to provide
# a better message if an error is detected
# return: the result of the NBosCommand call;
# return['returnCode'] is the return code of svn
#------------------------------------------------------------------------
def run(svnCmd,dir,project) :
if dir != '.' :
os.chdir(dir)
# NBlogMessages.writeMessage(' cd '+dir)
NBlogMessages.writeMessage(' '+svnCmd)
# result = NBosCommand.run(svnCmd)
# sometimes it is necessary to run more than once
for i in range(1, SVN_UPDATE_TRIALS + 1):
result = NBosCommand.run(svnCmd)
if result['returnCode'] == 0 : return result
time.sleep(SVN_UPDATE_SLEEP_TIME)
# if we are here something went wrong
# we do not send a mail if svn add reported "has binary mime type property"
# this is a stupid work around that works only if svn add was supposed to add only one binary file
# this binary file usually still gets added
if result['returnCode'] != 0 and result['stderr'].find('has binary mime type property')<0:
NBemail.sendCmdMsgs(project,result,svnCmd)
return result
#------------------------------------------------------------------------
# Function which returns the latest stable or release version of a project
# sr: must be 'stable' or 'releases'
#------------------------------------------------------------------------
def latestVersion(project,sr) :
chromeFailed=False
errMsg=""
if sr == "stable" :
reexp1=r'(\d+\.?\d+)'
reexp2=r'<li><a href="(\d+\.?\d+)/">(\d+\.?\d+)/</a></li>'
chromeFile=sr
else :
reexp1=r'(\d+\.?\d+\.?\d+)'
reexp2=r'<li><a href="(\d+\.?\d+\.?\d+)/">(\d+\.?\d+\.?\d+)/</a></li>'
chromeFile='release'
url='https://projects.coin-or.org/'+project+'/chrome/site/'+project+'-latest-'+chromeFile+'.txt'
if project == "CoinAll":
url='https://projects.coin-or.org/CoinBinary/chrome/site/CoinAll-latest-'+chromeFile+'.txt'
try :
handle=urllib2.urlopen(url)
latestVersion=handle.read()
handle.close()
except urllib2.URLError, why:
errMsg=' Warning: URLError exception caught while retrieving '+url+': '+str(why)+'. '+errMsg+'\n'
chromeFailed=True
except httplib.HTTPException, why:
errMsg=' Warning: HTTPException caught while retrieving '+url+': '+str(why)+'. '+errMsg+'\n'
chromeFailed=True
if not chromeFailed :
latestVersion=re.findall(reexp1,latestVersion)
if len(latestVersion) == 1:
return latestVersion[0]
else :
chromeFailed=True
errMsg=' Warning: Retrieving latest stable version number from '+url+' failed. Got '+str(latestVersion)+'. '+errMsg
# Accessing the chrome file did not work, so use the older method.
# The chrome method does not work for FlopC++
url='https://projects.coin-or.org/svn/'+project+'/'+sr
if project == "CoinAll":
url = 'https://projects.coin-or.org/svn/'+'CoinBinary/'+project+'/'+sr
try :
handle=urllib2.urlopen(url)
html=handle.read()
handle.close()
except urllib2.URLError, why:
errMsg=' Warning: URLError exception caught while retrieving '+url+': '+str(why)+'. '+errMsg+'\n'
NBlogMessages.writeMessage(errMsg)
return False
except httplib.HTTPException, why:
errMsg=' Warning: HTTPException caught while retrieving '+url+': '+str(why)+'. '+errMsg+'\n'
NBlogMessages.writeMessage(errMsg)
return False
# In html code find the latest version number
findResult=re.findall(reexp2,html)
if len(findResult)==0: return False
latestVersionRepeated2Times = findResult[-1:][0]
latestVersion=latestVersionRepeated2Times[0]
return latestVersion
#------------------------------------------------------------------------
# Function which returns the latest stable version of a project
#------------------------------------------------------------------------
def latestStableVersion(project) :
return latestVersion(project,'stable')
#------------------------------------------------------------------------
# Function which returns the latest release version of a project
# If there isn't a release version then False is returned
#------------------------------------------------------------------------
def latestReleaseVersion(project) :
return latestVersion(project,'releases')
#------------------------------------------------------------------------
# Return svn revision number from url
# If not found the return -1
#------------------------------------------------------------------------
def svnRevision(url) :
retVal=-1
result = NBosCommand.run('svn info --xml '+svnDir(url))
if result['returnCode']==0 :
# reg=r'Last Changed Rev: (\d+)'
reg=r'<commit\s*revision=\"(\d+)\"'
found=re.findall(reg,result['stdout'])
if len(found)!=0 :
retVal=int(found[0])
return retVal
#------------------------------------------------------------------------
# Return svn url corresponding to given directory
# If not found the return -1
#------------------------------------------------------------------------
def svnUrl(dir) :
retVal='error in NBsvnCommand.svnUrl'
svnCommandText='svn info --xml '+svnDir(dir)
retVal += ': '+svnCommandText
result = NBosCommand.run(svnCommandText)
if result['returnCode']==0 :
reg=r'<url>(.+)</url>'
found=re.findall(reg,result['stdout'])
if len(found)!=0 :
retVal=found[0]
return retVal
#------------------------------------------------------------------------
# newer(source, target)
# Return true if source and is more recently modified than target,
# (ie return true if source is newer than target and target needs
# to be rebuilt).
#
# If either sourrce or target don't exist then true is returned.
#
#------------------------------------------------------------------------
def newer(source,target) :
#print '------------------------'
#print source
#print target
#print '------------------------'
#print ' '
tarRev=svnRevision(target)
if tarRev==-1 :
# Target probably does not exist. It does not have an svn revision
# nubmer, so return that it is out of date.
return "Does not exist: "+target
srcRev=svnRevision(source)
if srcRev==-1 :
# Source should exist. Something is wrong that will be caught
# when an 'svn checkout' is done.
return "Does not exist: "+source
if srcRev>tarRev :
return "New revision of: "+source
# if there is an externals file then process it
extFileName=os.path.join(target,"Externals")
if os.path.isfile(extFileName) :
extFilePtr = open(extFileName, "r")
line = extFilePtr.readline()
while line:
line=line.strip()
if line!='' :
if line[0]!='#':
reg=r'(\S+)(\s+)(\S+)'
found=re.findall(reg,line)
if len(found)!=1:
# something is wrong. Do a rebuild
return "Assumed out of date when reading: "+extFileName
found=found[0]
if len(found)!=3 :
# something is wrong. Do a rebuild
return "Assumed out of date when reading: "+extFileName
extTarget=os.path.join(target,found[0])
extSource=found[2]
# Recursive call to see if external indicates rebuild
msg=newer(extSource,extTarget)
if msg :
extFilePtr.close()
return msg
line = extFilePtr.readline()
extFilePtr.close()
return False
#------------------------------------------------------------------------
# svnRevisionNumbers(target)
# Return svn version and version of all referenced externals
#
# target is the full path of the directory containing svn checkedout project
#------------------------------------------------------------------------
def svnRevisionNumbers(target) :
retVal={}
#stripTarget=target.strip()
svnRevisions(target,retVal)
return retVal
def svnRevisions(relPath,revisions) :
#print "svnRevisions:"
#print " relPath="+relPath
#print revisions
stripRelPath=relPath.strip()
#path=os.path.join(basePath,stripRelPath)
#os.chdir(path)
rev=svnRevision(stripRelPath)
if rev==-1 : rev = "Error getting svn revision"
url=svnUrl(stripRelPath)
revisions[stripRelPath]=url+" "+str(rev)
# get externals
#print ' url="'+url+'"'
#print ' relPath="'+stripRelPath+'"'
#print " "
result = NBosCommand.run('svn propget svn:externals '+svnDir(url))
if result['returnCode']!=0 :
print 'error getting external property'
print 'url="'+url+'"'
print 'result='
print result
return
externals = result['stdout']
for external in externals.split('\n'):
if external.strip()=="" : continue
#print "external='"+external+"'"
p=external.split()
#print p
path=os.path.join(stripRelPath,p[0])
#print path
svnRevisions(path,revisions)
return
#------------------------------------------------------------------------
# Function for manipulating directory string to a form needed by svn
# dir: String representing directory name
# return: directory in form needed by svn
#
# On cygwin a directory might be in the form
# /cygdrive/c/COIN/clp/trunk
# For some unfortunate reason svn wants the directory to be
# c:/COIN/clp/trunk
#------------------------------------------------------------------------
def svnDir(dir) :
retVal=dir
if not dir.startswith("/cygdrive/"): return retVal
# is the 11th character not a /
if dir.find("/",11) != 11 : return retVal
retVal=dir[10:11]+":/"+dir[12:]
return retVal
#r=svnRevisionNumbers(r'F:\COIN\nightlyBuild\buildDir\OS\trunk')
#for cod,rev in r.items():
# print " checkout directory: "+cod
# print " svn url: "+rev.split()[0]
# print " svn revision number: "+rev.split()[1]
# print " "