#!/usr/local/bin/python2.7

# Copyright 2012-2013, Andrey Kislyuk and argcomplete contributors.
# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.

'''
Check if an EASY-INSTALL-SCRIPT wrapper redirects to a script that contains "PYTHON_ARGCOMPLETE_OK"
'''
import os, sys, re, argparse, pkg_resources

parser = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument("wrapper_script", help="Wrapper script to examine")
args = parser.parse_args()

with open(args.wrapper_script) as fh:
    line1, lines = fh.read(1024).split("\n", 1)[:2]
    assert(line1.startswith('#') and 'py' in line1)
    for line in lines.split("\n", 10):
        if line.startswith("# EASY-INSTALL-SCRIPT"):
            dist, script = re.match("# EASY-INSTALL-SCRIPT: '(.+)','(.+)'", line).groups()
            if "PYTHON_ARGCOMPLETE_OK" in pkg_resources.get_distribution(dist).get_metadata('scripts/'+script):
                exit(0)
        elif line.startswith("# EASY-INSTALL-ENTRY-SCRIPT"):
            dist, group, name = re.match("# EASY-INSTALL-ENTRY-SCRIPT: '(.+)','(.+)','(.+)'", line).groups()
            import pkgutil
            module_name = pkg_resources.get_distribution(dist).get_entry_info(group, name).module_name
            with open(pkgutil.get_loader(module_name).get_filename()) as mod_fh:
                if "PYTHON_ARGCOMPLETE_OK" in mod_fh.read(1024):
                    exit(0)

exit(1)
