-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
240 lines (212 loc) · 8.65 KB
/
__init__.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
# -*- coding: utf-8 -*-
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is ActiveState Software Inc code.
# Portions created by German M. Bravo (Kronuz) are Copyright (C) 2015.
#
# Contributor(s):
# German M. Bravo (Kronuz)
# ActiveState Software Inc
#
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
#
# Mostly based in Komodo Editor's oop-driver.py
# at commit 40ccb140ac73935a63e6455ec39f2b976e33024d
#
from __future__ import absolute_import, unicode_literals, print_function
import os
import sys
import argparse
__version__ = "0.9.3"
__file__ = os.path.normpath(os.path.abspath(__file__))
__path__ = os.path.dirname(__file__)
python_sitelib_path = os.path.normpath(__path__)
if python_sitelib_path not in sys.path:
sys.path.insert(0, python_sitelib_path)
import socket
import logging
class DummyStream(object):
def write(self, message):
pass
def flush(self):
pass
def oop_driver(database_dir, connect=None, log_levels=[], log_file=None, import_path=[]):
"""
Starts OOP CodeIntel driver
Args:
:param database_dir: Directory where CodeIntel database is.
:param connect: Connect using a socket to this 'IP:port'. It can
also be an output file, for example 'stdin'
:param log_levels: List of logger:LEVEL, where logger can be,
for example, "codeintel.db" and level "DEBUG":
['codeintel:WARNING', 'codeintel.db:DEBUG']
:param log_file: File where logs will be written. It can be 'stdout'
or 'stderr', for example
"""
# Don't redirect output
os.environ["KOMODO_VERBOSE"] = "1"
if log_file:
if log_file in ('stdout', 'stderr', '/dev/stdout', '/dev/stderr'):
stream = getattr(sys, log_file)
else:
log_dir = os.path.dirname(log_file)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
stream = open(log_file, 'w', 0)
logging.basicConfig(stream=stream)
# XXX marky horrible ugly hack
sys.stderr = stream
sys.stdout = stream
else:
logging.basicConfig(stream=DummyStream())
logger = logging.getLogger('codeintel')
logger.setLevel(logging.INFO)
for log_level in log_levels:
name, _, level = log_level.rpartition(':')
try:
level = int(level)
except ValueError:
level = getattr(logging, level.upper(), logging.ERROR)
logging.getLogger(name).setLevel(level)
log = logging.getLogger('codeintel.oop.executable')
try:
set_process_limits(log)
except:
log.exception("Failed to set process memory/CPU limits")
try:
set_idle_priority(log)
except:
log.exception("Failed to set process CPU priority")
old_sys_path = set(os.path.abspath(os.path.join(p)) for p in sys.path)
for relpath in import_path:
import_path = os.path.abspath(os.path.join(__path__, relpath))
if import_path not in old_sys_path:
sys.path.append(import_path)
try:
if connect and connect not in ('-', 'stdin', '/dev/stdin'):
if connect.startswith('pipe:'):
pipe_name = connect.split(':', 1)[1]
log.debug("connecting to pipe: %s", pipe_name)
if sys.platform.startswith('win'):
# using Win32 pipes
from win32_named_pipe import Win32Pipe
fd_out = fd_in = Win32Pipe(name=pipe_name, client=True)
else:
# Open the write end first, so the parent doesn't hang
fd_out = open(os.path.join(pipe_name, 'out'), 'wb', 0)
fd_in = open(os.path.join(pipe_name, 'in'), 'rb', 0)
log.debug("opened: %r", fd_in)
else:
host, _, port = connect.partition(':')
port = int(port)
log.debug("connecting to: %s:%s", host, port)
conn = socket.create_connection((host, port))
fd_in = conn.makefile('r+b', 0)
fd_out = fd_in
else:
# force unbuffered stdout
fd_in = sys.stdin
fd_out = os.fdopen(sys.stdout.fileno(), 'wb', 0)
except Exception as ex:
log.exception("Failed to connect with client: %s", ex)
raise
if not os.path.exists(database_dir):
os.makedirs(database_dir)
from codeintel2.oop import Driver
driver = Driver(db_base_dir=database_dir, fd_in=fd_in, fd_out=fd_out)
try:
driver.start()
except KeyboardInterrupt:
pass
def set_idle_priority(log):
"""Attempt to set the process priority to idle"""
try:
os.nice(5)
except AttributeError:
pass # No os.nice on Windows
if sys.platform.startswith('win'):
import ctypes
from ctypes import wintypes
SetPriorityClass = ctypes.windll.kernel32.SetPriorityClass
SetPriorityClass.argtypes = [wintypes.HANDLE, wintypes.DWORD]
SetPriorityClass.restype = wintypes.BOOL
HANDLE_CURRENT_PROCESS = -1
BELOW_NORMAL_PRIORITY_CLASS = 0x00004000
SetPriorityClass(HANDLE_CURRENT_PROCESS, BELOW_NORMAL_PRIORITY_CLASS)
def set_process_limits(log):
import ctypes
if sys.platform.startswith("win"):
"""Pre-allocate (but don't commit) a 1GB chunk of memory to prevent it
from actually being used by codeintel; this acts as a limit on the
amount of memory we can actually use. It has no effects on performance
(since we're only eating address space, not RAM/swap) but helps to
prevent codeintel from blowing up the system.
"""
from ctypes import wintypes
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
VirtualAlloc = kernel32.VirtualAlloc
VirtualAlloc.argtypes = [wintypes.LPVOID, wintypes.ULONG, wintypes.DWORD, wintypes.DWORD]
VirtualAlloc.restype = wintypes.LPVOID
MEM_RESERVE = 0x00002000
MEM_TOP_DOWN = 0x00100000
PAGE_NOACCESS = 0x01
# we can only eat about 1GB; trying for 2GB causes the allocation to
# (harmlessly) fail, which doesn't accomplish our goals
waste = VirtualAlloc(None, 1 << 30, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS)
if waste:
log.debug("Successfullly allocated: %r", waste)
else:
log.debug("Failed to reduce address space: %s",
ctypes.WinError(ctypes.get_last_error()).strerror)
elif sys.platform.startswith("linux"):
import resource
# Limit the oop process to 2GB of memory.
#
# Note that setting to 1GB of memory cause "bk test" failures, showing
# this error:
# Fatal Python error: Couldn't create autoTLSkey mapping
GB = 1 << 30
resource.setrlimit(resource.RLIMIT_AS, (2 * GB, -1L))
else:
# TODO: What to do on the Mac?
pass
def main():
parser = argparse.ArgumentParser()
parser.description = "CodeIntel v%s out-of-process (OOP) driver" % __version__
parser.add_argument("--database-dir", default=os.path.expanduser("~/.codeintel"),
help="The base directory for the codeintel database.")
parser.add_argument("--log-level", action="append", default=[],
help="<log name>:<level> Set log level")
parser.add_argument("--log-file", default=None,
help="The name of the file to log to")
parser.add_argument("--connect", default=None,
help="Connect over TCP instead of using stdin/stdout")
parser.add_argument("--import-path", action="append", default=[""],
help="Paths to add to the Python import path")
args = parser.parse_args()
oop_driver(
database_dir=args.database_dir,
connect=args.connect,
log_levels=args.log_level,
log_file=args.log_file,
import_path=args.import_path
)
if __name__ == '__main__':
try:
main()
except Exception as ex:
print(ex)
finally:
print("Shutting down")