| 30 | return "'{}'".format(text) |
| 31 | |
| 32 | class ScriptBuilder: |
| 33 | def __init__(self): |
| 34 | self.commands = ['#!/bin/bash', '', 'rm -rf .tmp.output', ''] |
| 35 | |
| 36 | def blank(self): |
| 37 | self.commands.append('') |
| 38 | |
| 39 | def add_command(self, cmd, save=False): |
| 40 | if save: |
| 41 | self.commands.append(cmd + ' 2>&1 | tee .tmp.output') |
| 42 | else: |
| 43 | self.commands.append(cmd) |
| 44 | |
| 45 | def grep(self, text, file=None): |
| 46 | self.add_command("grep -q -F {} {}".format(quote(text), file or '.tmp.output')) |
| 47 | |
| 48 | def check(self, equal_zero=False, result=1): |
| 49 | op = 'eq' if equal_zero else 'ne' |
| 50 | cmds = ['RES=$?', |
| 51 | 'if [ $RES -{} "0" ]; then'.format(op), |
| 52 | ' exit {}'.format(result), |
| 53 | 'fi'] |
| 54 | self.commands.extend(cmds) |
| 55 | |
| 56 | def write(self, p): |
| 57 | write_to(p, self.commands) |
| 58 | make_executable(p) |
| 59 | |
| 60 | def show(self): |
| 61 | print_lines(self.commands) |
| 62 | |
| 63 | |
| 64 | parser = argparse.ArgumentParser() |