1#!/usr/bin/python -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
19from __future__ import print_function
20
21import argparse
22import os
23import subprocess
24import sys
25
26sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
27import i18nutil
28
29sys.path.append('%s/system/timezone' % os.environ.get('ANDROID_BUILD_TOP'))
30import tzdatautil
31
32
33# Calculate the paths that are referred to by multiple functions.
34android_build_top = i18nutil.GetAndroidRootOrDie()
35timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top)
36i18nutil.CheckDirExists(timezone_dir, 'system/timezone')
37
38android_host_out = i18nutil.GetAndroidHostOutOrDie()
39
40debug_tools_dir = os.path.realpath('%s/system/timezone/debug_tools/host' % android_build_top)
41i18nutil.CheckDirExists(debug_tools_dir, 'system/timezone/debug_tools/host')
42
43
44def BuildDebugTools():
45  tzdatautil.InvokeSoong(android_build_top, ['zone_splitter', 'tz_file_dumper'])
46
47
48def SplitTzData(tzdata_file, output_dir):
49  jar_file = '%s/framework/zone_splitter.jar' % android_host_out
50  subprocess.check_call(['java', '-jar', jar_file, tzdata_file, output_dir])
51
52
53def CreateCsvFiles(zones_dir, csvs_dir):
54  jar_file = '%s/framework/tz_file_dumper.jar' % android_host_out
55  subprocess.check_call(['java', '-jar', jar_file, zones_dir, csvs_dir])
56
57
58def CheckFileExists(file, filename):
59  if not os.path.isfile(file):
60    print("Couldn't find %s (%s)!" % (filename, file))
61    sys.exit(1)
62
63
64def main():
65  parser = argparse.ArgumentParser()
66  parser.add_argument('-tzdata', required=True,
67      help='The tzdata file to process')
68  parser.add_argument('-output', required=True,
69      help='The output directory for the dump')
70  args = parser.parse_args()
71
72  tzdata_file = args.tzdata
73  output_dir = args.output
74
75  CheckFileExists(tzdata_file, '-tzdata')
76  if not os.path.exists(output_dir):
77    print('Creating dir: %s'  % output_dir)
78    os.mkdir(output_dir)
79  i18nutil.CheckDirExists(output_dir, '-output')
80
81  BuildDebugTools()
82
83  SplitTzData(tzdata_file, output_dir)
84
85  zones_dir = '%s/zones' % output_dir
86  csvs_dir = '%s/csvs' % output_dir
87
88  i18nutil.CheckDirExists(zones_dir, 'zones output dir')
89  if not os.path.exists(csvs_dir):
90    os.mkdir(csvs_dir)
91
92  CreateCsvFiles(zones_dir, csvs_dir)
93
94  print('Look in %s for all extracted files' % output_dir)
95  print('Look in %s for dumped CSVs' % csvs_dir)
96  sys.exit(0)
97
98
99if __name__ == '__main__':
100  main()
101