From c8c7ca4198e3bf8110d3111d350ee7e9f9800f02 Mon Sep 17 00:00:00 2001 From: dorp Date: Wed, 27 Mar 2019 15:47:34 +0100 Subject: [PATCH] handling errors --- extract.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/extract.py b/extract.py index c24424d9..acb438c3 100755 --- a/extract.py +++ b/extract.py @@ -17,6 +17,10 @@ def parse_arguments(): return parser.parse_args() +def container_exists(container): + return subprocess.run('docker history {}'.format(container), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).returncode == 0 + + def call_docker(input_file, container, target): tmpdir = TemporaryDirectory() tmp = tmpdir.name @@ -28,12 +32,12 @@ def call_docker(input_file, container, target): subprocess.run('docker run --rm -v {}:/tmp/extractor -v /dev:/dev --privileged {}'.format(tmp, container), shell=True) - print('Moving files to {}'.format(target)) shutil.copytree(str(Path(tmp, 'files')), target) try: tmpdir.cleanup() except PermissionError: + print('Cleanup requires root. If you would be so kind..') subprocess.run('sudo rm -rf {}'.format(tmpdir.name), shell=True) tmpdir.cleanup() @@ -42,9 +46,21 @@ def main(): arguments = parse_arguments() output_directory = arguments.output_directory if arguments.output_directory else str(Path(__file__).parent / 'extracted_files') + if Path(output_directory).exists(): + print('Target directory exists ({}). Please choose a non-existing directory with -o option.'.format(output_directory)) + return 1 + + if not container_exists(arguments.container): + print('Container {} doesn\'t exist. Please specify an existing container with the -c option.'.format(arguments.container)) + return 1 + + if not Path(arguments.ARCHIVE[0]).is_file(): + print('Given input file {} doesn\'t exist. Please give an existing path.'.format(arguments.ARCHIVE[0])) call_docker(arguments.ARCHIVE[0], arguments.container, output_directory) + return 0 + if __name__ == '__main__': sys.exit(main())