mirror of
https://github.com/nvbn/thefuck.git
synced 2025-04-15 07:10:52 +01:00
Setup data dir and fix zombie process detection
This commit is contained in:
parent
66118f0b81
commit
7c8936d965
@ -19,6 +19,7 @@ class Settings(dict):
|
||||
from .logs import exception
|
||||
|
||||
self._setup_user_dir()
|
||||
self._setup_data_dir()
|
||||
self._init_settings_file()
|
||||
|
||||
try:
|
||||
@ -64,6 +65,18 @@ class Settings(dict):
|
||||
rules_dir.mkdir(parents=True)
|
||||
self.user_dir = user_dir
|
||||
|
||||
def _get_user_data_path(self):
|
||||
"""Return Path object representing the user local resource"""
|
||||
xdg_config_home = os.environ.get('XDG_DATA_HOME', '~/.local/share')
|
||||
user_dir = Path(xdg_config_home, 'thefuck').expanduser()
|
||||
return user_dir
|
||||
|
||||
def _setup_data_dir(self):
|
||||
data_dir = self._get_user_data_path()
|
||||
if not data_dir.is_dir():
|
||||
data_dir.mkdir(parents=True)
|
||||
self.data_dir = data_dir
|
||||
|
||||
def _settings_from_file(self):
|
||||
"""Loads settings from file."""
|
||||
settings = load_source(
|
||||
|
@ -28,6 +28,10 @@ ALL_ENABLED = _GenConst('All rules enabled')
|
||||
DEFAULT_RULES = [ALL_ENABLED]
|
||||
DEFAULT_PRIORITY = 1000
|
||||
|
||||
SHELL_LOGGER_SOCKET_ENV_VAR = '__SHELL_LOGGER_SOCKET'
|
||||
SHELL_LOGGER_SOCKET_PATH = '/tmp/tf_socket'
|
||||
SHELL_LOGGER_BINARY_FILENAME = 'shell_logger'
|
||||
|
||||
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
||||
'exclude_rules': [],
|
||||
'wait_command': 3,
|
||||
@ -42,8 +46,7 @@ DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
||||
'./gradlew', 'vagrant'],
|
||||
'repeat': False,
|
||||
'instant_mode': False,
|
||||
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1', '__SHELL_LOGGER_SOCKET': '/tmp/tf_socket',
|
||||
'__SHELL_LOGGER_BINARY_PATH': '$HOME/.local/bin/shell_logger'}}
|
||||
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}}
|
||||
|
||||
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
|
||||
'THEFUCK_EXCLUDE_RULES': 'exclude_rules',
|
||||
|
@ -1,11 +1,14 @@
|
||||
import subprocess
|
||||
import urllib.request
|
||||
|
||||
import os
|
||||
import sys
|
||||
import six
|
||||
import psutil
|
||||
from psutil import ZombieProcess
|
||||
|
||||
from ..conf import settings
|
||||
from ..const import SHELL_LOGGER_SOCKET_ENV_VAR, SHELL_LOGGER_SOCKET_PATH, SHELL_LOGGER_BINARY_FILENAME
|
||||
from ..logs import warn, debug
|
||||
from ..shells import shell
|
||||
from ..utils import which
|
||||
@ -33,23 +36,27 @@ def print_alias(known_args):
|
||||
print(_get_alias(known_args))
|
||||
|
||||
|
||||
def print_experimental_shell_history():
|
||||
def print_experimental_shell_history(known_args):
|
||||
settings.init(known_args)
|
||||
|
||||
filename_suffix = sys.platform
|
||||
client_release = 'https://www.dropbox.com/s/m0jqp8i4c6woko5/client?dl=1'
|
||||
filename = settings.env['__SHELL_LOGGER_BINARY_PATH']
|
||||
binary_path = '{}/{}'.format(settings.data_dir, SHELL_LOGGER_BINARY_FILENAME)
|
||||
|
||||
debug('Downloading the shell_logger release and putting it in the path ... ')
|
||||
urllib.request.urlretrieve(client_release, filename)
|
||||
urllib.request.urlretrieve(client_release, binary_path)
|
||||
|
||||
subprocess.Popen(['chmod', '+x', filename])
|
||||
subprocess.Popen(['chmod', '+x', binary_path])
|
||||
|
||||
proc = subprocess.Popen(['./{0}'.format(filename), '-mode', 'configure'], stdout=subprocess.PIPE,
|
||||
env={'__SHELL_LOGGER_BINARY_PATH': settings.env['__SHELL_LOGGER_BINARY_PATH']})
|
||||
proc = subprocess.Popen([binary_path, '-mode', 'configure'], stdout=subprocess.PIPE)
|
||||
print(''.join([line.decode() for line in proc.stdout.readlines()]))
|
||||
|
||||
# If process is running, close it
|
||||
if filename in (p.name() for p in psutil.process_iter()):
|
||||
subprocess.Popen(['./{0}'.format(filename), '-mode', 'daemon'])
|
||||
subprocess.Popen(['rm', './{0}'.format(filename)])
|
||||
try:
|
||||
# If process is not running, start the process
|
||||
if SHELL_LOGGER_BINARY_FILENAME not in (p.name() for p in psutil.process_iter()):
|
||||
os.spawnve(os.P_NOWAIT, binary_path, [binary_path, '-mode', 'daemon'],
|
||||
env={SHELL_LOGGER_SOCKET_ENV_VAR: SHELL_LOGGER_SOCKET_PATH})
|
||||
except ZombieProcess as e:
|
||||
warn("Zombie process is running. Please kill the running process " % e)
|
||||
|
||||
|
||||
subprocess.Popen(['./{0}'.format(filename), '-mode', 'daemon'],
|
||||
env={'__SHELL_LOGGER_SOCKET': settings.env['__SHELL_LOGGER_SOCKET']})
|
||||
|
@ -33,7 +33,7 @@ def main():
|
||||
else:
|
||||
shell_logger(known_args.shell_logger)
|
||||
elif known_args.enable_experimental_shell_history:
|
||||
print_experimental_shell_history()
|
||||
print_experimental_shell_history(known_args)
|
||||
else:
|
||||
parser.print_usage()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user