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