3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 # Copyright (c) 2009 Dominik Riebeling
12 # All files in this archive are subject to the GNU General Public License.
13 # See the file COPYING in the source tree root for full license agreement.
15 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 # KIND, either express or implied.
19 # Automate building releases for deployment.
20 # Run from any folder to build
22 # - any tag (using the -t option)
23 # - any local folder (using the -p option)
24 # Will build a binary archive (tar.bz2 / zip) and source archive.
25 # The source archive won't be built for local builds. Trunk and
26 # tag builds will retrieve the sources directly from svn and build
27 # below the systems temporary folder.
29 # If the required Qt installation isn't in PATH use --qmake option.
30 # Tested on Linux and MinGW / W32
32 # requires python which package (http://code.google.com/p/which/)
33 # requires pysvn package.
34 # requires upx.exe in PATH on Windows.
49 # modules that are not part of python itself.
53 print "Fatal: This script requires the pysvn package to run."
54 print " See http://pysvn.tigris.org/."
59 print "Fatal: This script requires the which package to run."
60 print " See http://code.google.com/p/which/."
64 # Windows nees some special treatment. Differentiate between program name
65 # and executable filename.
67 project
= "rbutil/rbutilqt/rbutilqt.pro"
68 if sys
.platform
== "win32":
69 progexe
= "Release/rbutilqt.exe"
75 programfiles
= [ progexe
]
77 svnserver
= "svn://svn.rockbox.org/rockbox/"
78 # Paths and files to retrieve from svn when creating a tarball.
79 # This is a mixed list, holding both paths and filenames.
80 svnpaths
= [ "rbutil/",
83 "apps/codecs/libspeex",
88 "tools/VOICE_PAUSE.wav",
96 "tools/sapi_voice.vbs" ]
100 print "Usage: %s [options]" % myself
101 print " -q, --qmake=<qmake> path to qmake"
102 print " -p, --project=<pro> path to .pro file for building with local tree"
103 print " -t, --tag=<tag> use specified tag from svn"
104 print " -a, --add=<file> add file to build folder before building"
105 print " -s, --source-only only create source archive"
106 print " -b, --binary-only only create binary archive"
107 print " -d, --dynamic link dynamically instead of static"
108 print " -h, --help this help"
109 print " If neither a project file nor tag is specified trunk will get downloaded"
112 def getsources(svnsrv
, filelist
, dest
):
113 '''Get the files listed in filelist from svnsrv and put it at dest.'''
114 client
= pysvn
.Client()
115 print "Checking out sources from %s, please wait." % svnsrv
117 for elem
in filelist
:
118 url
= re
.subn('/$', '', svnsrv
+ elem
)[0]
119 destpath
= re
.subn('/$', '', dest
+ elem
)[0]
121 client
.export(url
, destpath
)
123 print "SVN client error: %s" % sys
.exc_value
125 print "Checkout finished."
129 def gettrunkrev(svnsrv
):
130 '''Get the revision of trunk for svnsrv'''
131 client
= pysvn
.Client()
132 entries
= client
.info2(svnsrv
, recurse
=False)
133 return entries
[0][1].rev
.number
136 def findversion(versionfile
):
137 '''figure most recent program version from version.h,
138 returns version string.'''
139 h
= open(versionfile
, "r")
142 r
= re
.compile("#define +VERSION +\"(.[0-9\.a-z]+)\"")
144 s
= re
.compile("\$Revision: +([0-9]+)")
147 print "WARNING: Revision not found!"
152 '''Search for Qt4 installation. Return path to qmake.'''
153 print "Searching for Qt"
154 bins
= ["qmake", "qmake-qt4"]
157 q
= which
.which(binary
)
168 def checkqt(qmakebin
):
169 '''Check if given path to qmake exists and is a suitable version.'''
171 # check if binary exists
172 if not os
.path
.exists(qmakebin
):
173 print "Specified qmake path does not exist!"
176 output
= subprocess
.Popen([qmakebin
, "-version"], stdout
=subprocess
.PIPE
,
177 stderr
=subprocess
.PIPE
)
178 cmdout
= output
.communicate()
179 # don't check the qmake return code here, Qt3 doesn't return 0 on -version.
181 r
= re
.compile("Qt[^0-9]+([0-9\.]+[a-z]*)")
184 print "Qt found: %s" % m
.group(1)
185 s
= re
.compile("4\..*")
186 n
= re
.search(s
, m
.group(1))
192 def qmake(qmake
="qmake", projfile
=project
, wd
=".", static
=True):
193 print "Running qmake in %s..." % wd
194 command
= [qmake
, "-config", "release", "-config", "noccache"]
196 command
.append("-config")
197 command
.append("static")
198 command
.append(projfile
)
199 output
= subprocess
.Popen(command
, stdout
=subprocess
.PIPE
, cwd
=wd
)
201 if not output
.returncode
== 0:
202 print "qmake returned an error!"
210 output
= subprocess
.Popen([make
], stdout
=subprocess
.PIPE
, cwd
=wd
)
212 c
= output
.stdout
.readline()
213 sys
.stdout
.write(".")
215 if not output
.poll() == None:
216 sys
.stdout
.write("\n")
218 if not output
.returncode
== 0:
219 print "Build failed!"
223 print "Stripping binary."
224 output
= subprocess
.Popen(["strip", progexe
], stdout
=subprocess
.PIPE
, cwd
=wd
)
226 if not output
.returncode
== 0:
227 print "Stripping failed!"
234 print "UPX'ing binary ..."
235 output
= subprocess
.Popen(["upx", progexe
], stdout
=subprocess
.PIPE
, cwd
=wd
)
237 if not output
.returncode
== 0:
238 print "UPX'ing failed!"
243 def zipball(versionstring
, buildfolder
):
244 '''package created binary'''
245 print "Creating binary zipball."
246 archivebase
= program
+ "-" + versionstring
247 outfolder
= buildfolder
+ "/" + archivebase
248 archivename
= archivebase
+ ".zip"
249 # create output folder
251 # move program files to output folder
252 for f
in programfiles
:
253 shutil
.copy(buildfolder
+ "/" + f
, outfolder
)
254 # create zipball from output folder
255 zf
= zipfile
.ZipFile(archivename
, mode
='w', compression
=zipfile
.ZIP_DEFLATED
)
256 for root
, dirs
, files
in os
.walk(outfolder
):
258 physname
= os
.path
.join(root
, name
)
259 filename
= re
.sub("^" + buildfolder
, "", physname
)
260 zf
.write(physname
, filename
)
262 physname
= os
.path
.join(root
, name
)
263 filename
= re
.sub("^" + buildfolder
, "", physname
)
264 zf
.write(physname
, filename
)
266 # remove output folder
267 shutil
.rmtree(outfolder
)
271 def tarball(versionstring
, buildfolder
):
272 '''package created binary'''
273 print "Creating binary tarball."
274 archivebase
= program
+ "-" + versionstring
275 outfolder
= buildfolder
+ "/" + archivebase
276 archivename
= archivebase
+ ".tar.bz2"
277 # create output folder
279 # move program files to output folder
280 for f
in programfiles
:
281 shutil
.copy(buildfolder
+ "/" + f
, outfolder
)
282 # create tarball from output folder
283 tf
= tarfile
.open(archivename
, mode
='w:bz2')
284 tf
.add(outfolder
, archivebase
)
286 # remove output folder
287 shutil
.rmtree(outfolder
)
291 def filehashes(filename
):
292 '''Calculate md5 and sha1 hashes for a given file.'''
293 if not os
.path
.exists(filename
):
297 f
= open(filename
, 'rb')
304 return [m
.hexdigest(), s
.hexdigest()]
307 def filestats(filename
):
308 if not os
.path
.exists(filename
):
310 st
= os
.stat(filename
)
311 print filename
, "\n", "-" * len(filename
)
312 print "Size: %i bytes" % st
.st_size
313 h
= filehashes(filename
)
314 print "md5sum: %s" % h
[0]
315 print "sha1sum: %s" % h
[1]
316 print "-" * len(filename
), "\n"
320 startup
= time
.time()
322 opts
, args
= getopt
.getopt(sys
.argv
[1:], "q:p:t:a:sbdh",
323 ["qmake=", "project=", "tag=", "add=", "source-only", "binary-only", "dynamic", "help"])
324 except getopt
.GetoptError
, err
:
330 svnbase
= svnserver
+ "trunk/"
338 if o
in ("-q", "--qmake"):
340 if o
in ("-p", "--project"):
343 if o
in ("-t", "--tag"):
345 svnbase
= svnserver
+ "tags/" + tag
+ "/"
346 if o
in ("-a", "--add"):
348 if o
in ("-s", "--source-only"):
350 if o
in ("-b", "--binary-only"):
352 if o
in ("-d", "--dynamic"):
354 if o
in ("-h", "--help"):
358 if source
== False and binary
== False:
359 print "Building build neither source nor binary means nothing to do. Exiting."
368 print "ERROR: No suitable Qt installation found."
371 # create working folder. Use current directory if -p option used.
373 w
= tempfile
.mkdtemp()
374 # make sure the path doesn't contain backslashes to prevent issues
375 # later when running on windows.
376 workfolder
= re
.sub(r
'\\', '/', w
)
378 sourcefolder
= workfolder
+ "/" + tag
+ "/"
379 archivename
= tag
+ "-src.tar.bz2"
380 # get numeric version part from tag
381 ver
= "v" + re
.sub('^[^\d]+', '', tag
)
383 trunk
= gettrunkrev(svnbase
)
384 sourcefolder
= workfolder
+ "/rbutil-r" + str(trunk
) + "/"
385 archivename
= "rbutil-r" + str(trunk
) + "-src.tar.bz2"
386 ver
= "r" + str(trunk
)
387 os
.mkdir(sourcefolder
)
392 # check if project file explicitly given. If yes, don't get sources from svn
394 proj
= sourcefolder
+ project
395 # get sources and pack source tarball
396 if not getsources(svnbase
, svnpaths
, sourcefolder
) == 0:
400 tf
= tarfile
.open(archivename
, mode
='w:bz2')
401 tf
.add(sourcefolder
, os
.path
.basename(re
.subn('/$', '', sourcefolder
)[0]))
404 shutil
.rmtree(workfolder
)
407 # figure version from sources. Need to take path to project file into account.
408 versionfile
= re
.subn('[\w\.]+$', "version.h", proj
)[0]
409 ver
= findversion(versionfile
)
412 if not os
.path
.exists(proj
):
413 print "ERROR: path to project file wrong."
416 # copy specified (--add) files to working folder
418 shutil
.copy(f
, sourcefolder
)
419 buildstart
= time
.time()
420 header
= "Building %s %s" % (program
, ver
)
422 print len(header
) * "="
425 if not qmake(qm
, proj
, sourcefolder
, static
) == 0:
427 if not build(sourcefolder
) == 0:
429 if sys
.platform
== "win32":
430 if not upxfile(sourcefolder
) == 0:
432 archive
= zipball(ver
, sourcefolder
)
434 archive
= tarball(ver
, sourcefolder
)
436 # remove temporary files
437 print "Cleaning up working folder %s" % workfolder
439 shutil
.rmtree(workfolder
)
441 print "Project file specified, not cleaning up!"
444 headline
= "Build Summary for %s" % program
445 print "\n", headline
, "\n", "=" * len(headline
)
446 if not archivename
== "":
447 filestats(archivename
)
449 duration
= time
.time() - startup
450 building
= time
.time() - buildstart
451 durmins
= (int)(duration
/ 60)
452 dursecs
= (int)(duration
% 60)
453 buildmins
= (int)(building
/ 60)
454 buildsecs
= (int)(building
% 60)
455 print "Overall time %smin %ssec, building took %smin %ssec." % \
456 (durmins
, dursecs
, buildmins
, buildsecs
)
459 if __name__
== "__main__":