/django/utils/_os.py

https://github.com/blacktear23/django · Python · 67 lines · 30 code · 4 blank · 33 comment · 9 complexity · 9255f3878f407bb3465ef02df6209487 MD5 · raw file

  1. import os
  2. import stat
  3. from os.path import join, normcase, normpath, abspath, isabs, sep
  4. from django.utils.encoding import force_unicode
  5. # Define our own abspath function that can handle joining
  6. # unicode paths to a current working directory that has non-ASCII
  7. # characters in it. This isn't necessary on Windows since the
  8. # Windows version of abspath handles this correctly. The Windows
  9. # abspath also handles drive letters differently than the pure
  10. # Python implementation, so it's best not to replace it.
  11. if os.name == 'nt':
  12. abspathu = abspath
  13. else:
  14. def abspathu(path):
  15. """
  16. Version of os.path.abspath that uses the unicode representation
  17. of the current working directory, thus avoiding a UnicodeDecodeError
  18. in join when the cwd has non-ASCII characters.
  19. """
  20. if not isabs(path):
  21. path = join(os.getcwdu(), path)
  22. return normpath(path)
  23. def safe_join(base, *paths):
  24. """
  25. Joins one or more path components to the base path component intelligently.
  26. Returns a normalized, absolute version of the final path.
  27. The final path must be located inside of the base path component (otherwise
  28. a ValueError is raised).
  29. """
  30. # We need to use normcase to ensure we don't false-negative on case
  31. # insensitive operating systems (like Windows).
  32. base = force_unicode(base)
  33. paths = [force_unicode(p) for p in paths]
  34. final_path = normcase(abspathu(join(base, *paths)))
  35. base_path = normcase(abspathu(base))
  36. base_path_len = len(base_path)
  37. # Ensure final_path starts with base_path and that the next character after
  38. # the final path is os.sep (or nothing, in which case final_path must be
  39. # equal to base_path).
  40. if not final_path.startswith(base_path) \
  41. or final_path[base_path_len:base_path_len+1] not in ('', sep):
  42. raise ValueError('The joined path (%s) is located outside of the base '
  43. 'path component (%s)' % (final_path, base_path))
  44. return final_path
  45. def rmtree_errorhandler(func, path, exc_info):
  46. """
  47. On Windows, some files are read-only (e.g. in in .svn dirs), so when
  48. rmtree() tries to remove them, an exception is thrown.
  49. We catch that here, remove the read-only attribute, and hopefully
  50. continue without problems.
  51. """
  52. exctype, value = exc_info[:2]
  53. # lookin for a windows error
  54. if exctype is not WindowsError or 'Access is denied' not in str(value):
  55. raise
  56. # file type should currently be read only
  57. if ((os.stat(path).st_mode & stat.S_IREAD) != stat.S_IREAD):
  58. raise
  59. # convert to read/write
  60. os.chmod(path, stat.S_IWRITE)
  61. # use the original function to repeat the operation
  62. func(path)