pyimod01_os_path.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 bootstrap.
  16. """
  17. import sys
  18. _builtin_names = sys.builtin_module_names
  19. _mindirlen = 0
  20. # Wrap os.environ, os.listdir(), os.sep
  21. # We cannot cache the content of os.listdir(). It was found to cause problems with programs that dynamically add python
  22. # modules to be reimported by that same program (i.e., plugins), because the cache is only built once at the beginning,
  23. # and never updated. So, we must really list the directory again.
  24. if 'posix' in _builtin_names: # For Linux, Unix, Mac OS
  25. from posix import environ as os_environ
  26. from posix import listdir as os_listdir
  27. os_sep = '/'
  28. _mindirlen = 1
  29. elif 'nt' in _builtin_names: # For Windows
  30. from nt import environ as os_environ
  31. from nt import listdir as os_listdir
  32. os_sep = '\\'
  33. _mindirlen = 3
  34. else:
  35. raise ImportError('No OS-specific module found!')
  36. # Wrap os.path.join()
  37. def os_path_join(a, b, sep=os_sep):
  38. if a == '':
  39. return b
  40. lastchar = a[-1:]
  41. if lastchar == '/' or lastchar == sep:
  42. return a + b
  43. return a + sep + b
  44. # Wrap os.path.dirname()
  45. def os_path_dirname(a, sep=os_sep, mindirlen=_mindirlen):
  46. for i in range(len(a) - 1, -1, -1):
  47. c = a[i]
  48. if c == '/' or c == sep:
  49. if i < mindirlen:
  50. return a[:i + 1]
  51. return a[:i]
  52. return ''
  53. # Wrap os.path.basename()
  54. if sys.platform.startswith('win'):
  55. # Implementation from ntpath.py module from standard Python 2.7 Library.
  56. def os_path_basename(pth):
  57. # Implementation of os.path.splitdrive()
  58. if pth[1:2] == ':':
  59. p = pth[2:]
  60. else:
  61. p = pth
  62. # Implementation of os.path.split(): set i to index beyond p's last slash.
  63. i = len(p)
  64. while i and p[i - 1] not in '/\\':
  65. i = i - 1
  66. # Windows implementation is based on split(). We need to return only tail (which now contains no slashes).
  67. return p[i:]
  68. else:
  69. # Implementation from ntpath.py module from standard Python 2.7 Library.
  70. def os_path_basename(pth):
  71. i = pth.rfind('/') + 1
  72. return pth[i:]
  73. if 'PYTHONCASEOK' not in os_environ:
  74. def caseOk(filename):
  75. files = os_listdir(os_path_dirname(filename))
  76. return os_path_basename(filename) in files
  77. else:
  78. def caseOk(filename):
  79. return True