This repository was archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshellout.py
More file actions
127 lines (95 loc) · 3.77 KB
/
shellout.py
File metadata and controls
127 lines (95 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright (c) 2009 James Bowes <jbowes@dangerouslyinc.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
shellout provides an OO-like interface to running shell commands.
Example::
import shellout as so
print so.echo("hello, world")
"""
import sys
import types
class ShellOutQuoter(object):
def _shell_safe_string(self, *args):
def quote(s):
s = s.replace('\\', '\\\\') # escape backslashes
s = s.replace('"', '\\"') # escape double quotes
return '"%s"' % s
return ' '.join(map(quote, args))
class ShellError(OSError):
def __init__(self, command, exit_code, output):
self.command = command
self.exit_code = exit_code
self.output = output
def __str__(self):
return "Command '%s' failed with exit code %s:\n%s" % (
self.command,
self.exit_code,
self.output)
class ShellOutArg(ShellOutQuoter):
def __init__(self, cmd_string, arg_name):
self._cmd_string = cmd_string
self._arg_value = None
if len(arg_name) == 1:
self._arg = "-" + arg_name
self._arg_fmt_string = self._arg + " %s"
else:
self._arg = "--" + arg_name
self._arg_fmt_string = self._arg + "=%s"
def _set_arg_value(self, arg):
if self._arg_value is not None:
raise ValueError("option %s already set to \"%s\"" % self._arg)
self._arg_value = arg
self._arg = self._arg_fmt_string % self._shell_safe_string(arg)
def __getattr__(self, x):
return self.__class__(str(self), x)
def __getitem__(self, arg):
self._set_arg_value(arg)
return self
def __str__(self):
return self._cmd_string + " " + self._arg
def __call__(self, *args):
import commands
cmd = str(self)
if len(args) > 0:
cmd += " " + self._shell_safe_string(*args)
return commands.getoutput(cmd)
class ShellOutCommand(ShellOutQuoter):
_soa = ShellOutArg
ShellError = ShellError
def __init__(self, cmd):
self._cmd = cmd
def __getattr__(self, x):
return self._soa(self._cmd, x)
def __call__(self, *args):
import commands
to_run = self._cmd + ' ' + self._shell_safe_string(*args)
results = commands.getstatusoutput(to_run)
if results[0] != 0:
raise self.ShellError(to_run, *results)
return results[1]
class ShellOutModule(types.ModuleType):
# Hack this in so we can still describe the module at the top of the file
__doc__ = sys.modules[__name__].__doc__
def __init__(self):
self._soc = ShellOutCommand
self._soa = ShellOutArg
def __getattr__(self, x):
return self._soc(x)
sys.modules[__name__] = ShellOutModule()