def test_wrap(self): input = 'Neither mother, father, nor any other relative can do\n' + \ 'one greater good than one\'s own well-directed mind.' output = ' Neither mother, father, nor any other\n' + \ ' relative can do\n' + \ ' one greater good than one\'s own\n' + \ ' well-directed mind.' result = util.wrap(input, width=40, indent=3) self.assertEqual(output, result)
def run(command, echo=False, fg=False): """Runs command in a subprocess. If echo is True, then the command is echoed to screen. If fg is True, then command is run in the foreground. """ if not isinstance(command, plumbum.commands.BaseCommand): command = local['bash']['-c', str(command)] # Code to deal with a bug in plumbum's str(command)... def flatten_list(lst): result = [] for el in lst: if isinstance(el, (list, tuple)): result += flatten_list(el) else: result.append(el) return result command_string = ' '.join(flatten_list(command.formulate())) if echo: notice('Command:') notice(util.wrap(command_string, indent=4)) kwargs = {'retcode': None} if fg: kwargs.update(stdin=None, stdout=None, stderr=None) retcode, stdout, stderr = command.run(**kwargs) if not echo and (stderr or retcode != 0): warning('Command:') warning(util.wrap(command_string, indent=4)) if stderr: warning('Stderr:') warning(util.wrap(stderr, indent=4)) if retcode != 0: warning('Command exited with return code {}'.format(retcode)) warning('Exiting...') sys.exit(retcode)