import argparse
import subprocess
import re
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
parser = argparse.ArgumentParser(description='Setup Casa developer build image')
parser.add_argument("-s", "--sourcedir", type=str, required=True,
help='Source code location e.g. /home/<user>/source')
parser.add_argument("-c", "--ccachedir", type=str, required=True, help='ccache dir')
parser.add_argument("-d", "--casadata", type=str, required=True, help='Location of casa-data')
parser.add_argument("-r", "--casadatareq", type=str, help='Location of casa-data-req')
parser.add_argument("-i", "--image", type=str, help='Docker image')
parser.add_argument('--debug', action='store_true', help='Print debug messages')
args = parser.parse_args()
logger = logging.getLogger()
if args.debug:
logger.setLevel(logging.DEBUG)
logger.debug('Argument values:')
logger.debug(args.sourcedir)
logger.debug(args.ccachedir)
logger.debug(args.casadata)
logger.debug(args.casadatareq)
logger.debug(args.image)
process = subprocess.Popen(['docker', 'pull', args.image ], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=args.sourcedir)
stdout, stderr = process.communicate()
logger.debug(stdout)
cmd = ['docker', 'run','-d','-t','-v', args.sourcedir + ':/source', '-v', args.ccachedir + ':/ccache',\
'-v', args.casadata + ':/opt/casa/data','-v', args.casadatareq + ':/opt/casa/data-req',\
args.image]
logger.debug(" ".join(cmd))
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=args.sourcedir)
stdout, stderr = process.communicate()
print(stdout.rstrip().decode())