2010-12-05 22:01:05 +00:00
import imp , optparse , os , re , shutil , sys , zipfile
2012-04-26 16:25:04 +00:00
os . chdir ( os . path . dirname ( os . path . realpath ( sys . argv [ 0 ] ) ) + " /.. " )
import twlib
2010-12-05 22:01:05 +00:00
2012-04-26 16:25:04 +00:00
arguments = optparse . OptionParser ( )
arguments . add_option ( " -b " , " --url-bam " , default = " http://github.com/matricks/bam/zipball/master " , help = " URL from which the bam source code will be downloaded " )
arguments . add_option ( " -t " , " --url-teeworlds " , default = " http://github.com/teeworlds/teeworlds/zipball/master " , help = " URL from which the teeworlds source code will be downloaded " )
arguments . add_option ( " -r " , " --release-type " , default = " server_release client_release " , help = " Parts of the game which should be builded (for example client_release, debug, server_release or a combination of any of them) " )
2010-12-05 22:01:05 +00:00
( options , arguments ) = arguments . parse_args ( )
bam = options . url_bam [ 7 : ] . split ( " / " )
version_bam = re . search ( r " \ d \ . \ d \ . \ d " , bam [ len ( bam ) - 1 ] )
if version_bam :
version_bam = version_bam . group ( 0 )
else :
version_bam = " trunk "
teeworlds = options . url_teeworlds [ 7 : ] . split ( " / " )
version_teeworlds = re . search ( r " \ d \ . \ d \ . \ d " , teeworlds [ len ( teeworlds ) - 1 ] )
if version_teeworlds :
version_teeworlds = version_teeworlds . group ( 0 )
else :
version_teeworlds = " trunk "
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
bam_execution_path = " "
if version_bam < " 0.3.0 " :
bam_execution_path = " src %s " % os . sep
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
name = " teeworlds "
2008-03-22 18:40:27 +00:00
2007-12-12 21:53:06 +00:00
flag_clean = True
2011-04-02 02:58:45 +00:00
flag_download = True
flag_unpack = True
flag_build_bam = True
flag_build_teeworlds = True
flag_make_release = True
2007-12-12 21:53:06 +00:00
if os . name == " nt " :
platform = " win32 "
else :
# get name
2008-03-22 19:33:45 +00:00
osname = os . popen ( " uname " ) . readline ( ) . strip ( ) . lower ( )
if osname == " darwin " :
osname = " osx "
2010-12-05 22:01:05 +00:00
2007-12-12 21:53:06 +00:00
# get arch
machine = os . popen ( " uname -m " ) . readline ( ) . strip ( ) . lower ( )
arch = " unknown "
2010-12-05 22:01:05 +00:00
2007-12-12 21:53:06 +00:00
if machine [ 0 ] == " i " and machine [ 2 : ] == " 86 " :
arch = " x86 "
elif machine == " x86_64 " :
arch = " x86_64 "
elif " power " in machine . lower ( ) :
arch = " ppc "
2010-12-05 22:01:05 +00:00
platform = osname + " _ " + arch
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
print ( " %s - %s - %s " % ( name , version_teeworlds , platform ) )
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
root_dir = os . getcwd ( ) + os . sep
2011-05-06 16:27:06 +00:00
work_dir = root_dir + " scripts/work "
2007-12-12 21:53:06 +00:00
def unzip ( filename , where ) :
2010-12-05 22:01:05 +00:00
try :
z = zipfile . ZipFile ( filename , " r " )
except :
return False
list = " \n "
2007-12-12 21:53:06 +00:00
for name in z . namelist ( ) :
2010-12-05 22:01:05 +00:00
list + = " %s \n " % name
2007-12-12 21:53:06 +00:00
try : os . makedirs ( where + " / " + os . path . dirname ( name ) )
except : pass
2010-12-05 22:01:05 +00:00
2009-01-11 12:35:37 +00:00
try :
2010-12-05 22:01:05 +00:00
f = open ( where + " / " + name , " wb " )
2009-01-11 12:35:37 +00:00
f . write ( z . read ( name ) )
f . close ( )
except : pass
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
z . close ( )
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
directory = " [^/ \n ]*?/ "
part_1 = " (?<= \n ) "
part_2 = " [^/ \n ]+(?= \n ) "
regular_expression = r " %s %s " % ( part_1 , part_2 )
main_directory = re . search ( regular_expression , list )
while main_directory == None :
part_1 + = directory
regular_expression = r " %s %s " % ( part_1 , part_2 )
main_directory = re . search ( regular_expression , list )
main_directory = re . search ( r " .*/ " , " ./ %s " % main_directory . group ( 0 ) )
return main_directory . group ( 0 )
2007-12-12 21:53:06 +00:00
def bail ( reason ) :
2010-12-05 22:01:05 +00:00
print ( reason )
os . chdir ( work_dir )
2007-12-12 21:53:06 +00:00
sys . exit ( - 1 )
2010-12-05 22:01:05 +00:00
def clean ( ) :
print ( " *** cleaning *** " )
2011-05-06 16:27:06 +00:00
try : shutil . rmtree ( work_dir )
2007-12-12 21:53:06 +00:00
except : pass
2010-12-05 22:01:05 +00:00
def file_exists ( file ) :
try :
open ( file ) . close ( )
return True
except :
return False ;
2011-04-02 02:58:45 +00:00
def search_object ( directory , object ) :
directory = os . listdir ( directory )
for entry in directory :
match = re . search ( object , entry )
if match != None :
return entry
2010-12-05 22:01:05 +00:00
# clean
if flag_clean :
clean ( )
2007-12-12 21:53:06 +00:00
# make dir
2011-05-06 16:27:06 +00:00
try : os . mkdir ( work_dir )
2007-12-12 21:53:06 +00:00
except : pass
# change dir
2010-12-05 22:01:05 +00:00
os . chdir ( work_dir )
2007-12-12 21:53:06 +00:00
# download
if flag_download :
2010-12-05 22:01:05 +00:00
print ( " *** downloading bam source package *** " )
2012-04-26 16:25:04 +00:00
src_package_bam = twlib . fetch_file ( options . url_bam )
2010-12-05 22:01:05 +00:00
if src_package_bam :
if version_bam == ' trunk ' :
version = re . search ( r " -[^-]*?([^-]*?) \ .[^.]*$ " , src_package_bam )
if version :
version_bam = version . group ( 1 )
else :
2011-04-02 02:58:45 +00:00
bail ( " couldn ' t find bam source package and couldn ' t download it " )
2010-12-05 22:01:05 +00:00
print ( " *** downloading %s source package *** " % name )
2012-04-26 16:25:04 +00:00
src_package_teeworlds = twlib . fetch_file ( options . url_teeworlds )
2010-12-05 22:01:05 +00:00
if src_package_teeworlds :
if version_teeworlds == ' trunk ' :
version = re . search ( r " -[^-]*?([^-]*?) \ .[^.]*$ " , src_package_teeworlds )
if version :
version_teeworlds = version . group ( 1 )
else :
2011-04-02 02:58:45 +00:00
bail ( " couldn ' t find %s source package and couldn ' t download it " % name )
2007-12-12 21:53:06 +00:00
# unpack
2011-04-02 02:58:45 +00:00
if flag_unpack :
print ( " *** unpacking source *** " )
if hasattr ( locals ( ) , ' src_package_bam ' ) == False :
src_package_bam = search_object ( work_dir , r " bam.*? \ . " )
if not src_package_bam :
bail ( " couldn ' t find bam source package " )
src_dir_bam = unzip ( src_package_bam , " . " )
if not src_dir_bam :
bail ( " couldn ' t unpack bam source package " )
2010-12-05 22:01:05 +00:00
2011-04-02 02:58:45 +00:00
if hasattr ( locals ( ) , ' src_package_teeworlds ' ) == False :
src_package_teeworlds = search_object ( work_dir , r " teeworlds.*? \ . " )
if not src_package_bam :
bail ( " couldn ' t find %s source package " % name )
src_dir_teeworlds = unzip ( src_package_teeworlds , " . " )
if not src_dir_teeworlds :
bail ( " couldn ' t unpack %s source package " % name )
2007-12-12 21:53:06 +00:00
# build bam
2011-04-02 02:58:45 +00:00
if flag_build_bam :
2010-12-05 22:01:05 +00:00
print ( " *** building bam *** " )
os . chdir ( " %s / %s " % ( work_dir , src_dir_bam ) )
2007-12-12 21:53:06 +00:00
if os . name == " nt " :
2010-12-05 22:01:05 +00:00
bam_compiled = False
compiler_bam = [ " cl " , " gcc " ]
for compiler in compiler_bam :
if compiler == " cl " :
os . system ( " make_win32_msvc.bat " )
elif compiler == " gcc " :
os . system ( " make_win32_mingw.bat " )
if file_exists ( " %s bam.exe " % bam_execution_path ) == True :
bam_compiled = True
break
if bam_compiled == False :
2007-12-12 21:53:06 +00:00
bail ( " failed to build bam " )
else :
2010-12-05 22:01:05 +00:00
os . system ( " sh make_unix.sh " )
if file_exists ( " %s bam " % bam_execution_path ) == False :
2007-12-12 21:53:06 +00:00
bail ( " failed to build bam " )
2010-12-05 22:01:05 +00:00
os . chdir ( work_dir )
2007-12-12 21:53:06 +00:00
2008-03-22 18:40:27 +00:00
# build the game
2011-04-02 02:58:45 +00:00
if flag_build_teeworlds :
2010-12-05 22:01:05 +00:00
print ( " *** building %s *** " % name )
2011-04-02 02:58:45 +00:00
if hasattr ( locals ( ) , ' src_dir_bam ' ) == False :
src_dir_bam = search_object ( work_dir , r " bam[^ \ .]*$ " ) + os . sep
if src_dir_bam :
directory = src_dir_bam + bam_execution_path
file = r " bam "
if os . name == " nt " :
file + = r " \ .exe "
if not search_object ( directory , file ) :
bail ( " couldn ' t find bam " )
else :
bail ( " couldn ' t find bam " )
if hasattr ( locals ( ) , ' src_dir_teeworlds ' ) == False :
src_dir_teeworlds = search_object ( work_dir , r " teeworlds[^ \ .]*$ " )
if not src_dir_teeworlds :
bail ( " couldn ' t find %s source " % name )
command = 1
2010-12-05 22:01:05 +00:00
if os . name == " nt " :
2010-12-16 02:14:31 +00:00
file = open ( " build.bat " , " wb " )
content = " @echo off \n "
content + = " @REM check if we already have the tools in the environment \n "
content + = " if exist \" % VCINSTALLDIR % \" ( \n goto compile \n ) \n "
content + = " @REM Check for Visual Studio \n "
content + = " if exist \" % VS100COMNTOOLS % \" ( \n set VSPATH= \" % VS100COMNTOOLS % \" \n goto set_env \n ) \n "
content + = " if exist \" % VS90COMNTOOLS % \" ( \n set VSPATH= \" % VS90COMNTOOLS % \" \n goto set_env \n ) \n "
content + = " if exist \" % VS80COMNTOOLS % \" ( \n set VSPATH= \" % VS80COMNTOOLS % \" \n goto set_env \n ) \n \n "
content + = " echo You need Microsoft Visual Studio 8, 9 or 10 installed \n "
content + = " exit \n "
content + = " @ setup the environment \n "
content + = " :set_env \n "
content + = " call % VSPATH % vsvars32.bat \n \n "
content + = " :compile \n "
content + = ' cd %s \n " %s \\ %s %s bam " config \n " %s \\ %s %s bam " %s ' % ( src_dir_teeworlds , work_dir , src_dir_bam , bam_execution_path , work_dir , src_dir_bam , bam_execution_path , options . release_type )
2011-04-02 02:49:14 +00:00
file . write ( content . encode ( ) )
2010-12-16 02:14:31 +00:00
file . close ( )
command = os . system ( " build.bat " )
2010-12-05 22:01:05 +00:00
else :
os . chdir ( src_dir_teeworlds )
command = os . system ( " %s / %s %s bam %s " % ( work_dir , src_dir_bam , bam_execution_path , options . release_type ) )
if command != 0 :
2008-03-22 18:40:27 +00:00
bail ( " failed to build %s " % name )
2010-12-05 22:01:05 +00:00
os . chdir ( work_dir )
2007-12-12 21:53:06 +00:00
# make release
2011-04-02 02:58:45 +00:00
if flag_make_release :
2010-12-05 22:01:05 +00:00
print ( " *** making release *** " )
2011-04-02 02:58:45 +00:00
if hasattr ( locals ( ) , ' src_dir_teeworlds ' ) == False :
src_dir_teeworlds = search_object ( work_dir , r " teeworlds[^ \ .]*$ " )
if not src_dir_teeworlds :
bail ( " couldn ' t find %s source " % name )
2010-12-05 22:01:05 +00:00
os . chdir ( src_dir_teeworlds )
2012-04-26 16:25:04 +00:00
command = ' " %s / %s /scripts/make_release.py " -p " %s " -s " %s / %s " -v " %s " ' % ( work_dir , src_dir_teeworlds , platform , work_dir , src_dir_teeworlds , version_teeworlds )
2010-12-05 22:01:05 +00:00
if os . name != " nt " :
command = " python %s " % command
if os . system ( command ) != 0 :
bail ( " failed to make a relase of %s " % name )
2007-12-12 21:53:06 +00:00
final_output = " FAIL "
for f in os . listdir ( " . " ) :
2010-12-05 22:01:05 +00:00
if version_teeworlds in f and platform in f and name in f and ( " .zip " in f or " .tar.gz " in f ) :
2007-12-12 21:53:06 +00:00
final_output = f
2010-12-05 22:01:05 +00:00
os . chdir ( work_dir )
shutil . copy ( " %s / %s " % ( src_dir_teeworlds , final_output ) , " ../ " + final_output )
2007-12-12 21:53:06 +00:00
os . chdir ( root_dir )
2010-12-05 22:01:05 +00:00
clean ( )
2007-12-12 21:53:06 +00:00
2010-12-05 22:01:05 +00:00
print ( " *** all done *** " )