예제 #1
0
 def test_getTZExtrasPath(self):
     """
     Make sure TimezoneCache._getTZExtrasPath returns a valid file.
     """
     extras = TimezoneCache._getTZExtrasPath()
     self.assertTrue(os.path.isfile(extras))
예제 #2
0
def _doRefresh(tzpath, xmlfile, tzdb, tzvers):
    """
    Refresh data from IANA.
    """

    print("Downloading latest data from IANA")
    if tzvers:
        path = "https://www.iana.org/time-zones/repository/releases/tzdata{}.tar.gz".format(
            tzvers, )
    else:
        path = "https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz"
    data = urllib.urlretrieve(path)
    print("Extract data at: {}".format(data[0]))
    rootdir = tempfile.mkdtemp()
    zonedir = os.path.join(rootdir, "tzdata")
    os.mkdir(zonedir)
    with tarfile.open(data[0], "r:gz") as t:
        t.extractall(zonedir)

    # Get the version from the Makefile
    try:
        with open(os.path.join(zonedir, "Makefile")) as f:
            makefile = f.read()
        lines = makefile.splitlines()
        for line in lines:
            if line.startswith("VERSION="):
                tzvers = line[8:].strip()
                break
    except IOError:
        pass

    # Work around change to MAKEFILE by trying to extract the version from the NEWS file
    if tzvers == "unknown":
        tzvers = None
        try:
            with open(os.path.join(zonedir, "NEWS")) as f:
                makefile = f.read()
            lines = makefile.splitlines()
            for line in lines:
                if line.startswith("Release "):
                    tzvers = line.split()[1]
                    break
        except IOError:
            pass

    if not tzvers:
        tzvers = DateTime.getToday().getText()
    print("Converting data (version: {}) at: {}".format(
        tzvers,
        zonedir,
    ))
    startYear = 1800
    endYear = DateTime.getToday().getYear() + 10
    Calendar.sProdID = "-//calendarserver.org//Zonal//EN"
    zonefiles = "northamerica", "southamerica", "europe", "africa", "asia", "australasia", "antarctica", "etcetera", "backward"
    parser = tzconvert()
    for file in zonefiles:
        parser.parse(os.path.join(zonedir, file))

    # Try tzextras
    extras = TimezoneCache._getTZExtrasPath()
    if os.path.exists(extras):
        print("Converting extra data at: {}".format(extras, ))
        parser.parse(extras)
    else:
        print("No extra data to convert")

    # Check for windows aliases
    print("Downloading latest data from unicode.org")
    path = "http://unicode.org/repos/cldr/tags/latest/common/supplemental/windowsZones.xml"
    data = urllib.urlretrieve(path)
    wpath = data[0]

    # Generate the iCalendar data
    print("Generating iCalendar data")
    parser.generateZoneinfoFiles(os.path.join(rootdir, "zoneinfo"),
                                 startYear,
                                 endYear,
                                 windowsAliases=wpath,
                                 filterzones=())

    print("Copy new zoneinfo to destination: {}".format(tzpath, ))
    z = FilePath(os.path.join(rootdir, "zoneinfo"))
    tz = FilePath(tzpath)
    z.copyTo(tz)
    print("Updating XML file at: {}".format(xmlfile, ))
    tzdb.readDatabase()
    tzdb.updateDatabase()
    print("Current total: {}".format(len(tzdb.timezones), ))
    print("Total Changed: {}".format(tzdb.changeCount, ))
    if tzdb.changeCount:
        print("Changed:")
        for k in sorted(tzdb.changed):
            print("  {}".format(k, ))

    versfile = os.path.join(os.path.dirname(xmlfile), "version.txt")
    print("Updating version file at: {}".format(versfile, ))
    with open(versfile, "w") as f:
        f.write(TimezoneCache.IANA_VERSION_PREFIX + tzvers)