From 3aec03c1ed100acd0cc5a59e748f066be58e5e56 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Fri, 6 Dec 2019 09:56:59 +0100 Subject: [PATCH] New python script for zone generation Replace the old C# script with a python script to generate WindowsZones.c, update to new location of XML --- scripts/WindowsZones.csx | 91 --------------------------------- scripts/update-windows-zones.py | 41 +++++++++++++++ 2 files changed, 41 insertions(+), 91 deletions(-) delete mode 100644 scripts/WindowsZones.csx create mode 100755 scripts/update-windows-zones.py diff --git a/scripts/WindowsZones.csx b/scripts/WindowsZones.csx deleted file mode 100644 index 2fe59caa1..000000000 --- a/scripts/WindowsZones.csx +++ /dev/null @@ -1,91 +0,0 @@ -/** - * FreeRDP: A Remote Desktop Protocol Implementation - * TZID to Windows TimeZone Identifier Table Generator - * - * Copyright 2012 Marc-Andre Moreau - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* Run with ' csi scripts/WindowsZones.csx' from freerdp checkout root */ - -#r "System.Xml" - -using System; -using System.IO; -using System.Xml; -using System.Text; -using System.Collections; -using System.Collections.Generic; - -/* - * this script uses windowsZones.xml which can be obtained at: - * http://www.unicode.org/repos/cldr/tags/latest/common/supplemental/windowsZones.xml - */ - -string tzid, windows; -const string file = @"winpr/libwinpr/timezone/WindowsZones.c"; -const string zonesUrl = @"http://www.unicode.org/repos/cldr/tags/latest/common/supplemental/windowsZones.xml"; -List list = new List(); -StreamWriter stream = new StreamWriter(file, false); -XmlTextReader reader = new XmlTextReader(zonesUrl); - -Console.WriteLine("Updating " + file); -stream.WriteLine("/* "); -stream.WriteLine(" * Automatically generated with scripts/WindowsZones.csx"); -stream.WriteLine(" */ "); -stream.WriteLine(); - -stream.WriteLine("struct _WINDOWS_TZID_ENTRY"); -stream.WriteLine("{"); -stream.WriteLine("\tconst char* windows;"); -stream.WriteLine("\tconst char* tzid;"); -stream.WriteLine("};"); -stream.WriteLine("typedef struct _WINDOWS_TZID_ENTRY WINDOWS_TZID_ENTRY;"); -stream.WriteLine(); - -while (reader.Read()) -{ - switch (reader.NodeType) - { - case XmlNodeType.Element: - - if (reader.Name.Equals("mapZone")) - { - tzid = reader.GetAttribute("type"); - windows = reader.GetAttribute("other"); - - string entry = String.Format("\"{0}\", \"{1}\"", windows, tzid); - - if (!list.Contains(entry)) - list.Add(entry); - } - - break; - } -} - -list.Sort(); - -stream.WriteLine("const WINDOWS_TZID_ENTRY WindowsTimeZoneIdTable[] ="); -stream.WriteLine("{"); - -foreach (string entry in list) -{ - stream.Write("\t{ "); - stream.Write(entry); - stream.WriteLine(" },"); -} - -stream.WriteLine("};"); - -stream.Close(); diff --git a/scripts/update-windows-zones.py b/scripts/update-windows-zones.py new file mode 100755 index 000000000..49b96e6d7 --- /dev/null +++ b/scripts/update-windows-zones.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import os +import urllib.request +import xml.etree.ElementTree as ET + +name = os.path.realpath(__file__) +base = os.path.normpath(os.path.join(os.path.dirname(name), '..')) +rname = os.path.relpath(name, base) +zfile = os.path.join(base, 'winpr/libwinpr/timezone/WindowsZones.c') +url = 'https://raw.githubusercontent.com/unicode-org/cldr/latest/common/supplemental/windowsZones.xml' + +try: + with urllib.request.urlopen(url) as response: + xml = response.read() + root = ET.fromstring(xml) + entries = [] + for child in root.iter('mapZone'): + tzid = child.get('type') + windows = child.get('other') + entries += ['\t{ "' + windows + '", "' + tzid + '" },\n'] + entries.sort() + + with open(zfile, 'w') as f: + f.write('/*\n') + f.write(' * Automatically generated with ' + str(rname) + '\n') + f.write(' */\n') + f.write('\n') + f.write('#include "WindowsZones.h"\n') + f.write('\n') + f.write('const WINDOWS_TZID_ENTRY WindowsTimeZoneIdTable[] =\n') + f.write('{\n') + for entry in entries: + f.write(entry) + f.write('};\n') + f.write('\n'); + f.write('const size_t WindowsTimeZoneIdTableNrElements = ARRAYSIZE(WindowsTimeZoneIdTable);\n') +except Exception as e: + print('----------------------------------------------------') + print(str(e)) + print('----------------------------------------------------')