1#!/usr/bin/python3 -B
2
3# Copyright 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Dumps the contents of a tzdata file."""
18
19import argparse
20import os
21import subprocess
22import sys
23
24sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
25import i18nutil
26
27sys.path.append('%s/system/timezone' % os.environ.get('ANDROID_BUILD_TOP'))
28import tzdatautil
29
30
31# Calculate the paths that are referred to by multiple functions.
32android_build_top = i18nutil.GetAndroidRootOrDie()
33timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top)
34i18nutil.CheckDirExists(timezone_dir, 'system/timezone')
35
36android_host_out = i18nutil.GetAndroidHostOutOrDie()
37
38debug_tools_dir = os.path.realpath('%s/system/timezone/debug_tools/host' % android_build_top)
39i18nutil.CheckDirExists(debug_tools_dir, 'system/timezone/debug_tools/host')
40
41
42def BuildDebugTools():
43  tzdatautil.InvokeSoong(android_build_top, ['zone_splitter', 'tz_file_dumper'])
44
45
46def SplitTzData(tzdata_file, output_dir):
47  jar_file = '%s/framework/zone_splitter.jar' % android_host_out
48  subprocess.check_call(['java', '-jar', jar_file, tzdata_file, output_dir])
49
50
51def CreateCsvFiles(zones_dir, csvs_dir):
52  jar_file = '%s/framework/tz_file_dumper.jar' % android_host_out
53  subprocess.check_call(['java', '-jar', jar_file, zones_dir, csvs_dir])
54
55
56def CheckFileExists(file, filename):
57  if not os.path.isfile(file):
58    print("Couldn't find %s (%s)!" % (filename, file))
59    sys.exit(1)
60
61
62def main():
63  parser = argparse.ArgumentParser()
64  parser.add_argument('-tzdata', required=True,
65      help='The tzdata file to process')
66  parser.add_argument('-output', required=True,
67      help='The output directory for the dump')
68  args = parser.parse_args()
69
70  tzdata_file = args.tzdata
71  output_dir = args.output
72
73  CheckFileExists(tzdata_file, '-tzdata')
74  if not os.path.exists(output_dir):
75    print('Creating dir: %s'  % output_dir)
76    os.mkdir(output_dir)
77  i18nutil.CheckDirExists(output_dir, '-output')
78
79  BuildDebugTools()
80
81  SplitTzData(tzdata_file, output_dir)
82
83  zones_dir = '%s/zones' % output_dir
84  csvs_dir = '%s/csvs' % output_dir
85
86  i18nutil.CheckDirExists(zones_dir, 'zones output dir')
87  if not os.path.exists(csvs_dir):
88    os.mkdir(csvs_dir)
89
90  CreateCsvFiles(zones_dir, csvs_dir)
91
92  print('Look in %s for all extracted files' % output_dir)
93  print('Look in %s for dumped CSVs' % csvs_dir)
94  sys.exit(0)
95
96
97if __name__ == '__main__':
98  main()
99