Package osh :: Package command :: Module sh
[frames] | no frames]

Source Code for Module osh.command.sh

  1  # osh 
  2  # Copyright (C) 2005 Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 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  # CLI 
37 -def _sh():
38 return _Sh()
39 40 # API
41 -def sh(command):
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 # state 53 54 _command = None 55 56 57 # object interface 58
59 - def __init__(self):
60 osh.core.Generator.__init__(self, '', (1, 1))
61 62 63 # BaseOp interface 64
65 - def doc(self):
66 return __doc__
67
68 - def setup(self):
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
77 - def receive(self, object):
78 boundCommand = self._bind(object) 79 self._execute_command(boundCommand, object)
80
81 - def receive_complete(self):
82 self.send_complete()
83 84 # remote compile-time interface 85
86 - def setCommand(self, command):
87 self._command = command 88 return self
89 90 91 # Generator interface 92
93 - def execute(self):
94 self._execute_command(self._command, None) 95 self.send_complete()
96 97 # For use by this class 98
99 - def _bind(self, input):
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
107 - def _execute_command(self, command, input):
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