Skip to content

Commit

Permalink
handling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dorpvom committed Mar 27, 2019
1 parent f52b304 commit c8c7ca4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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())

0 comments on commit c8c7ca4

Please sign in to comment.