2013-09-20 20:24:34 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2014-01-13 14:08:43 -05:00
|
|
|
# Copyright (C) 2013 Google Inc.
|
2013-09-20 20:24:34 -04:00
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe 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.
|
|
|
|
#
|
|
|
|
# YouCompleteMe 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 YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-10-14 18:29:00 -04:00
|
|
|
from server_utils import SetUpPythonPath
|
|
|
|
SetUpPythonPath()
|
2013-10-01 19:21:17 -04:00
|
|
|
|
2013-10-14 18:29:00 -04:00
|
|
|
import sys
|
2013-09-20 20:24:34 -04:00
|
|
|
import logging
|
|
|
|
import json
|
2013-10-08 19:21:43 -04:00
|
|
|
import argparse
|
2013-10-11 23:12:04 -04:00
|
|
|
import waitress
|
2013-10-17 15:21:59 -04:00
|
|
|
import signal
|
2014-03-02 01:14:14 -05:00
|
|
|
import os
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm import user_options_store
|
2013-10-08 19:21:43 -04:00
|
|
|
from ycm import extra_conf_store
|
2013-10-23 14:02:25 -04:00
|
|
|
from ycm.server.watchdog_plugin import WatchdogPlugin
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2013-09-27 19:20:35 -04:00
|
|
|
|
2013-10-14 18:29:00 -04:00
|
|
|
def YcmCoreSanityCheck():
|
|
|
|
if 'ycm_core' in sys.modules:
|
|
|
|
raise RuntimeError( 'ycm_core already imported, ycmd has a bug!' )
|
2013-09-24 17:00:27 -04:00
|
|
|
|
|
|
|
|
2013-10-18 01:12:28 -04:00
|
|
|
# We manually call sys.exit() on SIGTERM and SIGINT so that atexit handlers are
|
|
|
|
# properly executed.
|
2014-03-02 01:14:14 -05:00
|
|
|
def SetUpSignalHandler(stdout, stderr, keep_logfiles):
|
2013-10-17 23:14:56 -04:00
|
|
|
def SignalHandler( signum, frame ):
|
2014-03-02 01:14:14 -05:00
|
|
|
if stderr:
|
|
|
|
# Reset stderr, just in case something tries to use it
|
|
|
|
tmp = sys.stderr
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
tmp.close()
|
|
|
|
if stdout:
|
|
|
|
# Reset stdout, just in case something tries to use it
|
|
|
|
tmp = sys.stdout
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
tmp.close()
|
|
|
|
|
|
|
|
if not keep_logfiles:
|
|
|
|
if stderr:
|
|
|
|
utils.RemoveIfExists( stderr )
|
|
|
|
if stdout:
|
|
|
|
utils.RemoveIfExists( stdout )
|
|
|
|
|
2013-10-18 01:12:28 -04:00
|
|
|
sys.exit()
|
2013-10-17 15:21:59 -04:00
|
|
|
|
|
|
|
for sig in [ signal.SIGTERM,
|
2013-10-17 23:16:44 -04:00
|
|
|
signal.SIGINT ]:
|
2013-10-17 15:21:59 -04:00
|
|
|
signal.signal( sig, SignalHandler )
|
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
def Main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument( '--host', type = str, default = 'localhost',
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'server hostname')
|
2013-10-25 16:11:13 -04:00
|
|
|
# Default of 0 will make the OS pick a free port for us
|
|
|
|
parser.add_argument( '--port', type = int, default = 0,
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'server port')
|
2013-09-20 20:24:34 -04:00
|
|
|
parser.add_argument( '--log', type = str, default = 'info',
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'log level, one of '
|
|
|
|
'[debug|info|warning|error|critical]' )
|
2013-10-23 15:33:27 -04:00
|
|
|
parser.add_argument( '--idle_suicide_seconds', type = int, default = 0,
|
2013-10-23 14:02:25 -04:00
|
|
|
help = 'num idle seconds before server shuts down')
|
2013-09-24 17:00:27 -04:00
|
|
|
parser.add_argument( '--options_file', type = str, default = '',
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'file with user options, in JSON format' )
|
2014-03-02 01:14:14 -05:00
|
|
|
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 = 'optional file to use for stderr' )
|
2013-09-20 20:24:34 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2014-03-02 01:14:14 -05:00
|
|
|
if args.stdout is not None:
|
|
|
|
sys.stdout = open(args.stdout, "wb")
|
|
|
|
#os.dup2(stdout.fileno(), sys.stdout.fileno())
|
|
|
|
if args.stderr is not None:
|
|
|
|
sys.stderr = open(args.stderr, "wb")
|
|
|
|
#os.dup2(stderr.fileno(), sys.stderr.fileno())
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
numeric_level = getattr( logging, args.log.upper(), None )
|
|
|
|
if not isinstance( numeric_level, int ):
|
|
|
|
raise ValueError( 'Invalid log level: %s' % args.log )
|
|
|
|
|
2013-10-14 18:29:00 -04:00
|
|
|
# Has to be called before any call to logging.getLogger()
|
2013-09-20 20:24:34 -04:00
|
|
|
logging.basicConfig( format = '%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
level = numeric_level )
|
|
|
|
|
2013-10-25 16:26:33 -04:00
|
|
|
options = ( json.load( open( args.options_file, 'r' ) )
|
|
|
|
if args.options_file
|
|
|
|
else user_options_store.DefaultOptions() )
|
|
|
|
user_options_store.SetAll( options )
|
|
|
|
|
|
|
|
# This ensures that ycm_core is not loaded before extra conf
|
|
|
|
# preload was run.
|
|
|
|
YcmCoreSanityCheck()
|
|
|
|
extra_conf_store.CallGlobalExtraConfYcmCorePreloadIfExists()
|
2013-10-14 18:29:00 -04:00
|
|
|
|
2013-10-15 14:19:56 -04:00
|
|
|
# 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.
|
2013-10-23 14:02:25 -04:00
|
|
|
from ycm.server import handlers
|
2013-10-14 18:29:00 -04:00
|
|
|
handlers.UpdateUserOptions( options )
|
2014-03-02 01:14:14 -05:00
|
|
|
SetUpSignalHandler(args.stdout, args.stderr, args.keep_logfiles)
|
2013-10-23 15:33:27 -04:00
|
|
|
handlers.app.install( WatchdogPlugin( args.idle_suicide_seconds ) )
|
2013-10-14 18:29:00 -04:00
|
|
|
waitress.serve( handlers.app,
|
|
|
|
host = args.host,
|
|
|
|
port = args.port,
|
2014-02-07 15:46:45 -05:00
|
|
|
threads = 30 )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()
|
|
|
|
|