| 1 | # -*- Python -*- -*- coding: iso-8859-1 -*- |
|---|
| 2 | # :Progetto: Bice -- Sync CVS->SVN |
|---|
| 3 | # :Creato: sab 10 apr 2004 16:43:48 CEST |
|---|
| 4 | # :Autore: Lele Gaifax <lele@nautilus.homeip.net> |
|---|
| 5 | # |
|---|
| 6 | |
|---|
| 7 | __docformat__ = 'reStructuredText' |
|---|
| 8 | |
|---|
| 9 | from StringIO import StringIO |
|---|
| 10 | from sys import stderr |
|---|
| 11 | |
|---|
| 12 | class VerboseStringIO(StringIO): |
|---|
| 13 | |
|---|
| 14 | def write(self, data): |
|---|
| 15 | """Give a feedback to the user.""" |
|---|
| 16 | |
|---|
| 17 | StringIO.write(self, data) |
|---|
| 18 | stderr.write('.'*data.count('\n')) |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class SystemCommand(object): |
|---|
| 22 | """Wrap a single command to be executed by the shell.""" |
|---|
| 23 | |
|---|
| 24 | COMMAND = None |
|---|
| 25 | """The default command for this class. Must be redefined by subclasses.""" |
|---|
| 26 | |
|---|
| 27 | VERBOSE = True |
|---|
| 28 | """Print the executed command on stderr, at each run.""" |
|---|
| 29 | |
|---|
| 30 | def __init__(self, command=None, working_dir=None): |
|---|
| 31 | """Initialize a SystemCommand instance, specifying the command |
|---|
| 32 | to be executed and eventually the working directory.""" |
|---|
| 33 | |
|---|
| 34 | self.command = command or self.COMMAND |
|---|
| 35 | """The command to be executed.""" |
|---|
| 36 | |
|---|
| 37 | self.working_dir = working_dir |
|---|
| 38 | """The working directory, go there before execution.""" |
|---|
| 39 | |
|---|
| 40 | self.exit_status = None |
|---|
| 41 | """Once the command has been executed, this is its exit status.""" |
|---|
| 42 | |
|---|
| 43 | def __call__(self, output=None, input=None, dry_run=False, **kwargs): |
|---|
| 44 | """Execute the command.""" |
|---|
| 45 | |
|---|
| 46 | from os import system, popen, popen2, wait, chdir |
|---|
| 47 | from shutil import copyfileobj |
|---|
| 48 | |
|---|
| 49 | wdir = self.working_dir or kwargs.get('working_dir') |
|---|
| 50 | if wdir: |
|---|
| 51 | chdir(wdir) |
|---|
| 52 | |
|---|
| 53 | command = self.command % kwargs |
|---|
| 54 | if self.VERBOSE: |
|---|
| 55 | stderr.write("%s " % command) |
|---|
| 56 | |
|---|
| 57 | if dry_run: |
|---|
| 58 | if self.VERBOSE: |
|---|
| 59 | stderr.write(" [dry run]\n") |
|---|
| 60 | return |
|---|
| 61 | |
|---|
| 62 | if output: |
|---|
| 63 | if output is True: |
|---|
| 64 | if self.VERBOSE: |
|---|
| 65 | output = VerboseStringIO() |
|---|
| 66 | else: |
|---|
| 67 | output = StringIO() |
|---|
| 68 | |
|---|
| 69 | if input: |
|---|
| 70 | inp, out = popen2(command) |
|---|
| 71 | inp.write(input) |
|---|
| 72 | inp.close() |
|---|
| 73 | else: |
|---|
| 74 | out = popen(command) |
|---|
| 75 | |
|---|
| 76 | copyfileobj(out, output, length=128) |
|---|
| 77 | output.seek(0) |
|---|
| 78 | |
|---|
| 79 | if input: |
|---|
| 80 | self.exit_status = wait()[1] |
|---|
| 81 | else: |
|---|
| 82 | self.exit_status = out.close() |
|---|
| 83 | else: |
|---|
| 84 | if input: |
|---|
| 85 | inp, out = popen2(command) |
|---|
| 86 | inp.write(input) |
|---|
| 87 | inp.close() |
|---|
| 88 | out.close() |
|---|
| 89 | self.exit_status = wait()[1] |
|---|
| 90 | else: |
|---|
| 91 | self.exit_status = system(command) |
|---|
| 92 | |
|---|
| 93 | if self.VERBOSE: |
|---|
| 94 | if not self.exit_status: |
|---|
| 95 | stderr.write(" [Ok]\n") |
|---|
| 96 | else: |
|---|
| 97 | stderr.write(" [Error %s]\n" % self.exit_status) |
|---|
| 98 | |
|---|
| 99 | return output |
|---|
| 100 | |
|---|