Skip to content

Commit

Permalink
added encapsulation to return a valid UNIX socket path always, resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnWidget committed Aug 30, 2016
1 parent 5462f7c commit bf3e2fd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 33 deletions.
49 changes: 49 additions & 0 deletions anaconda_lib/unix_socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# Copyright (C) 2013 ~ 2016 - Oscar Campos <[email protected]>
# 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
13 changes: 2 additions & 11 deletions anaconda_lib/workers/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion anaconda_lib/workers/local_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions anaconda_server/jsonserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions anaconda_server/lib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
)

0 comments on commit bf3e2fd

Please sign in to comment.