#!/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()

[((341, 381), 'shutil.copy', 'shutil.copy', (['input_file', 'temp_input_file'], {}), '(input_file, temp_input_file)\n', (352, 381), False, 'import shutil\n'), ((453, 531), 'subprocess.run', 'subprocess.run', (["['singularity', 'run', sif, temp_input_file, temp_output_file]"], {}), "(['singularity', 'run', sif, temp_input_file, temp_output_file])\n", (467, 531), False, 'import subprocess\n'), ((535, 575), 'shutil.copy', 'shutil.copy', (['temp_output_file', 'json_file'], {}), '(temp_output_file, json_file)\n', (546, 575), False, 'import shutil\n'), ((581, 612), 'os.path.exists', 'os.path.exists', (['temp_input_file'], {}), '(temp_input_file)\n', (595, 612), False, 'import os\n'), ((648, 680), 'os.path.exists', 'os.path.exists', (['temp_output_file'], {}), '(temp_output_file)\n', (662, 680), False, 'import os\n'), ((217, 229), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (227, 229), False, 'import uuid\n'), ((390, 421), 'mgm_utils.get_sif_dir', 'mgm_utils.get_sif_dir', (['root_dir'], {}), '(root_dir)\n', (411, 421), False, 'import mgm_utils\n'), ((616, 642), 'os.remove', 'os.remove', (['temp_input_file'], {}), '(temp_input_file)\n', (625, 642), False, 'import os\n'), ((684, 711), 'os.remove', 'os.remove', (['temp_output_file'], {}), '(temp_output_file)\n', (693, 711), False, 'import os\n')]