pyimod01_os_path.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. ### **NOTE** This module is used during bootstrap.
  12. ### Import *ONLY* builtin modules.
  13. ### List of built-in modules: sys.builtin_module_names
  14. """
  15. Set up 'os' and 'os.path' module replacement functions for use during import
  16. bootstrap.
  17. """
  18. import sys
  19. _builtin_names = sys.builtin_module_names
  20. _mindirlen = 0
  21. # Wrap os.environ, os.listdir(), os.sep
  22. # We cannot cache the content of os.listdir(). It was found to cause problems
  23. # with programs that dynamically add python modules to be reimported by that
  24. # same program (i.e., plugins), because the cache is only built once
  25. # at the beginning, and never updated. So, we must really list the directory
  26. # again.
  27. if 'posix' in _builtin_names: # For Linux, Unix, Mac OS X
  28. from posix import environ as os_environ
  29. from posix import listdir as os_listdir
  30. os_sep = '/'
  31. _mindirlen = 1
  32. elif 'nt' in _builtin_names: # For Windows
  33. from nt import environ as os_environ
  34. from nt import listdir as os_listdir
  35. os_sep = '\\'
  36. _mindirlen = 3
  37. else:
  38. raise ImportError('No os specific module found')
  39. # Wrap os.path.join()
  40. def os_path_join(a, b, sep=os_sep):
  41. if a == '':
  42. return b
  43. lastchar = a[-1:]
  44. if lastchar == '/' or lastchar == sep:
  45. return a + b
  46. return a + sep + b
  47. # Wrap os.path.dirname()
  48. def os_path_dirname(a, sep=os_sep, mindirlen=_mindirlen):
  49. for i in range(len(a) - 1, -1, -1):
  50. c = a[i]
  51. if c == '/' or c == sep:
  52. if i < mindirlen:
  53. return a[:i + 1]
  54. return a[:i]
  55. return ''
  56. # Wrap os.path.basename()
  57. if sys.platform.startswith('win'):
  58. # Implementation from ntpath.py module
  59. # from standard Python 2.7 Library.
  60. def os_path_basename(pth):
  61. ## Implementation of os.path.splitdrive()
  62. if pth[1:2] == ':':
  63. d = pth[0:2]
  64. p = pth[2:]
  65. else:
  66. d = ''
  67. p = pth
  68. ## Implementation of os.path.split()
  69. # set i to index beyond p's last slash
  70. i = len(p)
  71. while i and p[i - 1] not in '/\\':
  72. i = i - 1
  73. head, tail = p[:i], p[i:] # now tail has no slashes
  74. # Windows implementation is based on split(). We need
  75. # to return only tail.
  76. return tail
  77. else:
  78. # Implementation from ntpath.py module
  79. # from standard Python 2.7 Library.
  80. def os_path_basename(pth):
  81. i = pth.rfind('/') + 1
  82. return pth[i:]
  83. if 'PYTHONCASEOK' not in os_environ:
  84. def caseOk(filename):
  85. files = os_listdir(os_path_dirname(filename))
  86. return os_path_basename(filename) in files
  87. else:
  88. def caseOk(filename):
  89. return True