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
83
84
85
87 self._command = command
88 return self
89
90
91
92
94 self._execute_command(self._command, None)
95 self.send_complete()
96
97
98
100 command = self._command
101 if type(input) != tuple:
102 input = (input,)
103 for value in input:
104 command = command.replace('%s', str(value), 1)
105 return command
106
108 process = Spawn(command,
109 None,
110 LineOutputConsumer(lambda line: self.send(remove_crlf(line))),
111 LineOutputConsumer(lambda line: osh.error.stderr_handler(line, self, input)))
112 process.run()
113