about summary refs log tree commit diff
path: root/scripts/build.py
blob: 2da77169b5dbaf292204aa6eb1bcff1f320f3f20 (plain)
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
import os, sys, shutil, httplib, zipfile

name = "teeworlds"
domain = "www.%s.com" % name
version = sys.argv[1]

bam_version = "bam-0.2.0"

flag_download = True
flag_clean = True

if os.name == "nt":
	platform = "win32"
else:
	# get name
	osname = os.popen("uname").readline().strip().lower()
	if osname == "darwin":
		osname = "osx"
		
	# get arch
	machine = os.popen("uname -m").readline().strip().lower()
	arch = "unknown"
	
	if machine[0] == "i" and machine[2:] == "86":
		arch = "x86"
	elif machine == "x86_64":
		arch = "x86_64"
	elif "power" in machine.lower():
		arch = "ppc"
		
	platform = osname + "_" + arch

print "%s-%s-%s" % (name,version, platform)

src_package = "%s-%s-src.zip" % (name, version)

root_dir = os.getcwd() + "/work"
src_dir = ""

def fetch_file(server, url, local):
	try:
		conn = httplib.HTTPConnection(server)
		print "trying %s%s" % (server, url)
		conn.request("GET", url)
		response = conn.getresponse()
		if response.status != 200:
			return False
		
		f = file(local, "wb")
		f.write(response.read())
		f.close()
		conn.close()
		return True
	except:
		pass
	return False

def unzip(filename, where):
	z = zipfile.ZipFile(filename, "r")
	for name in z.namelist():
		try: os.makedirs(where+"/"+os.path.dirname(name))
		except: pass
		
		try:
			f = file(where+"/"+name, "wb")
			f.write(z.read(name))
			f.close()
		except: pass
		
	z.close()


def path_exist(d):
	try: os.stat(d)
	except: return False
	return True

def bail(reason):
	print reason
	os.chdir(root_dir)
	sys.exit(-1)

# clean
if flag_clean:
	print "*** cleaning ***"
	try: shutil.rmtree("work")
	except: pass
	
# make dir
try: os.mkdir("work")
except: pass

# change dir
os.chdir(root_dir)

# download
if flag_download:
	print "*** downloading bam source package ***"
	if not fetch_file(domain, "trac/bam/browser/releases/"+bam_version+".zip?format=raw", "bam.zip"):
			bail("couldn't find source package and couldn't download it")
		
	print "*** downloading %s source package ***" % name
	if not fetch_file(domain, "/files/%s" % src_package, src_package):
		if not fetch_file(domain, "/files/beta/%s" % src_package, src_package):
			bail("couldn't find source package and couldn't download it")

# unpack
print "*** unpacking source ***"
unzip("bam.zip", ".")
unzip(src_package, name)
src_dir = name+"/"+ os.listdir(name+"/")[0]

# build bam
if 1:
	print "*** building bam ***"
	os.chdir(bam_version)
	output = "bam"
	bam_cmd = "./bam"
	if os.name == "nt":
		if os.system("make_win32_msvc.bat") != 0:
			bail("failed to build bam")
		output += ".exe"
		bam_cmd = "bam"
	else:
		if os.system("sh make_unix.sh") != 0:
			bail("failed to build bam")
	os.chdir(root_dir)
	shutil.copy(bam_version+"/src/"+output, src_dir+"/"+output)

# build the game
if 1:
	print "*** building %s ***" % name
	os.chdir(src_dir)
	if os.system("%s server_release client_release" % bam_cmd) != 0:
		bail("failed to build %s" % name)
	os.chdir(root_dir)

# make release
if 1:
	print "*** making release ***"
	os.chdir(src_dir)
	if os.system("python scripts/make_release.py %s %s" % (version, platform)) != 0:
		bail("failed to make a relase of %s"%name)
	final_output = "FAIL"
	for f in os.listdir("."):
		if version in f and platform in f and name in f and (".zip" in f or ".tar.gz" in f):
			final_output = f
	os.chdir(root_dir)
	shutil.copy("%s/%s" % (src_dir, final_output), "../"+final_output)

print "*** all done ***"