1#!/usr/bin/python3 -B
2
3# Copyright 2020 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"""Allows visualization of zone trees (the thing that works out if zones are distinct)."""
18
19import argparse
20import glob
21import os
22import subprocess
23import sys
24
25sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
26import i18nutil
27
28sys.path.append('%s/system/timezone' % os.environ.get('ANDROID_BUILD_TOP'))
29import tzdatautil
30
31
32# Calculate the paths that are referred to by multiple functions.
33android_build_top = i18nutil.GetAndroidRootOrDie()
34timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top)
35i18nutil.CheckDirExists(timezone_dir, 'system/timezone')
36
37android_host_out = i18nutil.GetAndroidHostOutOrDie()
38
39debug_tools_dir = os.path.realpath('%s/system/timezone/debug_tools/host' % android_build_top)
40i18nutil.CheckDirExists(debug_tools_dir, 'system/timezone/debug_tools/host')
41
42
43def BuildAndRunTool(country_zones_txt, country_code, output_dir):
44  tzdatautil.InvokeSoong(android_build_top, ['unique_zones_visualizer'])
45  jar_file = '%s/framework/unique_zones_visualizer.jar' % android_host_out
46  subprocess.check_call(['java', '-jar', jar_file, country_zones_txt, country_code, output_dir])
47
48def CreatePngs(output_dir):
49  gv_files = glob.glob('%s/*.gv' % output_dir)
50  for gv_file in gv_files:
51    png_file = gv_file.replace('.gv', '.png')
52    print('Generating %s...' % png_file)
53    with open(png_file, 'w') as out_file:
54      subprocess.check_call(['dot', '-Tpng', gv_file], stdout=out_file)
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('-input', required=True,
65      help='The country_zones.txt file to process')
66  parser.add_argument('-country_code', required=True,
67      help='The country code (e.g. "us") to process')
68  parser.add_argument('-output', required=True,
69      help='The output directory for the dump')
70  args = parser.parse_args()
71
72  country_zones_txt = args.input
73  country_code = args.country_code
74  output_dir = args.output
75
76  CheckFileExists(country_zones_txt, '-input')
77  if not os.path.exists(output_dir):
78    print('Creating dir: %s'  % output_dir)
79    os.mkdir(output_dir)
80  i18nutil.CheckDirExists(output_dir, '-output')
81
82  BuildAndRunTool(country_zones_txt, country_code, output_dir)
83  CreatePngs(output_dir)
84
85  print('Look in %s for all generated files' % output_dir)
86  sys.exit(0)
87
88
89if __name__ == '__main__':
90  main()
91