<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def reload_libraries(library_directories: list = None):
 """ Reload the libraries stored in the project's local and shared library directories """

    directories = library_directories or []
    project = cauldron.project.get_internal_project()
    if project:
        directories += project.library_directories

    if not directories:
        return

    def reload_module(path: str, library_directory: str):
        path = os.path.dirname(path) if path.endswith('__init__.py') else path
        start_index = len(library_directory) + 1
        end_index = -3 if path.endswith('.py') else None
        package_path = path[start_index:end_index]

        module = sys.modules.get(package_path.replace(os.sep, '.'))
        return importlib.reload(module) if module is not None else None

    def reload_library(directory: str) -> list:
        if not add_library_path(directory):
            # If the library wasn't added because it doesn't exist, remove it
            # in case the directory has recently been deleted and then return
            # an empty result
            remove_library_path(directory)
            return []

        glob_path = os.path.join(directory, '**', '*.py')
        return [
            reload_module(path, directory)
            for path in glob.glob(glob_path, recursive=True)
        ]

    return [
        reloaded_module
        for directory in directories
        for reloaded_module in reload_library(directory)
        if reload_module is not None
    ]