1#!/usr/bin/python3 -B
2
3# Copyright 2017 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"""Generates a time zone version file"""
18
19import argparse
20import os
21import shutil
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
31android_build_top = i18nutil.GetAndroidRootOrDie()
32android_host_out_dir = i18nutil.GetAndroidHostOutOrDie()
33timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top)
34i18nutil.CheckDirExists(timezone_dir, 'system/timezone')
35
36def RunCreateTzVersion(properties_file):
37  # Build the libraries needed.
38  tzdatautil.InvokeSoong(android_build_top, ['create_tz_version'])
39
40  # Run the CreateTzVersion tool
41  command = '%s/bin/create_tz_version' % android_host_out_dir
42  subprocess.check_call([command, properties_file])
43
44
45def CreateTzVersion(
46    iana_version, revision, output_version_file):
47  original_cwd = os.getcwd()
48
49  i18nutil.SwitchToNewTemporaryDirectory()
50  working_dir = os.getcwd()
51
52  # Generate the properties file.
53  properties_file = '%s/tzversion.properties' % working_dir
54  with open(properties_file, "w") as properties:
55    properties.write('rules.version=%s\n' % iana_version)
56    properties.write('revision=%s\n' % revision)
57    properties.write('output.version.file=%s\n' % output_version_file)
58
59  RunCreateTzVersion(properties_file)
60
61  os.chdir(original_cwd)
62
63
64def main():
65  parser = argparse.ArgumentParser()
66  parser.add_argument('-iana_version', required=True,
67      help='The IANA time zone rules release version, e.g. 2017b')
68  parser.add_argument('-revision', type=int, required = True,
69      help='Revision of the current IANA version')
70  parser.add_argument('-output_version_file', required=True,
71      help='The output path for the version file')
72  args = parser.parse_args()
73
74  iana_version = args.iana_version
75  revision = args.revision
76  output_version_file = os.path.abspath(args.output_version_file)
77
78  CreateTzVersion(
79      iana_version=iana_version,
80      revision=revision,
81      output_version_file=output_version_file)
82
83  print('Version file created as %s' % output_version_file)
84  sys.exit(0)
85
86
87if __name__ == '__main__':
88  main()
89