Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
#!/usr/bin/env python
# This file is part of HDL Checker.
#
# Copyright (c) 2015 - 2019 suoto (Andre Souto)
#
# HDL Checker 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.
#
# HDL Checker 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 HDL Checker. If not, see <http://www.gnu.org/licenses/>.
"HDL Checker server launcher"
# PYTHON_ARGCOMPLETE_OK
import argparse
import logging
import os
import sys
from threading import Timer
from hdl_checker import __version__ as version
from hdl_checker import lsp
from hdl_checker.utils import (
getTemporaryFilename,
isProcessRunning,
setupLogging,
terminateProcess,
)
_logger = logging.getLogger(__name__)
def parseArguments():
"Argument parser for standalone hdl_checker"
parser = argparse.ArgumentParser()
parser.add_argument(
"--attach-to-pid",
action="store",
type=int,
help="Stops the server if given PID is not active",
)
parser.add_argument("--log-level", action="store", help="Logging level")
parser.add_argument(
"--log-stream",
action="store",
help="Log file, defaults to a temporary file named "
"hdl_checker_log_pid<PID>.log. Use NONE to disable logging altogether",
)
parser.add_argument(
"--stderr",
action="store",
help="File to redirect stderr to. Defaults to a temporary file "
"named hdl_checker_stderr_pid<PID>.log. "
"Use NONE to disable redirecting stderr altogether",
)
parser.add_argument(
"--version",
"-V",
action="store_true",
help="Prints hdl_checker version and exit",
)
try:
import argcomplete # type: ignore
argcomplete.autocomplete(parser)
except ImportError: # pragma: no cover
pass
args = parser.parse_args()
if args.version:
sys.stdout.write("%s\n" % version)
sys.exit(0)
# If not set, create a temporary file safely so there's no clashes
if args.log_stream == "NONE":
args.log_stream = None
else:
args.log_stream = args.log_stream or getTemporaryFilename("log")
if args.stderr == "NONE":
args.stderr = None
else:
args.stderr = args.stderr or getTemporaryFilename("stderr")
args.log_level = args.log_level or logging.INFO
return args
# Copied from ycmd!
def openForStdHandle(filepath):
"""
Returns a file object that can be used to replace sys.stdout or
sys.stderr
"""
return open(filepath, mode="w", buffering=1)
def _setupPipeRedirection(stderr): # pragma: no cover
"Redirect stderr to a file"
if stderr is not None:
sys.stderr = openForStdHandle(stderr)
def _binaryStdio(): # pragma: no cover
"""
(from https://github.com/palantir/python-language-server)
"""
# pylint: disable=no-member
return sys.stdin.buffer, sys.stdout.buffer
def run(args):
"""
Starts the hdl_checker LSP server
"""
_setupPipeRedirection(args.stderr)
if args.log_stream:
setupLogging(args.log_stream, args.log_level)
globals()["_logger"] = logging.getLogger(__name__)
def _attachPids(source_pid, target_pid):
"Monitors if source_pid is alive. If not, terminate target_pid"
def _watchPidWrapper():
"PID attachment monitor"
try:
if isProcessRunning(source_pid):
Timer(1, _watchPidWrapper).start()
else:
_logger.warning("Process %d is not running anymore", source_pid)
terminateProcess(target_pid)
except (TypeError, AttributeError): # pragma: no cover
return
_logger.debug("Setting up PID attachment from %s to %s", source_pid, target_pid)
Timer(2, _watchPidWrapper).start()
_logger.info(
"Starting server. Our PID is %s, %s. Version string for hdl_checker is '%s'",
os.getpid(),
"no parent PID to attach to"
if args.attach_to_pid is None
else "our parent is %s" % args.attach_to_pid,
version,
)
if args.attach_to_pid is not None:
_attachPids(args.attach_to_pid, os.getpid())
stdin, stdout = _binaryStdio()
server = lsp.HdlCheckerLanguageServer()
lsp.setupLanguageServerFeatures(server)
server.start_io(stdin=stdin, stdout=stdout)
def main():
return run(parseArguments())
if __name__ == "__main__":
main()
f82552317This file is pinned to the selected release. Download the original to open it in your design tool.