def scan_directory(pym, directory, sentinel, installed, depth=0):
    '''Entry point scan that creates a PyModule instance if needed.
    '''
    if not pym:
        d = os.path.abspath(directory)
        basename = os.path.basename(d)
        pym = utils.find_package(basename, installed)
        if not pym:
            version = 'DIRECTORY'
            if os.path.isfile(os.path.join(d, '__init__.py')):
                version = 'MODULE'
            pym = PyModule(basename, version, d)
            installed.insert(0, pym)
        else:
            pym.is_scan = True

    # Keep track of how many file scans resulted in nothing
    bad_scans = 0

    for item in _scan_directory(directory, sentinel, depth):
        if os.path.isfile(item):
            if bad_scans > 100:
                # Keep in mind this counter resets if it a good scan happens
                # in *this* directory. If you have a module with more than 100
                # files in a single directory, you should probably refactor it.
                log.debug('Stopping scan of directory since it looks like a data dump: %s', directory)
                break

            if not scan_file(pym, item, sentinel, installed):
                bad_scans += 1
            else:
                bad_scans = 0
        elif os.path.isdir(item):
            scan_directory(pym, item, sentinel, installed, depth + 1)

    return pym