From bf3e2fd37919ac4120948f7c8514d764a891e898 Mon Sep 17 00:00:00 2001 From: Oscar Campos Date: Tue, 30 Aug 2016 10:21:26 +0100 Subject: [PATCH] added encapsulation to return a valid UNIX socket path always, resolves #519 --- anaconda_lib/unix_socket.py | 49 ++++++++++++++++++++++++++++ anaconda_lib/workers/interpreter.py | 13 ++------ anaconda_lib/workers/local_worker.py | 2 +- anaconda_server/jsonserver.py | 22 +++++++------ anaconda_server/lib/path.py | 11 ------- 5 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 anaconda_lib/unix_socket.py diff --git a/anaconda_lib/unix_socket.py b/anaconda_lib/unix_socket.py new file mode 100644 index 00000000..93c3fb54 --- /dev/null +++ b/anaconda_lib/unix_socket.py @@ -0,0 +1,49 @@ + +# Copyright (C) 2013 ~ 2016 - Oscar Campos +# This program is Free Software se LICENSE file for details + +import os +import platform +import tempfile + + +class UnixSocketPath(object): + """Encapsulate logic to handle with paths to UNIX domain sockets + """ + + socketpath = { + 'linux': os.path.join('~', '.local', 'share', 'anaconda', 'run'), + 'darwin': os.path.join( + '~', 'Library', 'Application Support', 'Anaconda') + } + + def __init__(self, project): + self.__project = project + self.__socket_file = os.path.join( + os.path.expanduser( + UnixSocketPath.socketpath.get(platform.system().lower()) + ), + project or 'anaconda', + 'anaconda.sock' + ) + + @property + def socket(self): + """Return back a valid socket path always + """ + + if len(self.__socket_file) < 103: + return self.__socket_file + + socket_path = os.path.join( + tempfile.gettempdir(), + self.__project or 'anaconda', + 'anaconda.sock' + ) + if len(socket_path) > 103: + # probably the project name is crazy long + socket_path = os.path.join( + tempfile.gettempdir(), self.__project[:10], 'anaconda.sock' + ) + + return socket_path diff --git a/anaconda_lib/workers/interpreter.py b/anaconda_lib/workers/interpreter.py index d1f2fbe7..eff2e19c 100644 --- a/anaconda_lib/workers/interpreter.py +++ b/anaconda_lib/workers/interpreter.py @@ -10,6 +10,7 @@ import sublime from ..logger import Log +from ..unix_socket import UnixSocketPath from ..helpers import project_name, debug_enabled from ..helpers import get_settings, active_view, get_interpreter from ..vagrant import VagrantIPAddressGlobal, VagrantMachineGlobalInfo @@ -175,17 +176,7 @@ def __get_unix_domain_socket(self): if sublime.platform() == 'windows': return 'localhost' - socketpath = { - 'linux': os.path.join('~', '.local', 'share', 'anaconda', 'run'), - 'osx': os.path.join( - '~', 'Library', 'Application Support', 'Anaconda'), - } - - return os.path.expanduser(os.path.join( - socketpath[sublime.platform()], - self.project_name, - 'anaconda.sock') - ) + return UnixSocketPath(self.project_name).socket def __parse_raw_interpreter(self): """Parses the raw interpreter string for later simple use diff --git a/anaconda_lib/workers/local_worker.py b/anaconda_lib/workers/local_worker.py index 73605690..b7cb675f 100644 --- a/anaconda_lib/workers/local_worker.py +++ b/anaconda_lib/workers/local_worker.py @@ -31,7 +31,7 @@ def check(self): return False start = time.time() - while not self._status(0.05): + while not self._status(0.10): if time.time() - start >= 1: # 1s return False time.sleep(0.1) diff --git a/anaconda_server/jsonserver.py b/anaconda_server/jsonserver.py index 3d5affc6..99f9c310 100644 --- a/anaconda_server/jsonserver.py +++ b/anaconda_server/jsonserver.py @@ -25,11 +25,12 @@ sys.path.insert(0, os.path.join( os.path.split(os.path.split(__file__)[0])[0], 'anaconda_lib')) +from lib.path import log_directory from lib.contexts import json_decode +from unix_socket import UnixSocketPath from handlers import ANACONDA_HANDLERS from jedi import settings as jedi_settings from lib.anaconda_handler import AnacondaHandler -from lib.path import log_directory, socket_directory DEBUG_MODE = True @@ -161,7 +162,9 @@ def handle_accept(self): """Called when we accept and incomming connection """ sock, addr = self.accept() - self.logger.info('Incomming connection from {0}'.format(repr(addr))) + self.logger.info('Incomming connection from {0}'.format( + repr(addr) or 'unix socket') + ) self.handler(sock, self) def handle_close(self): @@ -335,18 +338,17 @@ def get_log_traceback(): if WINDOWS: server = JSONServer(('localhost', port)) else: - socket_directory = os.path.join(socket_directory, options.project or 'anaconda') # noqa - if not os.path.exists(socket_directory): - os.makedirs(socket_directory) - unix_domain_socket = os.path.join(socket_directory, 'anaconda.sock') # noqa - if os.path.exists(unix_domain_socket): - os.unlink(unix_domain_socket) - server = JSONServer(unix_domain_socket) + unix_socket_path = UnixSocketPath(options.project) + if not os.path.exists(unix_socket_path.socket): + os.makedirs(os.path.dirname(unix_socket_path.socket)) + if os.path.exists(unix_socket_path.socket): + os.unlink(unix_socket_path.socket) + server = JSONServer(unix_socket_path.socket) logger.info( 'Anaconda Server started in {0} for ' 'PID {1} with cache dir {2}{3}'.format( - port or unix_domain_socket, PID, + port or unix_socket_path.socket, PID, jedi_settings.cache_directory, ' and extra paths {0}'.format( options.extra_paths diff --git a/anaconda_server/lib/path.py b/anaconda_server/lib/path.py index a407c28d..67507bf3 100644 --- a/anaconda_server/lib/path.py +++ b/anaconda_server/lib/path.py @@ -14,14 +14,3 @@ log_directory = os.path.expanduser( logpath.get(platform.system().lower()) ) - -socketpath = { - 'linux': os.path.join('~', '.local', 'share', 'anaconda', 'run'), - 'darwin': os.path.join('~', 'Library', 'Application Support', 'Anaconda'), -} - -socket_directory = None -if platform.system().lower() != 'windows': - socket_directory = os.path.expanduser( - socketpath.get(platform.system().lower()) - )