File indexing completed on 2024-04-21 14:55:45

0001 #!/usr/bin/env python
0002 # -*- coding: UTF-8 -*-
0003 #
0004 # Takes given zone.tab file, or looks for expected locations on the system,
0005 # and uses it to update dummy TIMEZONES file for i18n message extraction.
0006 #
0007 # Timezones read from zone.tab are only added into TIMEZONES,
0008 # no timezone (or timezone comment) is removed from TIMEZONES.
0009 # This in order not to lose i18n for timezones and comments
0010 # found on various newer and older systems.
0011 #
0012 # Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0013 
0014 import sys
0015 import os
0016 import codecs
0017 import re
0018 
0019 cmdname = os.path.basename(sys.argv[0])
0020 def error (msg):
0021     print >>sys.stderr, "%s: %s" % (cmdname, msg)
0022     sys.exit(1)
0023 
0024 system_zonetab_paths = [
0025     "/usr/share/zoneinfo/zone.tab",
0026 ]
0027 
0028 if len(sys.argv) not in (2, 3):
0029     print >>sys.stderr, "usage: %s TIMEZONES_PATH [ZONETAB_PATH]" % cmdname
0030     sys.exit(1)
0031 
0032 timezone_path = sys.argv[1]
0033 if not os.path.isfile(timezone_path):
0034     error("TIMEZONES file '%s' does not exist "
0035           "(create manually an empty file if really starting from scratch")
0036 
0037 if len(sys.argv) >= 3:
0038     zonetab_path = sys.argv[2]
0039     if not os.path.isfile(zonetab_path):
0040         error("zone.tab file '%s' does not exist" % zonetab_path)
0041     print "using given zone.tab file at '%s'" % zonetab_path
0042 else:
0043     for system_zonetab_path in system_zonetab_paths:
0044         if os.path.isfile(system_zonetab_path):
0045             zonetab_path = system_zonetab_path
0046             break
0047     if not zonetab_path:
0048         error("cannot fine zone.tab file at any of known system locations")
0049     print "found system zone.tab file at '%s'" % zonetab_path
0050 
0051 # Parse current timezones into dictionary zone->[comments].
0052 ifs = codecs.open(timezone_path, "r", "UTF-8")
0053 message_rx = re.compile(r"i18n\(\"(.*?)\"\)")
0054 tzcomment_rx = re.compile(r"^// i18n:.*comment")
0055 tzcomment_follows = False
0056 current_timezones = {}
0057 for line in ifs:
0058     if tzcomment_rx.search(line):
0059         tzcomment_follows = True
0060     else:
0061         m = message_rx.search(line)
0062         if m:
0063             message = m.group(1)
0064             if not tzcomment_follows:
0065                 tzone = message
0066                 if tzone not in current_timezones:
0067                     current_timezones[tzone] = []
0068             else:
0069                 tzcomment = message
0070                 if tzcomment not in current_timezones[tzone]:
0071                     current_timezones[tzone].append(tzcomment)
0072                 tzcomment_follows = False
0073 ifs.close()
0074 
0075 # Parse system timezones into same form.
0076 ifs = codecs.open(zonetab_path, "r", "UTF-8")
0077 system_timezones = {}
0078 for line in ifs:
0079     if line.lstrip().startswith("#"):
0080         continue
0081     line = line.rstrip("\n")
0082     els = line.split("\t")
0083     if len(els) >= 3:
0084         tzone = els[2]
0085         if tzone not in system_timezones:
0086             system_timezones[tzone] = []
0087         if len(els) >= 4:
0088             tzcomment = els[3]
0089             if tzcomment not in system_timezones[tzone]:
0090                 system_timezones[tzone].append(tzcomment)
0091 ifs.close()
0092 
0093 # Compose new timezones by adding new timezones read from the system,
0094 # and appending new comments to existing timezones.
0095 num_new_tzones = 0
0096 num_new_tzcomments = 0
0097 for tzone, tzcomments in system_timezones.items():
0098     if tzone not in current_timezones:
0099         current_timezones[tzone] = tzcomments
0100         num_new_tzones += 1
0101     else:
0102         for tzcomment in tzcomments:
0103             if tzcomment not in current_timezones[tzone]:
0104                 current_timezones[tzone].append(tzcomment)
0105                 num_new_tzcomments += 1
0106 
0107 if num_new_tzones or num_new_tzcomments:
0108     tzlines = []
0109     tzones = current_timezones.keys()
0110     tzones.sort()
0111     for tzone in tzones:
0112         tzlines.append("i18n(\"%s\");\n" % tzone);
0113         for tzcomment in current_timezones[tzone]:
0114             tzlines.append("// i18n: comment to the previous timezone\n")
0115             tzlines.append("i18n(\"%s\");\n" % tzcomment)
0116     ofs = codecs.open(timezone_path, "w", "UTF-8")
0117     ofs.writelines(tzlines)
0118     ofs.close()
0119     num_new = num_new_tzones + num_new_tzcomments
0120     print "added %d new timezones/comments to '%s'" % (num_new, timezone_path)
0121 else:
0122     print "no new timezones/comments to add"