Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
gajim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Weblate
gajim
Commits
c6c2f157
Commit
c6c2f157
authored
14 years ago
by
Alexander Cherniuk
Browse files
Options
Downloads
Patches
Plain Diff
Added safe /execute and /show commands. May be moved to plugins later
parent
1e468043
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/command_system/implementation/execute.py
+117
-0
117 additions, 0 deletions
src/command_system/implementation/execute.py
src/command_system/implementation/standard.py
+4
-3
4 additions, 3 deletions
src/command_system/implementation/standard.py
with
121 additions
and
3 deletions
src/command_system/implementation/execute.py
0 → 100644
+
117
−
0
View file @
c6c2f157
# 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
from
glib
import
timeout_add
from
..framework
import
CommandContainer
,
command
,
doc
from
hosts
import
*
class
Execute
(
CommandContainer
):
HOSTS
=
ChatCommands
,
PrivateChatCommands
,
GroupChatCommands
DIRECTORY
=
"
~
"
POLL_TIME
=
100
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
)
timeout_add
(
cls
.
POLL_TIME
,
poller
.
next
)
@classmethod
def
poller
(
cls
,
processor
,
popen
):
for
x
in
xrange
(
cls
.
POLL_COUNT
):
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
)
if
stdout
:
processor
.
echo
(
stdout
)
elif
stderr
:
processor
.
echo
(
stderr
)
@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
)
return
text
.
strip
(
strip
)
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
)
if
stdout
:
processor
.
send
(
stdout
)
elif
stderr
:
processor
.
send
(
stderr
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/command_system/implementation/standard.py
+
4
−
3
View file @
c6c2f157
...
...
@@ -30,7 +30,8 @@ from ..errors import CommandError
from
..framework
import
CommandContainer
,
command
,
doc
from
..mapping
import
generate_usage
from
hosts
import
ChatCommands
,
PrivateChatCommands
,
GroupChatCommands
from
hosts
import
*
import
execute
# This holds constants fron the logger, which we'll be using in some of our
# commands.
...
...
@@ -42,7 +43,7 @@ class StandardCommonCommands(CommandContainer):
to all - chat, private chat, group chat.
"""
HOSTS
=
(
ChatCommands
,
PrivateChatCommands
,
GroupChatCommands
)
HOSTS
=
ChatCommands
,
PrivateChatCommands
,
GroupChatCommands
@command
@doc
(
_
(
"
Clear the text window
"
))
...
...
@@ -163,7 +164,7 @@ class StandardCommonChatCommands(CommandContainer):
to a chat and a private chat only.
"""
HOSTS
=
(
ChatCommands
,
PrivateChatCommands
)
HOSTS
=
ChatCommands
,
PrivateChatCommands
@command
@doc
(
_
(
"
Toggle the GPG encryption
"
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment