Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/babel/localtime/_unix.py: 31%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

52 statements  

1import datetime 

2import os 

3import re 

4 

5from babel.localtime._helpers import ( 

6 _get_tzinfo, 

7 _get_tzinfo_from_file, 

8 _get_tzinfo_or_raise, 

9) 

10 

11 

12def _tz_from_env(tzenv: str) -> datetime.tzinfo: 

13 if tzenv[0] == ':': 

14 tzenv = tzenv[1:] 

15 

16 # TZ specifies a file 

17 if os.path.exists(tzenv): 

18 return _get_tzinfo_from_file(tzenv) 

19 

20 # TZ specifies a zoneinfo zone. 

21 return _get_tzinfo_or_raise(tzenv) 

22 

23 

24def _get_localzone(_root: str = '/') -> datetime.tzinfo: 

25 """Tries to find the local timezone configuration. 

26 This method prefers finding the timezone name and passing that to 

27 zoneinfo or pytz, over passing in the localtime file, as in the later 

28 case the zoneinfo name is unknown. 

29 The parameter _root makes the function look for files like /etc/localtime 

30 beneath the _root directory. This is primarily used by the tests. 

31 In normal usage you call the function without parameters. 

32 """ 

33 

34 tzenv = os.environ.get('TZ') 

35 if tzenv: 

36 return _tz_from_env(tzenv) 

37 

38 # This is actually a pretty reliable way to test for the local time 

39 # zone on operating systems like OS X. On OS X especially this is the 

40 # only one that actually works. 

41 try: 

42 link_dst = os.readlink('/etc/localtime') 

43 except OSError: 

44 pass 

45 else: 

46 pos = link_dst.find('/zoneinfo/') 

47 if pos >= 0: 

48 # On occasion, the `/etc/localtime` symlink has a double slash, e.g. 

49 # "/usr/share/zoneinfo//UTC", which would make `zoneinfo.ZoneInfo` 

50 # complain (no absolute paths allowed), and we'd end up returning 

51 # `None` (as a fix for #1092). 

52 # Instead, let's just "fix" the double slash symlink by stripping 

53 # leading slashes before passing the assumed zone name forward. 

54 zone_name = link_dst[pos + 10:].lstrip("/") 

55 tzinfo = _get_tzinfo(zone_name) 

56 if tzinfo is not None: 

57 return tzinfo 

58 

59 # Now look for distribution specific configuration files 

60 # that contain the timezone name. 

61 tzpath = os.path.join(_root, 'etc/timezone') 

62 if os.path.exists(tzpath): 

63 with open(tzpath, 'rb') as tzfile: 

64 data = tzfile.read() 

65 

66 # Issue #3 in tzlocal was that /etc/timezone was a zoneinfo file. 

67 # That's a misconfiguration, but we need to handle it gracefully: 

68 if data[:5] != b'TZif2': 

69 etctz = data.strip().decode() 

70 # Get rid of host definitions and comments: 

71 if ' ' in etctz: 

72 etctz, dummy = etctz.split(' ', 1) 

73 if '#' in etctz: 

74 etctz, dummy = etctz.split('#', 1) 

75 

76 return _get_tzinfo_or_raise(etctz.replace(' ', '_')) 

77 

78 # CentOS has a ZONE setting in /etc/sysconfig/clock, 

79 # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and 

80 # Gentoo has a TIMEZONE setting in /etc/conf.d/clock 

81 # We look through these files for a timezone: 

82 timezone_re = re.compile(r'\s*(TIME)?ZONE\s*=\s*"(?P<etctz>.+)"') 

83 

84 for filename in ('etc/sysconfig/clock', 'etc/conf.d/clock'): 

85 tzpath = os.path.join(_root, filename) 

86 if not os.path.exists(tzpath): 

87 continue 

88 with open(tzpath) as tzfile: 

89 for line in tzfile: 

90 match = timezone_re.match(line) 

91 if match is not None: 

92 # We found a timezone 

93 etctz = match.group("etctz") 

94 return _get_tzinfo_or_raise(etctz.replace(' ', '_')) 

95 

96 # No explicit setting existed. Use localtime 

97 for filename in ('etc/localtime', 'usr/local/etc/localtime'): 

98 tzpath = os.path.join(_root, filename) 

99 

100 if not os.path.exists(tzpath): 

101 continue 

102 return _get_tzinfo_from_file(tzpath) 

103 

104 raise LookupError('Can not find any timezone configuration')