2010-08-04 10:08:24 +02:00
|
|
|
# Copyright (c) 2010, Alexander Cherniuk (ts33kr@gmail.com)
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Provides facilities to safely execute expressions inside a shell process
|
|
|
|
and capture the resulting output, in an asynchronous fashion, avoiding
|
|
|
|
deadlocks. If the process execution time reaches the threshold - it is
|
|
|
|
forced to terminate. Consists of a tiny framework and a couple of
|
|
|
|
commands as a frontend.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
from os.path import expanduser
|
|
|
|
|
2013-07-29 18:35:49 +02:00
|
|
|
from gi.repository import GLib
|
2010-08-04 10:08:24 +02:00
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from gajim.common.i18n import _
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.command_system.framework import CommandContainer, command, doc
|
|
|
|
from gajim.command_system.implementation.hosts import ChatCommands, PrivateChatCommands, GroupChatCommands
|
2010-08-04 10:08:24 +02:00
|
|
|
|
|
|
|
class Execute(CommandContainer):
|
2011-10-08 21:54:05 +02:00
|
|
|
AUTOMATIC = True
|
2010-08-04 10:08:24 +02:00
|
|
|
HOSTS = ChatCommands, PrivateChatCommands, GroupChatCommands
|
|
|
|
|
|
|
|
DIRECTORY = "~"
|
|
|
|
|
2010-08-04 10:37:01 +02:00
|
|
|
POLL_INTERVAL = 100
|
2010-08-04 10:08:24 +02:00
|
|
|
POLL_COUNT = 5
|
|
|
|
|
|
|
|
@command("exec", raw=True)
|
|
|
|
@doc(_("Execute expression inside a shell, show output"))
|
|
|
|
def execute(self, expression):
|
|
|
|
Execute.spawn(self, expression)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def spawn(cls, processor, expression):
|
|
|
|
pipes = dict(stdout=PIPE, stderr=PIPE)
|
|
|
|
directory = expanduser(cls.DIRECTORY)
|
|
|
|
popen = Popen(expression, shell=True, cwd=directory, **pipes)
|
|
|
|
cls.monitor(processor, popen)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def monitor(cls, processor, popen):
|
|
|
|
poller = cls.poller(processor, popen)
|
2013-07-29 18:35:49 +02:00
|
|
|
GLib.timeout_add(cls.POLL_INTERVAL, next, poller)
|
2010-08-04 10:08:24 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poller(cls, processor, popen):
|
2017-02-07 21:05:27 +01:00
|
|
|
for _ in range(cls.POLL_COUNT):
|
2010-08-04 10:08:24 +02:00
|
|
|
yield cls.brush(processor, popen)
|
|
|
|
cls.overdue(processor, popen)
|
|
|
|
yield False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def brush(cls, processor, popen):
|
|
|
|
if popen.poll() is not None:
|
|
|
|
cls.terminated(processor, popen)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def terminated(cls, processor, popen):
|
|
|
|
stdout, stderr = cls.fetch(popen)
|
2010-08-06 14:36:30 +02:00
|
|
|
success = popen.returncode == 0
|
|
|
|
if success and stdout:
|
2010-08-04 10:08:24 +02:00
|
|
|
processor.echo(stdout)
|
2010-08-06 14:36:30 +02:00
|
|
|
elif not success and stderr:
|
|
|
|
processor.echo_error(stderr)
|
2010-08-04 10:08:24 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def overdue(cls, processor, popen):
|
|
|
|
popen.terminate()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fetch(cls, popen):
|
|
|
|
data = popen.communicate()
|
|
|
|
return map(cls.clean, data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def clean(text):
|
|
|
|
strip = chr(10) + chr(32)
|
2013-05-06 21:25:49 +02:00
|
|
|
return text.decode().strip(strip)
|
2010-08-04 10:08:24 +02:00
|
|
|
|
|
|
|
class Show(Execute):
|
|
|
|
|
|
|
|
@command("sh", raw=True)
|
|
|
|
@doc(_("Execute expression inside a shell, send output"))
|
|
|
|
def show(self, expression):
|
|
|
|
Show.spawn(self, expression)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def terminated(cls, processor, popen):
|
|
|
|
stdout, stderr = cls.fetch(popen)
|
2010-08-06 14:36:30 +02:00
|
|
|
success = popen.returncode == 0
|
|
|
|
if success and stdout:
|
2010-08-04 10:08:24 +02:00
|
|
|
processor.send(stdout)
|
2010-08-06 14:36:30 +02:00
|
|
|
elif not success and stderr:
|
2011-10-05 13:37:40 +02:00
|
|
|
processor.echo_error(stderr)
|