#!/usr/bin/env python3

import os
import os.path
import shutil
import subprocess
import sys
import tempfile
import uuid

import mgm_utils

def main():
	(root_dir, input_file, json_file) = sys.argv[1:4]
	tmpName = str(uuid.uuid4())
	tmpdir = "/tmp"
	temp_input_file = f"{tmpdir}/{tmpName}.dat"
	temp_output_file = f"{tmpdir}/{tmpName}.json"
	shutil.copy(input_file, temp_input_file)

	sif = mgm_utils.get_sif_dir(root_dir) + "/ina_segmentation.sif"
	r = subprocess.run(["singularity", "run", sif, temp_input_file, temp_output_file])
	
	shutil.copy(temp_output_file, json_file)

	if os.path.exists(temp_input_file):
		os.remove(temp_input_file)

	if os.path.exists(temp_output_file):
		os.remove(temp_output_file)
		
	exit(r.returncode)
		
if __name__ == "__main__":
	main()

[((19, 1, 19, 41), 'shutil.copy', 'shutil.copy', ({(19, 13, 19, 23): 'input_file', (19, 25, 19, 40): 'temp_input_file'}, {}), '(input_file, temp_input_file)', False, 'import shutil\n'), ((22, 5, 22, 83), 'subprocess.run', 'subprocess.run', ({(22, 20, 22, 82): "['singularity', 'run', sif, temp_input_file, temp_output_file]"}, {}), "(['singularity', 'run', sif, temp_input_file, temp_output_file])", False, 'import subprocess\n'), ((24, 1, 24, 41), 'shutil.copy', 'shutil.copy', ({(24, 13, 24, 29): 'temp_output_file', (24, 31, 24, 40): 'json_file'}, {}), '(temp_output_file, json_file)', False, 'import shutil\n'), ((26, 4, 26, 35), 'os.path.exists', 'os.path.exists', ({(26, 19, 26, 34): 'temp_input_file'}, {}), '(temp_input_file)', False, 'import os\n'), ((29, 4, 29, 36), 'os.path.exists', 'os.path.exists', ({(29, 19, 29, 35): 'temp_output_file'}, {}), '(temp_output_file)', False, 'import os\n'), ((15, 15, 15, 27), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((21, 7, 21, 38), 'mgm_utils.get_sif_dir', 'mgm_utils.get_sif_dir', ({(21, 29, 21, 37): 'root_dir'}, {}), '(root_dir)', False, 'import mgm_utils\n'), ((27, 2, 27, 28), 'os.remove', 'os.remove', ({(27, 12, 27, 27): 'temp_input_file'}, {}), '(temp_input_file)', False, 'import os\n'), ((30, 2, 30, 29), 'os.remove', 'os.remove', ({(30, 12, 30, 28): 'temp_output_file'}, {}), '(temp_output_file)', False, 'import os\n')]