import re


def get_casa_version_from_calibration_script(script):
    """Pulls CASA pipeline version from calibration script

    Again, taken from Daizhong's A3COSMOS stuff, thanks Daizhong!

    """

    casa_pipeline_version = None

    with open(script, 'r') as fp:

        for line in fp:
            if line.find('PLEASE USE THE SAME VERSION OF CASA') >= 0:
                if re.match(r'^.*PLEASE USE THE SAME VERSION OF CASA.*:[ \t]*([0-9.]+)\b.*$', line):
                    casa_pipeline_version = re.sub(r'^.*PLEASE USE THE SAME VERSION OF CASA.*:[ \t]*([0-9.]+)\b.*$',
                                                   r'\1', line)
                    # Trim any whitespace
                    casa_pipeline_version = casa_pipeline_version.strip()
                    break

    return casa_pipeline_version