cliutil: Take on --jobs support from audit_report.

This commit is contained in:
Brett Smith 2020-09-02 13:26:35 -04:00
parent 0f91aefb5a
commit 552dae6ea5
2 changed files with 21 additions and 16 deletions

View file

@ -59,6 +59,7 @@ from .beancount_types import (
OutputFile = Union[int, IO]
CPU_COUNT = len(os.sched_getaffinity(0))
STDSTREAM_PATH = Path('-')
VERSION = pkg_resources.require(PKGNAME)[0].version
@ -241,6 +242,15 @@ class SearchTerm(NamedTuple):
postings, self.meta_key, re.compile(self.pattern),
)
def add_jobs_argument(parser: argparse.ArgumentParser) -> argparse.Action:
return parser.add_argument(
'--jobs', '-j',
metavar='NUM',
type=jobs_arg,
default=CPU_COUNT,
help="""Maximum number of processes to run concurrently.
Can specify a positive integer or a percentage of CPU cores. Default all cores.
""")
def add_loglevel_argument(parser: argparse.ArgumentParser,
default: LogLevel=LogLevel.INFO) -> argparse.Action:
@ -306,6 +316,16 @@ def year_or_date_arg(arg: str) -> Union[int, datetime.date]:
else:
return date_arg(arg)
def jobs_arg(arg: str) -> int:
if arg.endswith('%'):
arg_n = round(CPU_COUNT * 100 / int(arg[:-1]))
else:
arg_n = int(arg)
if arg_n < 1:
raise ValueError("--jobs argument must be a positive integer or percentage")
else:
return arg_n
def make_entry_point(mod_name: str, prog_name: str=sys.argv[0]) -> Callable[[], int]:
"""Create an entry_point function for a tool

View file

@ -52,17 +52,9 @@ from beancount.scripts import check as bc_check
ArgList = List[str]
ReportFunc = Callable[[ArgList, TextIO, TextIO, configmod.Config], int]
CPU_COUNT = len(os.sched_getaffinity(0))
PROGNAME = 'audit-report'
logger = logging.getLogger('conservancy_beancount.tools.audit_report')
def jobs_arg(arg: str) -> int:
if arg.endswith('%'):
arg_n = round(CPU_COUNT * 100 / int(arg[:-1]))
else:
arg_n = int(arg)
return max(1, arg_n)
def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
parser = argparse.ArgumentParser(prog=PROGNAME)
cliutil.add_version_argument(parser)
@ -72,14 +64,7 @@ def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace
action='store_true',
help="""Display progress information
""")
parser.add_argument(
'--jobs', '-j',
metavar='NUM',
type=jobs_arg,
default=CPU_COUNT,
help="""Maximum number of processes to run concurrently.
Can specify a positive integer or a percentage of CPU cores. Default all cores.
""")
cliutil.add_jobs_argument(parser)
parser.add_argument(
'--output-directory', '-O',
metavar='DIR',