1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{sh COMMAND}
19
20 Spawns a process and executes C{COMMAND}. Occurrences of formatting
21 directives (e.g. C{%s}) will be replaced by input values. Each line
22 of C{stdout} is sent to the output stream. Each line of C{stderr} is
23 handled by the osh stderr handler, (the default handler prints to
24 osh's stderr).
25 """
26
27 import osh.core
28 import osh.error
29 import osh.spawn
30 import osh.util
31
32 Spawn = osh.spawn.Spawn
33 LineOutputConsumer = osh.spawn.LineOutputConsumer
34 remove_crlf = osh.util.remove_crlf
35
36
39
40
42 """Spawns a process and executes C{command}. Occurrences of formatting
43 directives (e.g. C{%s}) will be replaced by input values. Each line
44 of C{stdout} is sent to the output stream. Each line of C{stderr} is
45 handled by the osh stderr handler, (the default handler prints to
46 osh's stderr).
47 """
48 return _Sh().process_args(command)
49
50 -class _Sh(osh.core.Generator):
51
52
53
54 _command = None
55
56
57
58
61
62
63
64
67
69 args = self.args()
70 if args.has_next():
71 self._command = args.next_string()
72 if args.has_next():
73 self.usage()
74 else:
75 self.usage()
76
78 boundCommand = self._bind(object)
79 self._execute_command(boundCommand, object)
80
81
82
84 self._command = command
85 return self
86
87
88
89
91 self._execute_command(self._command, None)
92
93
94
96 command = self._command
97 if type(input) != tuple:
98 input = (input,)
99 for value in input:
100 command = command.replace('%s', str(value), 1)
101 return command
102
104 process = Spawn(command,
105 None,
106 LineOutputConsumer(lambda line: self.send(remove_crlf(line))),
107 LineOutputConsumer(lambda line:
108 osh.error.stderr_handler(line,
109 self,
110 input)))
111 process.run()
112