diff options
Diffstat (limited to 'patternutils/commands/targs.py')
| -rw-r--r-- | patternutils/commands/targs.py | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index bfacbfc..51f5527 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -1,11 +1,15 @@ +import functools import json import multiprocessing +import os import shlex import subprocess from typing import ( Union, List, + Dict, + Callable, ) import click @@ -13,6 +17,16 @@ import click import patternutils +def run_command(data: Dict[str, str], render: Callable[..., str], shell: bool=False) -> None: + c = render(**data) + if not shell: + prepared_command = shlex.split(c) + else: + prepared_command = c + + subprocess.run(prepared_command, shell=shell) + + @click.command(context_settings={'help_option_names': ('-h', '--help', '-?')}) @click.option('--read-from', default='-', type=click.File('r')) @click.option('-s', '--shell', is_flag=True, default=False, @@ -21,6 +35,7 @@ import patternutils help='select the template engine to use') @click.option('-v', '--verbose', is_flag=True, default=False, help='explain what is being done') +@click.option('-P', '--max-procs', default=1, type=click.IntRange(min=0, clamp=True)) @click.version_option() @click.argument('command') def targs( @@ -28,6 +43,7 @@ def targs( read_from: click.File, shell: bool, template_engine: str, + max_procs: int, verbose: bool, ) -> None: @@ -44,14 +60,15 @@ def targs( click.secho('jinja2 not installed!', fg='red', err=True) raise SystemExit - for r in data: - c = render(**r) - if not shell: - prepared_command = shlex.split(c) # type: Union[str, List[str]] - else: - prepared_command = c + max_procs = os.cpu_count() * 2 if max_procs == 0 else max_procs - subprocess.run(prepared_command, shell=shell) + patched_run_command = functools.partial(run_command, render=render, shell=shell) + if max_procs == 1: + for r in data: + patched_run_command(r) + else: + with multiprocessing.Pool(max_procs) as p: + p.map(patched_run_command, data) if __name__ == '__main__': |
