forked from guozanhua/game
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctl.py
executable file
·159 lines (139 loc) · 4.67 KB
/
ctl.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
#!/usr/bin/env python
from __future__ import print_function
import os
import json
import sys
import string
import random
import commands
erl = "erl "
f = file( "config/server.json" )
s = json.load(f)
f.close()
serverId = s["server_id"]
cookie = s["cookie"]
def get_dependencies():
ebinList = [ 'ebin/' ]
ebinStringPipe = os.popen( 'find deps -type d | grep ebin' )
for line in ebinStringPipe:
ebinList.append( line.strip('\n') )
dependencyDirectory = ' '
for e in ebinList:
dependencyDirectory += (' '+ e)
return dependencyDirectory
dependencies = get_dependencies()
def get_proto_files_list():
files = os.listdir("proto")
def f(x):
s = x.split('.')
if len(s) == 1:
return False
else:
filename, extension = s
if extension == "proto":
return True
else:
return False
return filter(f, files)
def generate_pb_list():
files = get_proto_files_list()
r = ""
for file in files:
file = file.split('.')
name,extension = file
pb_name = (name+"_pb")
load_pb = "code:load_file("+pb_name+")"
r += ( name + "," +load_pb+"," )
return r
pb_list = generate_pb_list()
def build():
command = erl + '-pa ' + dependencies + ' -make'
os.system( command )
command = '''for file in $(find src -type f -name "*.app.src");
do
fileBasename=$(basename ${file});
cp -av ${file} ebin/${fileBasename%.*};
done
'''
os.system( command )
def proto():
files = get_proto_files_list()
list.sort(files)
protoStartNum = 20000
nextProtoStep = 100;
output = ""
protoNum = protoStartNum
resultList = []
for file in files:
module,extension = file.split('.')
if extension == "proto":
command = '''./proto/proto_mapper --proto_path=./proto --erlang_mapper_output=./ --proto_id='''+str(protoNum) + " proto/"+file
output = commands.getoutput(command)
resultList.append(output)
protoNum += nextProtoStep
lastProto = resultList[-1]
lastSemicolonIndex = lastProto.rfind(';')
lastProtoWithoutSemicolon = lastProto[:lastSemicolonIndex]
lastProto = lastProtoWithoutSemicolon + '.'
resultList[-1] = lastProto
fileHeaders = '-module(proto_mapper).\n-export([get/1]).\n'
fileContents = '\n'.join(resultList)
fileContents = fileHeaders + fileContents
with open( "src/proto/proto_mapper.erl", "w") as f:
f.write(fileContents)
command = erl + '-pa ' + dependencies + ' -eval \'proto:compile_all()\''
os.system(command)
def rebuild():
command = ''' rm -fr ebin && mkdir ebin '''
os.system( command )
proto()
build()
def debug():
command = erl+'-pa '+dependencies+' -name debug'+str(int(random.uniform(1,20000)))+'@127.0.0.1'+' -setcookie '+cookie+" -hidden" + " -eval \'" + pb_list + "ok\'"
os.system( command )
def start_connectors():
connectorNodes = s['connector']
for node in connectorNodes:
id = node['id']
host = node['host']
port = node['port']
nodeName = serverId+'_'+id+'@'+host
command1 = erl + '-setcookie ' + cookie + " -s lager" + " -pa " + dependencies + " -detached -name " + nodeName
command2 = " -port " + str(port) + " -eval \'" + pb_list+ " application:ensure_all_started(connector).\' "
command = command1+command2
os.system(command)
def start_games():
gameNodes = s['game']
for node in gameNodes:
id = node['id']
host = node['host']
nodeName = serverId+'_'+id+'@'+host
command1 = erl + '-setcookie ' + cookie + " -s lager" +" -pa " + dependencies + " -detached -name " + nodeName
command2 = " -eval \'" + pb_list+ "application:start(game).\'"
command = command1 + command2
os.system(command)
def start():
start_connectors()
start_games()
def help():
helpMessage = '''
Execute this script to generate protocol files,
build, and start the erlang game server.
Usage:
./ctl build #build *.erl to *.beam files
./ctl proto #generate *.proto to *.erl and *.hrl files
./ctl start #start this game server
'''
print(helpMessage)
if __name__ == '__main__':
commandFunMapper = { 'build':build, 'start':start, 'debug':debug,
'rebuild':rebuild, 'proto':proto,'help':help }
try:
command = sys.argv[1]
commandFunMapper[command]()
except IndexError:
commandFunMapper['help']()
exit(1)
except KeyError:
commandFunMapper['help']()
exit(1)