<commit_before>"""This small script offers a python equivalent to "which"

For every argument on the command line it lists
    all python files and all directories with that name
    in each of the directories in sys.path

If a name is found more than once each is listed
    (but python will only import one, probably the first listed)
"""

_copyright = """
(c) <NAME> 2013
    The source is released under the MIT license
    See http://jalanb.mit-license.org/ for more information
"""


import os
import sys
import fnmatch
import importlib


def directory_list(path):
    """A list of all items in that path

    If path is not a directory, an empty list
    """
    if not os.path.isdir(path):
        return []
    return os.listdir(path)


def is_file_in(path, name):
    """Whether that name is a file in the directory at that path"""
    path_to_name = os.path.join(path, name)
    return os.path.isfile(path_to_name)


def is_matching_file_in(path, name, glob):
    """Whether that name is a file in path and matches that glob"""
    return is_file_in(path, name) and fnmatch.fnmatch(name, glob)


def path_to_module(path, name):
    """Whether the name matches a python source or compiled file in that path

    If source and compiled files are found, give the source
    """
    glob = '%s.py*' % name
    python_files = [f
                    for f in directory_list(path)
                    if is_matching_file_in(path, f, glob)]
    if not python_files:
        return None
    source_files = [f for f in python_files if os.path.splitext(f)[-1] == '.py']
    python_file = source_files and source_files[0] or python_files[0]
    return os.path.join(path, python_file)


def path_to_sub_directory(path, name):
    """If name is a real sub-directory of path, return that"""
    result = os.path.join(path, name)
    if os.path.isdir(result):
        return result


def path_to_python(path, name):
    """Path to either a module or sub-dir of that path, with that name"""
    result = path_to_sub_directory(path, name)
    if result:
        return result
    return path_to_module(path, name)


def built_in(name):
    """Whether the name is that of one of python's builtin modules"""
    try:
        #  Not all builtin modules are initially imported, so bring it in first
        __import__(name)
    except ImportError:
        return False
    return '(built-in)' in str(sys.modules[name])


def path_to_import(string):
    module = importlib.import_module(string)
    if module:
        path_to_file = module.__file__
        if '.egg/' in path_to_file:
            return path_to_file.split('.egg/')[0] + '.egg'
        return path_to_file
    return None


def main(strings):
    """Run the program"""
    for string in strings:
        if built_in(string):
            print 'builtin', string
        path_to_imported_module = path_to_import(string)
        if path_to_imported_module:
            os.system('ls -ld %s' % path_to_imported_module)


if __name__ == '__main__':
    args = sys.argv[1:]
    if not args:
        from what import test
        sys.exit(test())
    else:
        sys.exit(main(args))<commit_msg>Catch another ImportError<commit_after>"""This small script offers a python equivalent to "which"

For every argument on the command line it lists
    all python files and all directories with that name
    in each of the directories in sys.path

If a name is found more than once each is listed
    (but python will only import one, probably the first listed)
"""

_copyright = """
(c) <NAME> 2013
    The source is released under the MIT license
    See http://jalanb.mit-license.org/ for more information
"""


import os
import sys
import fnmatch
import importlib


def directory_list(path):
    """A list of all items in that path

    If path is not a directory, an empty list
    """
    if not os.path.isdir(path):
        return []
    return os.listdir(path)


def is_file_in(path, name):
    """Whether that name is a file in the directory at that path"""
    path_to_name = os.path.join(path, name)
    return os.path.isfile(path_to_name)


def is_matching_file_in(path, name, glob):
    """Whether that name is a file in path and matches that glob"""
    return is_file_in(path, name) and fnmatch.fnmatch(name, glob)


def path_to_module(path, name):
    """Whether the name matches a python source or compiled file in that path

    If source and compiled files are found, give the source
    """
    glob = '%s.py*' % name
    python_files = [f
                    for f in directory_list(path)
                    if is_matching_file_in(path, f, glob)]
    if not python_files:
        return None
    source_files = [f
                    for f in python_files
                    if os.path.splitext(f)[-1] == '.py']
    python_file = source_files and source_files[0] or python_files[0]
    return os.path.join(path, python_file)


def path_to_sub_directory(path, name):
    """If name is a real sub-directory of path, return that"""
    result = os.path.join(path, name)
    if os.path.isdir(result):
        return result


def path_to_python(path, name):
    """Path to either a module or sub-dir of that path, with that name"""
    result = path_to_sub_directory(path, name)
    if result:
        return result
    return path_to_module(path, name)


def built_in(name):
    """Whether the name is that of one of python's builtin modules"""
    try:
        #  Not all builtin modules are initially imported, so bring it in first
        __import__(name)
    except ImportError:
        return False
    return '(built-in)' in str(sys.modules[name])


def path_to_import(string):
    try:
        module = importlib.import_module(string)
    except ImportError as e:
        print >> sys.stderr, e
        return None
    if module:
        path_to_file = module.__file__
        if '.egg/' in path_to_file:
            return path_to_file.split('.egg/')[0] + '.egg'
        return path_to_file
    return None


def main(strings):
    """Run the program"""
    for string in strings:
        if built_in(string):
            print 'builtin', string
        path_to_imported_module = path_to_import(string)
        if path_to_imported_module:
            os.system('ls -ld %s' % path_to_imported_module)


if __name__ == '__main__':
    args = sys.argv[1:]
    if not args:
        from what import test
        sys.exit(test())
    else:
        sys.exit(main(args))
{'fraction_non_alphanumeric': 0.09140369967355821, 'fraction_numerical': 0.002798072438986476, 'mean_word_length': 2.751603498542274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5761325', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1931, 'n_words': 703}