2 # -*- coding: UTF-8 -*-
4 # Takes given zone.tab file, or looks for expected locations on the system,
5 # and uses it to update dummy TIMEZONES file for i18n message extraction.
7 # Timezones read from zone.tab are only added into TIMEZONES,
8 # no timezone (or timezone comment) is removed from TIMEZONES.
9 # This in order not to lose i18n for timezones and comments
10 # found on various newer and older systems.
12 # Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
19 cmdname
= os
.path
.basename(sys
.argv
[0])
21 print >>sys
.stderr
, "%s: %s" % (cmdname
, msg
)
24 system_zonetab_paths
= [
25 "/usr/share/zoneinfo/zone.tab",
28 if len(sys
.argv
) not in (2, 3):
29 print >>sys
.stderr
, "usage: %s TIMEZONES_PATH [ZONETAB_PATH]" % cmdname
32 timezone_path
= sys
.argv
[1]
33 if not os
.path
.isfile(timezone_path
):
34 error("TIMEZONES file '%s' does not exist "
35 "(create manually an empty file if really starting from scratch")
37 if len(sys
.argv
) >= 3:
38 zonetab_path
= sys
.argv
[2]
39 if not os
.path
.isfile(zonetab_path
):
40 error("zone.tab file '%s' does not exist" % zonetab_path
)
41 print "using given zone.tab file at '%s'" % zonetab_path
43 for system_zonetab_path
in system_zonetab_paths
:
44 if os
.path
.isfile(system_zonetab_path
):
45 zonetab_path
= system_zonetab_path
48 error("cannot fine zone.tab file at any of known system locations")
49 print "found system zone.tab file at '%s'" % zonetab_path
51 # Parse current timezones into dictionary zone->[comments].
52 ifs
= codecs
.open(timezone_path
, "r", "UTF-8")
53 message_rx
= re
.compile(r
"i18n\(\"(.*?
)\"\
)")
54 tzcomment_rx = re.compile(r"^
// i18n
:.*comment
")
55 tzcomment_follows = False
56 current_timezones = {}
58 if tzcomment_rx.search(line):
59 tzcomment_follows = True
61 m = message_rx.search(line)
64 if not tzcomment_follows:
66 if tzone not in current_timezones:
67 current_timezones[tzone] = []
70 if tzcomment not in current_timezones[tzone]:
71 current_timezones[tzone].append(tzcomment)
72 tzcomment_follows = False
75 # Parse system timezones into same form.
76 ifs = codecs.open(zonetab_path, "r
", "UTF
-8")
79 if line.lstrip().startswith("#"):
81 line
= line
.rstrip("\n")
82 els
= line
.split("\t")
85 if tzone
not in system_timezones
:
86 system_timezones
[tzone
] = []
89 if tzcomment
not in system_timezones
[tzone
]:
90 system_timezones
[tzone
].append(tzcomment
)
93 # Compose new timezones by adding new timezones read from the system,
94 # and appending new comments to existing timezones.
96 num_new_tzcomments
= 0
97 for tzone
, tzcomments
in system_timezones
.items():
98 if tzone
not in current_timezones
:
99 current_timezones
[tzone
] = tzcomments
102 for tzcomment
in tzcomments
:
103 if tzcomment
not in current_timezones
[tzone
]:
104 current_timezones
[tzone
].append(tzcomment
)
105 num_new_tzcomments
+= 1
107 if num_new_tzones
or num_new_tzcomments
:
109 tzones
= current_timezones
.keys()
112 tzlines
.append("i18n(\"%s\");\n" % tzone
);
113 for tzcomment
in current_timezones
[tzone
]:
114 tzlines
.append("// i18n: comment to the previous timezone\n")
115 tzlines
.append("i18n(\"%s\");\n" % tzcomment
)
116 ofs
= codecs
.open(timezone_path
, "w", "UTF-8")
117 ofs
.writelines(tzlines
)
119 num_new
= num_new_tzones
+ num_new_tzcomments
120 print "added %d new timezones/comments to '%s'" % (num_new
, timezone_path
)
122 print "no new timezones/comments to add"