_tzpath.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import os
  2. import sysconfig
  3. def reset_tzpath(to=None):
  4. global TZPATH
  5. tzpaths = to
  6. if tzpaths is not None:
  7. if isinstance(tzpaths, (str, bytes)):
  8. raise TypeError(
  9. f"tzpaths must be a list or tuple, "
  10. + f"not {type(tzpaths)}: {tzpaths!r}"
  11. )
  12. if not all(map(os.path.isabs, tzpaths)):
  13. raise ValueError(_get_invalid_paths_message(tzpaths))
  14. base_tzpath = tzpaths
  15. else:
  16. env_var = os.environ.get("PYTHONTZPATH", None)
  17. if env_var is not None:
  18. base_tzpath = _parse_python_tzpath(env_var)
  19. else:
  20. base_tzpath = _parse_python_tzpath(
  21. sysconfig.get_config_var("TZPATH")
  22. )
  23. TZPATH = tuple(base_tzpath)
  24. def _parse_python_tzpath(env_var):
  25. if not env_var:
  26. return ()
  27. raw_tzpath = env_var.split(os.pathsep)
  28. new_tzpath = tuple(filter(os.path.isabs, raw_tzpath))
  29. # If anything has been filtered out, we will warn about it
  30. if len(new_tzpath) != len(raw_tzpath):
  31. import warnings
  32. msg = _get_invalid_paths_message(raw_tzpath)
  33. warnings.warn(
  34. "Invalid paths specified in PYTHONTZPATH environment variable. "
  35. + msg,
  36. InvalidTZPathWarning,
  37. )
  38. return new_tzpath
  39. def _get_invalid_paths_message(tzpaths):
  40. invalid_paths = (path for path in tzpaths if not os.path.isabs(path))
  41. prefix = "\n "
  42. indented_str = prefix + prefix.join(invalid_paths)
  43. return (
  44. "Paths should be absolute but found the following relative paths:"
  45. + indented_str
  46. )
  47. def find_tzfile(key):
  48. """Retrieve the path to a TZif file from a key."""
  49. _validate_tzfile_path(key)
  50. for search_path in TZPATH:
  51. filepath = os.path.join(search_path, key)
  52. if os.path.isfile(filepath):
  53. return filepath
  54. return None
  55. _TEST_PATH = os.path.normpath(os.path.join("_", "_"))[:-1]
  56. def _validate_tzfile_path(path, _base=_TEST_PATH):
  57. if os.path.isabs(path):
  58. raise ValueError(
  59. f"ZoneInfo keys may not be absolute paths, got: {path}"
  60. )
  61. # We only care about the kinds of path normalizations that would change the
  62. # length of the key - e.g. a/../b -> a/b, or a/b/ -> a/b. On Windows,
  63. # normpath will also change from a/b to a\b, but that would still preserve
  64. # the length.
  65. new_path = os.path.normpath(path)
  66. if len(new_path) != len(path):
  67. raise ValueError(
  68. f"ZoneInfo keys must be normalized relative paths, got: {path}"
  69. )
  70. resolved = os.path.normpath(os.path.join(_base, new_path))
  71. if not resolved.startswith(_base):
  72. raise ValueError(
  73. f"ZoneInfo keys must refer to subdirectories of TZPATH, got: {path}"
  74. )
  75. del _TEST_PATH
  76. def available_timezones():
  77. """Returns a set containing all available time zones.
  78. .. caution::
  79. This may attempt to open a large number of files, since the best way to
  80. determine if a given file on the time zone search path is to open it
  81. and check for the "magic string" at the beginning.
  82. """
  83. from importlib import resources
  84. valid_zones = set()
  85. # Start with loading from the tzdata package if it exists: this has a
  86. # pre-assembled list of zones that only requires opening one file.
  87. try:
  88. with resources.files("tzdata").joinpath("zones").open("r") as f:
  89. for zone in f:
  90. zone = zone.strip()
  91. if zone:
  92. valid_zones.add(zone)
  93. except (ImportError, FileNotFoundError):
  94. pass
  95. def valid_key(fpath):
  96. try:
  97. with open(fpath, "rb") as f:
  98. return f.read(4) == b"TZif"
  99. except Exception: # pragma: nocover
  100. return False
  101. for tz_root in TZPATH:
  102. if not os.path.exists(tz_root):
  103. continue
  104. for root, dirnames, files in os.walk(tz_root):
  105. if root == tz_root:
  106. # right/ and posix/ are special directories and shouldn't be
  107. # included in the output of available zones
  108. if "right" in dirnames:
  109. dirnames.remove("right")
  110. if "posix" in dirnames:
  111. dirnames.remove("posix")
  112. for file in files:
  113. fpath = os.path.join(root, file)
  114. key = os.path.relpath(fpath, start=tz_root)
  115. if os.sep != "/": # pragma: nocover
  116. key = key.replace(os.sep, "/")
  117. if not key or key in valid_zones:
  118. continue
  119. if valid_key(fpath):
  120. valid_zones.add(key)
  121. if "posixrules" in valid_zones:
  122. # posixrules is a special symlink-only time zone where it exists, it
  123. # should not be included in the output
  124. valid_zones.remove("posixrules")
  125. return valid_zones
  126. class InvalidTZPathWarning(RuntimeWarning):
  127. """Warning raised if an invalid path is specified in PYTHONTZPATH."""
  128. TZPATH = ()
  129. reset_tzpath()