Skip to content

Commit

Permalink
Merge pull request #1127 from OCR-D/fix-bashlib-log3
Browse files Browse the repository at this point in the history
add file handler for bashlib
  • Loading branch information
kba authored Oct 19, 2023
2 parents 46bee61 + a078b99 commit 69cfcc9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
9 changes: 7 additions & 2 deletions ocrd/ocrd/processor/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_processor(
parameter_override=None,
working_dir=None,
mets_server_url=None,
instance_caching=False # TODO don't set this yet!
instance_caching=False
): # pylint: disable=too-many-locals
"""
Instantiate a Pythonic processor, open a workspace, run the processor and save the workspace.
Expand Down Expand Up @@ -169,6 +169,7 @@ def run_cli(
page_id=None,
overwrite=None,
log_level=None,
log_filename=None,
input_file_grp=None,
output_file_grp=None,
parameter=None,
Expand Down Expand Up @@ -213,7 +214,11 @@ def run_cli(
args += ['--mets-server-url', mets_server_url]
log = getLogger('ocrd.processor.helpers.run_cli')
log.debug("Running subprocess '%s'", ' '.join(args))
result = run(args, check=False)
if not log_filename:
result = run(args, check=False)
else:
with open(log_filename, 'a') as file_desc:
result = run(args, check=False, stdout=file_desc, stderr=file_desc)
return result.returncode


Expand Down
46 changes: 24 additions & 22 deletions ocrd_network/ocrd_network/process_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ def invoke_processor(
page_id: str,
parameters: dict,
mets_server_url: Optional[str] = None,
log_filename : str = None,
log_filename: str = None,
) -> None:
if not (processor_class or executable):
raise ValueError('Missing processor class and executable')
input_file_grps_str = ','.join(input_file_grps)
output_file_grps_str = ','.join(output_file_grps)

ctx_mgr = redirect_stderr_and_stdout_to_file(log_filename) if log_filename else nullcontext()
with ctx_mgr:
workspace = get_ocrd_workspace_instance(
mets_path=abs_path_to_mets,
mets_server_url=mets_server_url
)
if processor_class:
workspace = get_ocrd_workspace_instance(
mets_path=abs_path_to_mets,
mets_server_url=mets_server_url
)

if processor_class:
ctx_mgr = redirect_stderr_and_stdout_to_file(log_filename) if log_filename else nullcontext()
with ctx_mgr:
initLogging(force_reinit=True)
try:
run_processor(
Expand All @@ -46,17 +47,18 @@ def invoke_processor(
)
except Exception as e:
raise RuntimeError(f"Python executable '{processor_class.__dict__}' exited with: {e}")
else:
return_code = run_cli(
executable=executable,
workspace=workspace,
mets_url=abs_path_to_mets,
input_file_grp=input_file_grps_str,
output_file_grp=output_file_grps_str,
page_id=page_id,
parameter=json.dumps(parameters),
mets_server_url=mets_server_url,
log_level='DEBUG'
)
if return_code != 0:
raise RuntimeError(f"CLI executable '{executable}' exited with: {return_code}")
else:
return_code = run_cli(
executable=executable,
workspace=workspace,
mets_url=abs_path_to_mets,
input_file_grp=input_file_grps_str,
output_file_grp=output_file_grps_str,
page_id=page_id,
parameter=json.dumps(parameters),
mets_server_url=mets_server_url,
log_level='DEBUG',
log_filename=log_filename
)
if return_code != 0:
raise RuntimeError(f"CLI executable '{executable}' exited with: {return_code}")

0 comments on commit 69cfcc9

Please sign in to comment.