-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
485 lines (391 loc) · 16.7 KB
/
server.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import sublime
import os
import stat
import requests
import subprocess
import json
import tarfile
import shutil
import io
SETTINGS = "JSONComma.sublime-settings"
SETTINGS_EXECUTABLE = "executable_path"
SETTINGS_AUTO_UPDATE = "automatically_update_executable"
class server:
""" This server is more complex that it should be because sublime text
doesn't have an on_exit event (hence we can't kill the server)
Instead, we always use the same port (and assume it jsoncomma running on
it). If we need to shut it down (for example to update), we send a
/shutdown request because we have no guarantee that it is our process
that owns the server process.
"""
# make sure we explicitely use IPv6 or IPv4, because otherwise, if the client
# and the server don't use the same by defaut, Windows will have a one second
# timeout (eg. server runs on IPv6, client on IPv4. On Windows, it'll try IPv4,
# timeout after one second, and *then* try IPv6. So, it blocks the editor).
# https://github.com/jsoncomma/sublime-jsoncomma/issues/10#issuecomment-574889982
# So, we force IPv4 because it looks like it's a pain to force IPv6 with
# requests
HOST = "127.0.0.1"
PORT = 2442
downloading = False
@classmethod
def start(cls):
""" Starts the server, downloading updates if needed
"""
# this is some bad logic, but it needs to cover all those paths
# auto updates on, executable doesn't exists (require update)
# auto updates on, executable exists (try to update)
# auto updates off, exectuable doesn't exists (ask to enable auto updates)
# auto updates off, exectuable exists (start process)
# TODO: add in yes/no from user
settings = sublime.load_settings(SETTINGS)
if settings.get(SETTINGS_AUTO_UPDATE):
executable_path = cls.get_default_executable_path(expand_vars=True)
else:
executable_path = settings.get(SETTINGS_EXECUTABLE)
executable_path = os.path.expanduser(os.path.expandvars(executable_path))
if not os.path.exists(executable_path) and not fixed_wrong_location(
executable_path
):
if not confirm_automatic_download(executable_path):
notify(
"no binary found, and automatic updates disabled. JSONComma will not work"
)
settings.set(SETTINGS_AUTO_UPDATE, False)
sublime.save_settings(SETTINGS)
return
settings.set(SETTINGS_AUTO_UPDATE, True)
sublime.save_settings(SETTINGS)
executable_path = cls.get_default_executable_path(expand_vars=True)
if settings.get(SETTINGS_AUTO_UPDATE):
try:
cls.auto_update_executable()
except requests.ConnectionError as e:
print("JSONComma:", e)
if not os.path.exists(executable_path):
notify(
"failed to download server due to network error, JSONComma won't work"
)
return
else:
notify(
"failed to automatically download server due to network error, running current version"
)
# we don't store the process, see comment under server
process = start_process(
[
executable_path,
"server",
"-host",
server.HOST,
"-port",
str(server.PORT),
],
)
line = process.stdout.readline().decode("utf-8")
try:
infos = json.loads(line)
except ValueError as e:
sublime.error_message(
"Failed to start jsoncomma server.\n\n{}\n\nMore details in the console".format(
e
)
)
print("JSONComma: output from the server: {!r}".format(line))
raise e
assert "kind" in infos, "expected 'kind' field in {}".format(infos)
if infos["kind"] == "error":
assert "error" in infos, "expected 'error' field in {}".format(infos)
assert "details" in infos, "expected 'details' field in {}".format(infos)
assert "context" in infos, "expected 'context' field in {}".format(infos)
if infos["context"] != "opening socket":
print("JSONComma: unexpected error trying to open new server", infos)
print("JSONComma: assume already running")
return
assert infos["kind"] == "started", "expected started kind in {}".format(infos)
assert "addr" in infos, "server infos should include 'addr' ({})".format(infos)
assert "port" in infos, "server infos should include 'port' ({})".format(infos)
assert "host" in infos, "server infos should include 'host' ({})".format(infos)
# we can't assert about host because, for example, we might listen on "localhost",
# the server will reply 127.0.0.1. We could do gethostbyname, but that's probably
# an over kill
assert (
infos["port"] == server.PORT
), "server started on port {}, expected {}".format(infos["port"], server.PORT)
notify("server {} started on {}", executable_path, infos["addr"])
@classmethod
def stop(cls):
""" stops the server. It is safe even if the server is dead.
This function blocks, because wait for the server to close all of it's handlers.
"""
try:
resp = requests.get(
"http://{host}:{port}/shutdown".format(
host=server.HOST, port=server.PORT
),
stream=True,
)
except requests.ConnectionError as e:
# this is the best we can do to check that we got a ConnectionRefusedError
# after than, we get string, wtf urllib3. e.args[0].reason.args[0] is a
# string.
# we could try to parse the string... But that's not worth it.
assert isinstance(
e.args[0].reason,
requests.packages.urllib3.exceptions.NewConnectionError,
), "expected NewConnectionError, got {}".format(e.args[0].reason)
# the server isn't running
return
line = resp.raw.readline().decode("utf-8")
try:
data = json.loads(line)
except ValueError as e:
print("JSONComma: first line after /shutdown: {!r}".format(line))
raise e
assert (
"timedout" in data
), "response should include 'timedout' field ({})".format(data)
if data["timedout"] is True:
notify("JSONComma: server aborted")
else:
notify("JSONComma: server gracefully shutdown")
@classmethod
def fix(cls, json_to_fix):
try:
resp = requests.post(
"http://{host}:{port}".format(port=server.PORT, host=server.HOST),
data=json_to_fix,
)
except requests.ConnectionError as e:
if cls.downloading:
notify("downloading/updating the server, please wait")
return
cls.start()
notify(
"connection error with server ({}), started server, gave up trying to correct (save again to trigger)",
e,
)
return
if resp.status_code != 200:
print("response from JSONComma server:")
print(resp)
notify(
"invalid response code from server (got {}, expected 200)",
resp.status_code,
)
return
if resp.headers["Content-Type"] != "text/plain; charset=utf-8":
print("response from JSONComma server:")
print(resp)
notify(
"invalid header 'Content-Type' (got {!r}, expected 'text/plain; charset=utf-8')",
resp.headers["Content-Type"],
)
return
return resp.text
#
# downloading/updating the binary
#
@classmethod
def auto_update_executable(cls):
""" install the binary in the right location, and store the path in settings.
It returns the location of the binary. This function blocks. """
assert (
cls.downloading is False
), "auto_update_executable called already (cls.downloading: {})".format(
cls.downloading
)
executable_path = cls.get_default_executable_path(expand_vars=True)
cls.downloading = True
latest_version = cls.get_latest_version()
try:
current_version = cls.get_current_executable_version(executable_path)
except FileNotFoundError as e:
# we are going to download it
notify(
"current version: non existent ({!r}), latest version: {}",
e,
latest_version,
)
else:
if current_version == latest_version:
# don't need to update
cls.downloading = False
return executable_path
notify(
"current version: {}, latest version: {}".format(
current_version, latest_version
)
)
platforms = {
# sublime.plaform() -> goreleaser's platform names
"windows": "Windows",
"osx": "Darwin",
"linux": "Linux",
}
archs = {
# sublime.arch() -> goreleaser's arch names
"x32": "i386",
"x64": "x86_64",
}
download_url = (
"https://github.com/jsoncomma/jsoncomma/releases/download/"
"v{version}/jsoncomma_v{version}_{platform}_{arch}.tar.gz".format(
version=latest_version,
platform=platforms[sublime.platform()],
arch=archs[sublime.arch()],
)
)
notify("downloading binary from {}", download_url)
# We don't stream the download because tarfile can't extract file if you
# don't have a seek method (and setting stream=True gives you a file object
# without seek)
# let any network error boil up the stack, it's cleaner when handled above
resp = requests.get(download_url)
assert (
resp.status_code == 200
), "[downloading] expected 200 status code, got {}".format(resp.status_code)
os.makedirs(os.path.dirname(executable_path), exist_ok=True)
# make sure the server is stopped before we update the binary
cls.stop()
notify("extracting tar...")
with open(executable_path, "wb") as target, tarfile.open(
mode="r:gz", fileobj=io.BytesIO(resp.content)
) as tar:
for tarinfo in tar.getmembers():
if tarinfo.name.startswith("jsoncomma"):
break
notify("extracting file {!r}...", tarinfo.name)
fileobj = tar.extractfile(tarinfo.name)
assert fileobj is not None, "extracted {}, but got None".format(
tarinfo.name
)
shutil.copyfileobj(fileobj, target)
os.chmod(executable_path, os.stat(executable_path).st_mode | stat.S_IEXEC)
cls.downloading = False
# FIXME: maybe we should have a platform dependent settings file...
@classmethod
def get_latest_version(cls) -> str:
""" Gets the latest version from GitHub
"""
notify("checking last release...")
resp = requests.get("https://api.github.com/repos/jsoncomma/jsoncomma/releases")
assert (
resp.status_code == 200
), "[getting last release] expected 200 status code, got {}".format(
resp.status_code
)
releases = resp.json()
for release in releases:
# ignore prereleases and drafts
if release["draft"] or release["prerelease"]:
continue
assert release["tag_name"].startswith(
"v"
), "expected tag name to start with 'v' in {!r}".format(release["tag_name"])
return release["tag_name"][1:]
assert False, "no non-draft or non-prerelease release found"
@classmethod
def get_current_executable_version(cls, executable_path):
process = start_process([executable_path, "-version"])
try:
exit_code = process.wait(timeout=1)
except subprocess.TimeoutExpired:
kill_nicely(process)
return
if exit_code != 0:
notify("{} -version exited with code {}", executable_path, exit_code)
return None
return process.stdout.readline().decode("utf-8").split(" ")[0]
@classmethod
def get_default_executable_path(cls, *, expand_vars):
assert isinstance(
expand_vars, bool
), "expand_vars should be a boolean, got {}".format(type(expand_vars))
path = None
platform = sublime.platform()
if platform == "windows":
path = "%APPDATA%\\jsoncomma\\jsoncomma.exe"
elif platform == "osx":
path = "~/Library/Application Support/jsoncomma/jsoncomma"
elif platform == "linux":
xdg_data_home = os.environ.get("XDG_DATA_HOME", "") or "~/.local/share"
path = os.path.join(xdg_data_home, "jsoncomma", "jsoncomma")
assert path is not None, "unknown platform {!r}".format(platform)
if expand_vars:
return os.path.expandvars(os.path.expanduser(path))
return path
def confirm_automatic_download(current_path):
# I'm not sure how I can make it clear that this is a one time thing.
# if the user wants to update the server, he will have to do so manually
return sublime.ok_cancel_dialog(
"The jsoncomma server was not found at '{}' "
"but, it needs to be installed for JSONComma to work. "
"More details can be found at https://jsoncomma.github.io. "
"If you decline, automatic updates will be disabled (you can change "
"that in JSONComma's settings)."
"\n\n"
"Automatic download is recommended for all users who are not "
"currently contributing to the development to jsoncomma."
"\n\n"
"Do you want JSONComma to install it for you?".format(current_path),
"Download jsoncomma for me",
)
def notify(format, *args, **kwargs):
message = "JSONComma: " + format.format(*args, **kwargs)
print(message)
sublime.status_message(message)
def start_process(cmd, *args, **kwargs):
# hide the terminal window on Windows
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# catch stdout and stderr by default
if "stdout" not in kwargs:
kwargs["stdout"] = subprocess.PIPE
if "stderr" not in kwargs:
kwargs["stderr"] = subprocess.STDOUT
return subprocess.Popen(cmd, startupinfo=startupinfo, *args, **kwargs)
def kill_nicely(process, *, timeout=1):
""" tries to terminate, and then kills after timeout seconds
returns the exit code
"""
process.terminate()
try:
return process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
process.kill()
return process.poll()
def fixed_wrong_location(new_path):
""" I stuffed the location of the binary executable initially, and @WillMoggridge
pointed me towards the relevant document and paths I should use.
So, this is an automatic fix for my mistake
This returns True if it did fix anything
"""
if sublime.platform() != "linux":
return False
old_path = os.path.expanduser("~/.config/jsoncomma/jsoncomma")
if not os.path.isfile(old_path):
return False
# we know what new_path doesn't exists because otherwise, this fixup wouldn't have been
# called
try:
os.makedirs(os.path.dirname(new_path), exist_ok=True)
os.rename(old_path, new_path)
# we shouldn't have any other files in old_path's folder. If we do, rmdir is going
# to fail (which we want it to, don't want to remove files we haven't put there)
os.rmdir(os.path.dirname(old_path))
except OSError:
import traceback
sublime.error_message(
"Failed trying to automatically move {} to {}. Accept the automatic update"
"that is going to show up next, and then delete the folder "
"~/.config/jsoncomma. More details about the error in the console".format(
old_path, new_path
)
)
print("JSONComma: automatic fixup error:")
traceback.print_exc()
return False
return True