From 5285d68fcc4d8ced4889f29ca3ff68d5be1f539b Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 11 Oct 2023 01:02:50 -0700 Subject: [PATCH] Add check for filenotfound due to being inside a snap --- src/ocrmypdf/_validation.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ocrmypdf/_validation.py b/src/ocrmypdf/_validation.py index 69c65252..3639ebca 100644 --- a/src/ocrmypdf/_validation.py +++ b/src/ocrmypdf/_validation.py @@ -245,6 +245,18 @@ def check_options(options: Namespace, plugin_manager: PluginManager) -> None: _check_plugin_options(options, plugin_manager) +def _in_docker(): + return Path('/.dockerenv').exists() + + +def _in_snap(): + try: + cgroup_text = Path('/proc/self/cgroup').read_text() + return 'snap.ocrmypdf' in cgroup_text + except FileNotFoundError: + return False + + def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str]: if options.input_file == '-': # stdin @@ -268,7 +280,7 @@ def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str] return target, os.fspath(options.input_file) except FileNotFoundError as e: msg = f"File not found - {options.input_file}" - if Path('/.dockerenv').exists(): # pragma: no cover + if _in_docker(): # pragma: no cover msg += ( "\nDocker cannot your working directory unless you " "explicitly share it with the Docker container and set up" @@ -278,6 +290,15 @@ def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str] "\tdocker run -i --rm jbarlow83/ocrmypdf - - output.pdf" "\n" ) + elif _in_snap(): # pragma: no cover + msg += ( + "\nSnap applications cannot access files outside of " + "your home directory unless you explicitly allow it. " + "You may find it easier to use stdin/stdout:" + "\n" + "\tsnap run ocrmypdf - - output.pdf" + "\n" + ) raise InputFileError(msg) from e