/
__main__.py
executable file
·183 lines (150 loc) · 5.86 KB
/
__main__.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
# Copyright (C) 2013 Google Inc.
#
# This file is part of ycmd.
#
# ycmd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ycmd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ycmd. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
# Other imports from `future` must be placed after SetUpPythonPath.
import sys
import os
sys.path.insert( 0, os.path.dirname( os.path.abspath( __file__ ) ) )
from server_utils import SetUpPythonPath, CompatibleWithCurrentCore
SetUpPythonPath()
from future import standard_library
standard_library.install_aliases()
from builtins import * # noqa
import sys
import logging
import json
import argparse
import waitress
import signal
import os
import base64
from ycmd import user_options_store
from ycmd import extra_conf_store
from ycmd import utils
from ycmd.watchdog_plugin import WatchdogPlugin
from ycmd.hmac_plugin import HmacPlugin
from ycmd.utils import ToBytes, ReadFile, OpenForStdHandle
def YcmCoreSanityCheck():
if 'ycm_core' in sys.modules:
raise RuntimeError( 'ycm_core already imported, ycmd has a bug!' )
# We manually call sys.exit() on SIGTERM and SIGINT so that atexit handlers are
# properly executed.
def SetUpSignalHandler( stdout, stderr, keep_logfiles ):
def SignalHandler( signum, frame ):
# We reset stderr & stdout, just in case something tries to use them
if stderr:
tmp = sys.stderr
sys.stderr = sys.__stderr__
tmp.close()
if stdout:
tmp = sys.stdout
sys.stdout = sys.__stdout__
tmp.close()
if not keep_logfiles:
if stderr:
utils.RemoveIfExists( stderr )
if stdout:
utils.RemoveIfExists( stdout )
sys.exit()
for sig in [ signal.SIGTERM,
signal.SIGINT ]:
signal.signal( sig, SignalHandler )
def PossiblyDetachFromTerminal():
# If not on windows, detach from controlling terminal to prevent
# SIGINT from killing us.
if not utils.OnWindows():
try:
os.setsid()
# setsid() can fail if the user started ycmd directly from a shell.
except OSError:
pass
def ParseArguments():
parser = argparse.ArgumentParser()
# Not using 'localhost' on purpose; see #987 and #1130
parser.add_argument( '--host', type = str, default = '127.0.0.1',
help = 'server hostname')
# Default of 0 will make the OS pick a free port for us
parser.add_argument( '--port', type = int, default = 0,
help = 'server port')
parser.add_argument( '--log', type = str, default = 'info',
help = 'log level, one of '
'[debug|info|warning|error|critical]' )
parser.add_argument( '--idle_suicide_seconds', type = int, default = 0,
help = 'num idle seconds before server shuts down')
parser.add_argument( '--options_file', type = str, required = True,
help = 'file with user options, in JSON format' )
parser.add_argument( '--stdout', type = str, default = None,
help = 'optional file to use for stdout' )
parser.add_argument( '--stderr', type = str, default = None,
help = 'optional file to use for stderr' )
parser.add_argument( '--keep_logfiles', action = 'store_true', default = None,
help = 'retain logfiles after the server exits' )
return parser.parse_args()
def SetupLogging( log_level ):
numeric_level = getattr( logging, log_level.upper(), None )
if not isinstance( numeric_level, int ):
raise ValueError( 'Invalid log level: %s' % log_level )
# Has to be called before any call to logging.getLogger()
logging.basicConfig( format = '%(asctime)s - %(levelname)s - %(message)s',
level = numeric_level )
def SetupOptions( options_file ):
options = user_options_store.DefaultOptions()
user_options = json.loads( ReadFile( options_file ) )
options.update( user_options )
utils.RemoveIfExists( options_file )
hmac_secret = ToBytes( base64.b64decode( options[ 'hmac_secret' ] ) )
del options[ 'hmac_secret' ]
user_options_store.SetAll( options )
return options, hmac_secret
def CloseStdin():
sys.stdin.close()
os.close( 0 )
def Main():
args = ParseArguments()
if args.stdout is not None:
sys.stdout = OpenForStdHandle( args.stdout )
if args.stderr is not None:
sys.stderr = OpenForStdHandle( args.stderr )
SetupLogging( args.log )
options, hmac_secret = SetupOptions( args.options_file )
# This ensures that ycm_core is not loaded before extra conf
# preload was run.
YcmCoreSanityCheck()
extra_conf_store.CallGlobalExtraConfYcmCorePreloadIfExists()
code = CompatibleWithCurrentCore()
if code:
sys.exit( code )
PossiblyDetachFromTerminal()
# This can't be a top-level import because it transitively imports
# ycm_core which we want to be imported ONLY after extra conf
# preload has executed.
from ycmd import handlers
handlers.UpdateUserOptions( options )
handlers.SetHmacSecret( hmac_secret )
SetUpSignalHandler( args.stdout, args.stderr, args.keep_logfiles )
handlers.app.install( WatchdogPlugin( args.idle_suicide_seconds ) )
handlers.app.install( HmacPlugin( hmac_secret ) )
CloseStdin()
waitress.serve( handlers.app,
host = args.host,
port = args.port,
threads = 30 )
if __name__ == "__main__":
Main()