prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  New numpy hook for intel mkl libraries
  Hello - anaconda and winpython build numpy against Intel's mkl libraries. Building someone that uses numpy will need to have those libraries.
  
  Here is a winpython hook:
  http://stackoverflow.com/a/35853001
  
  and I adapted that for anaconda & python3:
  https://github.com/maqifrnswa/scimpy/blob/master/pyinstaller-hooks/hook-numpy.py
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `PyInstaller/hooks/hook-numpy.core.py`
  Content:
  ```
  1 #-----------------------------------------------------------------------------
  2 # Copyright (c) 2013-2016, PyInstaller Development Team.
  3 #
  4 # Distributed under the terms of the GNU General Public License with exception
  5 # for distributing bootloader.
  6 #
  7 # The full license is in the file COPYING.txt, distributed with this software.
  8 #-----------------------------------------------------------------------------
  9 # On Windows, numpy depends on a set of dynamically-detemined DLLs, which means
  10 # that PyInstaller's static analysis can't find them. See https://github.com/pyinstaller/pyinstaller/issues/1969
  11 # for more information. The typical error message: ``Intel MKL FATAL ERROR:
  12 # Cannot load mkl_intel_thread.dll.``
  13 #
  14 # So, include them manually.
  15 import os
  16 import os.path
  17 from PyInstaller.utils.hooks import get_package_paths
  18 
  19 pkg_base, pkg_dir = get_package_paths('numpy.core')
  20 # Walk through all files in ``numpy.core``, looking for DLLs.
  21 datas = []
  22 for f in os.listdir(pkg_dir):
  23     extension = os.path.splitext(f)[1]
  24     if extension == '.dll':
  25         # Produce the tuple ('/abs/path/to/libs/numpy/core/file.dll', '')
  26         source = os.path.join(pkg_dir, f)
  27         datas.append((source, ''))
  28
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/PyInstaller/hooks/hook-numpy.core.py b/PyInstaller/hooks/hook-numpy.core.py
  --- a/PyInstaller/hooks/hook-numpy.core.py
  +++ b/PyInstaller/hooks/hook-numpy.core.py
  @@ -6,22 +6,41 @@
   #
   # The full license is in the file COPYING.txt, distributed with this software.
   #-----------------------------------------------------------------------------
  -# On Windows, numpy depends on a set of dynamically-detemined DLLs, which means
  -# that PyInstaller's static analysis can't find them. See https://github.com/pyinstaller/pyinstaller/issues/1969
  -# for more information. The typical error message: ``Intel MKL FATAL ERROR:
  -# Cannot load mkl_intel_thread.dll.``
  +# If numpy is built with MKL support it depends on a set of libraries loaded
  +# at runtime. Since PyInstaller's static analysis can't find them they must be
  +# included manually.
   #
  -# So, include them manually.
  +# See
  +# https://github.com/pyinstaller/pyinstaller/issues/1881
  +# https://github.com/pyinstaller/pyinstaller/issues/1969
  +# for more information
   import os
   import os.path
  +import re
   from PyInstaller.utils.hooks import get_package_paths
  +from PyInstaller import log as logging 
  +from PyInstaller import compat
   
  +binaries = []
  +
  +# look for libraries in numpy package path
   pkg_base, pkg_dir = get_package_paths('numpy.core')
  -# Walk through all files in ``numpy.core``, looking for DLLs.
  -datas = []
  -for f in os.listdir(pkg_dir):
  -    extension = os.path.splitext(f)[1]
  -    if extension == '.dll':
  -        # Produce the tuple ('/abs/path/to/libs/numpy/core/file.dll', '')
  -        source = os.path.join(pkg_dir, f)
  -        datas.append((source, ''))
  +re_anylib = re.compile(r'\w+\.(?:dll|so)', re.IGNORECASE)
  +dlls_pkg = [f for f in os.listdir(pkg_dir) if re_anylib.match(f)]
  +binaries += [(os.path.join(pkg_dir, f), '') for f in dlls_pkg]
  +
  +# look for MKL libraries in pythons lib directory
  +# TODO: check numpy.__config__ if numpy is actually depending on MKL
  +# TODO: determine which directories are searched by the os linker
  +if compat.is_win:
  +    lib_dir = os.path.join(compat.base_prefix, "Library", "bin")
  +else:
  +    lib_dir = os.path.join(compat.base_prefix, "lib")
  +if os.path.isdir(lib_dir):
  +    re_mkllib = re.compile(r'^(?:lib)?mkl\w+\.(?:dll|so)', re.IGNORECASE)
  +    dlls_mkl = [f for f in os.listdir(lib_dir) if re_mkllib.match(f)]
  +    if dlls_mkl:
  +        logger = logging.getLogger(__name__)
  +        logger.info("MKL libraries found when importing numpy. Adding MKL to binaries")
  +        binaries += [(os.path.join(lib_dir, f), '') for f in dlls_mkl]
  +
  
verification_info: {"golden_diff": "diff --git a/PyInstaller/hooks/hook-numpy.core.py b/PyInstaller/hooks/hook-numpy.core.py\n--- a/PyInstaller/hooks/hook-numpy.core.py\n+++ b/PyInstaller/hooks/hook-numpy.core.py\n@@ -6,22 +6,41 @@\n #\n # The full license is in the file COPYING.txt, distributed with this software.\n #-----------------------------------------------------------------------------\n-# On Windows, numpy depends on a set of dynamically-detemined DLLs, which means\n-# that PyInstaller's static analysis can't find them. See https://github.com/pyinstaller/pyinstaller/issues/1969\n-# for more information. The typical error message: ``Intel MKL FATAL ERROR:\n-# Cannot load mkl_intel_thread.dll.``\n+# If numpy is built with MKL support it depends on a set of libraries loaded\n+# at runtime. Since PyInstaller's static analysis can't find them they must be\n+# included manually.\n #\n-# So, include them manually.\n+# See\n+# https://github.com/pyinstaller/pyinstaller/issues/1881\n+# https://github.com/pyinstaller/pyinstaller/issues/1969\n+# for more information\n import os\n import os.path\n+import re\n from PyInstaller.utils.hooks import get_package_paths\n+from PyInstaller import log as logging \n+from PyInstaller import compat\n \n+binaries = []\n+\n+# look for libraries in numpy package path\n pkg_base, pkg_dir = get_package_paths('numpy.core')\n-# Walk through all files in ``numpy.core``, looking for DLLs.\n-datas = []\n-for f in os.listdir(pkg_dir):\n-    extension = os.path.splitext(f)[1]\n-    if extension == '.dll':\n-        # Produce the tuple ('/abs/path/to/libs/numpy/core/file.dll', '')\n-        source = os.path.join(pkg_dir, f)\n-        datas.append((source, ''))\n+re_anylib = re.compile(r'\\w+\\.(?:dll|so)', re.IGNORECASE)\n+dlls_pkg = [f for f in os.listdir(pkg_dir) if re_anylib.match(f)]\n+binaries += [(os.path.join(pkg_dir, f), '') for f in dlls_pkg]\n+\n+# look for MKL libraries in pythons lib directory\n+# TODO: check numpy.__config__ if numpy is actually depending on MKL\n+# TODO: determine which directories are searched by the os linker\n+if compat.is_win:\n+    lib_dir = os.path.join(compat.base_prefix, \"Library\", \"bin\")\n+else:\n+    lib_dir = os.path.join(compat.base_prefix, \"lib\")\n+if os.path.isdir(lib_dir):\n+    re_mkllib = re.compile(r'^(?:lib)?mkl\\w+\\.(?:dll|so)', re.IGNORECASE)\n+    dlls_mkl = [f for f in os.listdir(lib_dir) if re_mkllib.match(f)]\n+    if dlls_mkl:\n+        logger = logging.getLogger(__name__)\n+        logger.info(\"MKL libraries found when importing numpy. Adding MKL to binaries\")\n+        binaries += [(os.path.join(lib_dir, f), '') for f in dlls_mkl]\n+\n", "issue": "New numpy hook for intel mkl libraries\nHello - anaconda and winpython build numpy against Intel's mkl libraries. Building someone that uses numpy will need to have those libraries.\n\nHere is a winpython hook:\nhttp://stackoverflow.com/a/35853001\n\nand I adapted that for anaconda & python3:\nhttps://github.com/maqifrnswa/scimpy/blob/master/pyinstaller-hooks/hook-numpy.py\n\n", "before_files": [{"content": "#-----------------------------------------------------------------------------\n# Copyright (c) 2013-2016, PyInstaller Development Team.\n#\n# Distributed under the terms of the GNU General Public License with exception\n# for distributing bootloader.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#-----------------------------------------------------------------------------\n# On Windows, numpy depends on a set of dynamically-detemined DLLs, which means\n# that PyInstaller's static analysis can't find them. See https://github.com/pyinstaller/pyinstaller/issues/1969\n# for more information. The typical error message: ``Intel MKL FATAL ERROR:\n# Cannot load mkl_intel_thread.dll.``\n#\n# So, include them manually.\nimport os\nimport os.path\nfrom PyInstaller.utils.hooks import get_package_paths\n\npkg_base, pkg_dir = get_package_paths('numpy.core')\n# Walk through all files in ``numpy.core``, looking for DLLs.\ndatas = []\nfor f in os.listdir(pkg_dir):\n    extension = os.path.splitext(f)[1]\n    if extension == '.dll':\n        # Produce the tuple ('/abs/path/to/libs/numpy/core/file.dll', '')\n        source = os.path.join(pkg_dir, f)\n        datas.append((source, ''))\n", "path": "PyInstaller/hooks/hook-numpy.core.py"}], "after_files": [{"content": "#-----------------------------------------------------------------------------\n# Copyright (c) 2013-2016, PyInstaller Development Team.\n#\n# Distributed under the terms of the GNU General Public License with exception\n# for distributing bootloader.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#-----------------------------------------------------------------------------\n# If numpy is built with MKL support it depends on a set of libraries loaded\n# at runtime. Since PyInstaller's static analysis can't find them they must be\n# included manually.\n#\n# See\n# https://github.com/pyinstaller/pyinstaller/issues/1881\n# https://github.com/pyinstaller/pyinstaller/issues/1969\n# for more information\nimport os\nimport os.path\nimport re\nfrom PyInstaller.utils.hooks import get_package_paths\nfrom PyInstaller import log as logging \nfrom PyInstaller import compat\n\nbinaries = []\n\n# look for libraries in numpy package path\npkg_base, pkg_dir = get_package_paths('numpy.core')\nre_anylib = re.compile(r'\\w+\\.(?:dll|so)', re.IGNORECASE)\ndlls_pkg = [f for f in os.listdir(pkg_dir) if re_anylib.match(f)]\nbinaries += [(os.path.join(pkg_dir, f), '') for f in dlls_pkg]\n\n# look for MKL libraries in pythons lib directory\n# TODO: check numpy.__config__ if numpy is actually depending on MKL\n# TODO: determine which directories are searched by the os linker\nif compat.is_win:\n    lib_dir = os.path.join(compat.base_prefix, \"Library\", \"bin\")\nelse:\n    lib_dir = os.path.join(compat.base_prefix, \"lib\")\nif os.path.isdir(lib_dir):\n    re_mkllib = re.compile(r'^(?:lib)?mkl\\w+\\.(?:dll|so)', re.IGNORECASE)\n    dlls_mkl = [f for f in os.listdir(lib_dir) if re_mkllib.match(f)]\n    if dlls_mkl:\n        logger = logging.getLogger(__name__)\n        logger.info(\"MKL libraries found when importing numpy. Adding MKL to binaries\")\n        binaries += [(os.path.join(lib_dir, f), '') for f in dlls_mkl]\n\n", "path": "PyInstaller/hooks/hook-numpy.core.py"}]}