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

Source Code for Module osh.command.sh

  1  # osh 
  2  # Copyright (C) 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 # remote compile-time interface 82
83 - def setCommand(self, command):
84 self._command = command 85 return self
86 87 88 # Generator interface 89
90 - def execute(self):
91 self._execute_command(self._command, None)
92 93 # For use by this class 94
95 - def _bind(self, input):
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
103 - def _execute_command(self, command, input):
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