-
-
Save reinholdsson/e5cde1bb22c8b61a5ab93b791c5099a5 to your computer and use it in GitHub Desktop.
shellout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shellout(template, **kwargs): | |
""" | |
Takes a shell command template and executes it. The template must use | |
the new (2.6+) format mini language. `kwargs` must contain any defined | |
placeholder, only `output` is optional. | |
Raises RuntimeError on nonzero exit codes. | |
Simple template: | |
wc -l < {input} > {output} | |
Quoted curly braces: | |
ps ax|awk '{{print $1}}' > {output} | |
Usage with luigi: | |
... | |
tmp = shellout('wc -l < {input} > {output}', input=self.input().fn) | |
luigi.File(tmp).move(self.output.fn()) | |
.... | |
""" | |
preserve_spaces = kwargs.get('preserve_spaces', False) | |
stopover = random_tmp_path() # just returns a random path string, e.g. /tmp/asdas8d90a8s9f8d | |
if not 'output' in kwargs: | |
kwargs.update({'output': stopover}) | |
command = template.format(**kwargs) | |
if not preserve_spaces: | |
command = re.sub(' +', ' ', command) | |
# logger.debug(cyan(command)) | |
code = subprocess.call([command], shell=True) | |
if not code == 0: | |
logger.error('%s: %s' % (command, code)) | |
raise RuntimeError('%s exitcode: %s' % (command, code)) | |
return kwargs.get('output') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment