Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

_os.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os
  2. import tempfile
  3. from os.path import abspath, dirname, join, normcase, sep
  4. from pathlib import Path
  5. from django.core.exceptions import SuspiciousFileOperation
  6. def safe_join(base, *paths):
  7. """
  8. Join one or more path components to the base path component intelligently.
  9. Return a normalized, absolute version of the final path.
  10. Raise ValueError if the final path isn't located inside of the base path
  11. component.
  12. """
  13. final_path = abspath(join(base, *paths))
  14. base_path = abspath(base)
  15. # Ensure final_path starts with base_path (using normcase to ensure we
  16. # don't false-negative on case insensitive operating systems like Windows),
  17. # further, one of the following conditions must be true:
  18. # a) The next character is the path separator (to prevent conditions like
  19. # safe_join("/dir", "/../d"))
  20. # b) The final path must be the same as the base path.
  21. # c) The base path must be the most root path (meaning either "/" or "C:\\")
  22. if (
  23. not normcase(final_path).startswith(normcase(base_path + sep))
  24. and normcase(final_path) != normcase(base_path)
  25. and dirname(normcase(base_path)) != normcase(base_path)
  26. ):
  27. raise SuspiciousFileOperation(
  28. "The joined path ({}) is located outside of the base path "
  29. "component ({})".format(final_path, base_path)
  30. )
  31. return final_path
  32. def symlinks_supported():
  33. """
  34. Return whether or not creating symlinks are supported in the host platform
  35. and/or if they are allowed to be created (e.g. on Windows it requires admin
  36. permissions).
  37. """
  38. with tempfile.TemporaryDirectory() as temp_dir:
  39. original_path = os.path.join(temp_dir, "original")
  40. symlink_path = os.path.join(temp_dir, "symlink")
  41. os.makedirs(original_path)
  42. try:
  43. os.symlink(original_path, symlink_path)
  44. supported = True
  45. except (OSError, NotImplementedError):
  46. supported = False
  47. return supported
  48. def to_path(value):
  49. """Convert value to a pathlib.Path instance, if not already a Path."""
  50. if isinstance(value, Path):
  51. return value
  52. elif not isinstance(value, str):
  53. raise TypeError("Invalid path type: %s" % type(value).__name__)
  54. return Path(value)