forked from lowne/xCodea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcodeaserver.py
executable file
·159 lines (138 loc) · 5.01 KB
/
xcodeaserver.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
#!/usr/local/bin/python2.7
# encoding: utf-8
''' xCodea server
Usage:
xcodeaserver.py <projDir> [--root=<projectsRoot>] [--src=<srcSubDir>]
[--images=<imgSubDir>] [--shaders=<shdSubDir]
[--sounds=<sndSubDir>] [--music=<musSubDir>]
[--color] [--notify] [--sound] [--logging]
[--polling=<interval>] [--verbose]
[--long-polling]
xcodeaserver.py --push [-csv] <projDir>
xcodeaserver.py --pull [-csv] <projDir>
xcodeaserver.py --help
Options:
--root=<projectsRoot> Directory containing your projects [default: .]
--src=<srcSubDir> Location of .lua files relative to the project directory
(e.g. use --src=src for Eclipse/LDT) [default: .]
--images=<imgSubDir> Location of image files (.png/.jpg/.gif) [default: .]
--shaders=<shdSubDir> Location of shader files (not implemented) [default: .]
--sounds=<sndSubDir> Location of sound files (not implemented) [default: .]
--music=<musSubDir> Location of music files (not implemented) [default: .]
-s --sound Use afplay for feedback (sounds)
-n --notify Use terminal-notifier for feedback on errors
-c --color Colorize output
-l --logging Log print() statements from Codea
-p --polling=<interval> Polling interval in seconds [default: 1]
-v --verbose Debug logging
--long-polling Enable long polling (WARNING: Codea will crash when
you back out of xCodea)
--pull Pulls
--push Pushes
-h --help Show this screen
'''
import xcodeaserver_dev as x
DEV = False
DEV = True
import sys,time,os
from os.path import isdir,isfile,join,normpath
from os import listdir
import BaseHTTPServer
from docopt import docopt
import json
is_running = False
class XCodeaServer(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(self):
if DEV:
reload(x)
x.do_HEAD(self)
def do_GET(self):
if self.path=='/done':
self.send_response(200)
self.end_headers()
x.log('Operation completed, exiting now.')
stop_server()
return
if DEV:
reload(x)
x.do_GET(self)
def do_POST(self):
if DEV:
reload(x)
x.do_POST(self)
def do_PUT(self):
if DEV:
reload(x)
x.do_PUT(self)
def log_request(self, code='-', size='-'): #called by send_response()
try:
(200,204).index(code)
except ValueError:
self.log_message('"%s" %s %s',
self.requestline, str(code), str(size))
httpd = BaseHTTPServer.HTTPServer(('', 49374), XCodeaServer)
def start_server():
global is_running
global httpd
x.counter=0
x.polling_wait = 0.1
x.last_eval = ''
x.POLLING_MAX_WAIT = args['--long-polling'] and 4 or 0.1
x.log_buffer = []
proj=args['<projDir>']
#x.notify = 'terminal-notifier' if args['--notify'] else None
x.notify = args['--notify']
x.sound = args['--sound']
x.color = args['--color']
x.pollingInterval = args['--long-polling'] and 0.1 or args['--polling']
x.logging = args['--logging']
#x.watches = args['--watches']
x.verbose = args['--verbose']
#x.overwrite = args['--overwrite']
x.projectsRoot = args['--root']
x.file_dirs = {'source':args['--src'],'image':args['--images'],'sound':args['--sounds'],
'music':args['--music'],'shader':args['--shaders']}
x.pull = args['--pull']
x.push = args['--push']
x.cachefile = normpath(join(x.projectsRoot,'.xcodea.cache'))
if not isfile(x.cachefile):
f=open(x.cachefile,'w')
f.write('{}')
f.close()
x.cache = json.load(open(x.cachefile))
x.ldtbuildpath = '.buildpath'
x.gui_app = args.get('gui_app')
proj = normpath(proj)
if x.push:
if not isdir(join(x.projectsRoot,proj)):
x.colorprint(x.RED,'Project '+proj+' not found!')
sys.exit(1)
if not isfile(join(x.projectsRoot,proj,'Main.lua')):
x.colorprint(x.RED,'Project '+proj+' is not a valid Codea project!')
sys.exit(1)
if proj=='xCodea':
print('Better not :p')
sys.exit(1)
if not isdir(join(x.projectsRoot,proj)):
x.colorprint(x.RED,'Warning: project '+proj+' not found! Its folder will be created on successful connection')
x.colorprint(x.RED,'If you are using LDT, better create an empty project *first* so that dependencies can be synced correctly')
x.project = proj
x.colorprint(x.BLUE,'xCodea server started on port 49374')
if x.pull:
x.colorprint(x.RED,'Warning: will pull project '+proj+' from Codea. Any local files in the project or its dependencies will be overwritten or deleted.')
if x.push:
x.colorprint(x.RED,'Warning: will push project '+proj+' to Codea. Existing Codea tabs in the project or its dependencies will be overwritten or deleted.')
is_running = True
httpd.serve_forever()
is_running = False
# while not x.shutdown:
# httpd.handle_request()
# is_running = False
# httpd.serve_forever()
def stop_server():
httpd.shutdown()
is_running = False
x.colorprint(x.BLUE,'xCodea server stopped')
if __name__ == '__main__':
args = docopt(__doc__)
start_server()