You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.8KB

  1. """Filesystem utility functions."""
  2. from __future__ import absolute_import
  3. import os
  4. import errno
  5. def makedirs(path):
  6. """Create directory recursively if not exists.
  7. Similar to `makedir -p`, you can skip checking existence before this function.
  8. Parameters
  9. ----------
  10. path : str
  11. Path of the desired dir
  12. """
  13. try:
  14. os.makedirs(path)
  15. except OSError as exc:
  16. if exc.errno != errno.EEXIST:
  17. raise
  18. def try_import(package, message=None):
  19. """Try import specified package, with custom message support.
  20. Parameters
  21. ----------
  22. package : str
  23. The name of the targeting package.
  24. message : str, default is None
  25. If not None, this function will raise customized error message when import error is found.
  26. Returns
  27. -------
  28. module if found, raise ImportError otherwise
  29. """
  30. try:
  31. return __import__(package)
  32. except ImportError as e:
  33. if not message:
  34. raise e
  35. raise ImportError(message)
  36. def try_import_cv2():
  37. """Try import cv2 at runtime.
  38. Returns
  39. -------
  40. cv2 module if found. Raise ImportError otherwise
  41. """
  42. msg = "cv2 is required, you can install by package manager, e.g. 'apt-get', \
  43. or `pip install opencv-python --user` (note that this is unofficial PYPI package)."
  44. return try_import('cv2', msg)
  45. def import_try_install(package, extern_url=None):
  46. """Try import the specified package.
  47. If the package not installed, try use pip to install and import if success.
  48. Parameters
  49. ----------
  50. package : str
  51. The name of the package trying to import.
  52. extern_url : str or None, optional
  53. The external url if package is not hosted on PyPI.
  54. For example, you can install a package using:
  55. "pip install git+http://github.com/user/repo/tarball/master/egginfo=xxx".
  56. In this case, you can pass the url to the extern_url.
  57. Returns
  58. -------
  59. <class 'Module'>
  60. The imported python module.
  61. """
  62. try:
  63. return __import__(package)
  64. except ImportError:
  65. try:
  66. from pip import main as pipmain
  67. except ImportError:
  68. from pip._internal import main as pipmain
  69. # trying to install package
  70. url = package if extern_url is None else extern_url
  71. pipmain(['install', '--user', url]) # will raise SystemExit Error if fails
  72. # trying to load again
  73. try:
  74. return __import__(package)
  75. except ImportError:
  76. import sys
  77. import site
  78. user_site = site.getusersitepackages()
  79. if user_site not in sys.path:
  80. sys.path.append(user_site)
  81. return __import__(package)
  82. return __import__(package)
  83. """Import helper for pycocotools"""
  84. # NOTE: for developers
  85. # please do not import any pycocotools in __init__ because we are trying to lazy
  86. # import pycocotools to avoid install it for other users who may not use it.
  87. # only import when you actually use it
  88. def try_import_pycocotools():
  89. """Tricks to optionally install and import pycocotools"""
  90. # first we can try import pycocotools
  91. try:
  92. import pycocotools as _
  93. except ImportError:
  94. import os
  95. # we need to install pycootools, which is a bit tricky
  96. # pycocotools sdist requires Cython, numpy(already met)
  97. import_try_install('cython')
  98. # pypi pycocotools is not compatible with windows
  99. win_url = 'git+https://github.com/zhreshold/cocoapi.git#subdirectory=PythonAPI'
  100. try:
  101. if os.name == 'nt':
  102. import_try_install('pycocotools', win_url)
  103. else:
  104. import_try_install('pycocotools')
  105. except ImportError:
  106. faq = 'cocoapi FAQ'
  107. raise ImportError('Cannot import or install pycocotools, please refer to %s.' % faq)