update-tzdata.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/sh
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. set -e
  6. # Usage: update-tzdata.sh <tzdata version>
  7. # E.g., for tzdata2016f: update-tzdata.sh 2016f
  8. # Ensure that $Date$ in the checked-out git files expands timezone-agnostically,
  9. # so that this script's behavior is consistent when run from any time zone.
  10. export TZ=UTC
  11. # Also ensure GIT-INFO is consistently English.
  12. export LANG=en_US.UTF-8
  13. export LANGUAGE=en_US
  14. export LC_ALL=en_US.UTF-8
  15. # Path to icupkg executable, typically located at $ICU_DIR/bin/icupkg.
  16. icu_pkg=
  17. # Force updates even when current tzdata is newer than the requested version.
  18. force=false
  19. # Dry run, doesn't run 'git clone' and 'icupkg'.
  20. dry=false
  21. # Compare ICU and local tzdata versions (used by update-icu.sh).
  22. check_version=false
  23. while getopts ce:fd opt
  24. do
  25. case "$opt" in
  26. c) check_version=true;;
  27. e) icu_pkg="$OPTARG";;
  28. f) force=true;;
  29. d) dry=true;;
  30. \?)
  31. echo >&2 "Usage: $0 [-e <path to icupkg>] [-f] [-d] <tzdata version>"
  32. exit 1;;
  33. esac
  34. done
  35. shift "$((OPTIND - 1))"
  36. if [ $# -ne 1 -a $check_version = false ]; then
  37. echo >&2 "Usage: $0 [-e <path to icupkg>] [-f] [-d] <tzdata version>"
  38. exit 1
  39. fi
  40. tzdata_version=$1
  41. icudata_dir=`dirname "$0"`/../config/external/icu/data
  42. icu_dir=`dirname "$0"`/icu
  43. tzdata_dir=`dirname "$0"`/tzdata
  44. tzdata_files="${tzdata_dir}"/files.txt
  45. tzdata_url=https://github.com/unicode-org/icu-data.git
  46. icu_tzdata_version=`grep --only-matching --perl-regexp --regexp="tz version:\s+\K.*$" "${icu_dir}"/source/data/misc/zoneinfo64.txt`
  47. local_tzdata_version=
  48. if [ -f "${tzdata_dir}"/VERSION ]; then
  49. local_tzdata_version=`grep --only-matching --perl-regexp --regexp="^\K[0-9a-z]+" "${tzdata_dir}"/VERSION`
  50. fi
  51. # Check ICU and current local tzdata versions.
  52. if [ $check_version = true ]; then
  53. if [ ! -z ${local_tzdata_version} ]; then
  54. if [ ${local_tzdata_version} \> ${icu_tzdata_version} ]; then
  55. echo >&2 "WARN: Local tzdata (${local_tzdata_version}) is newer than ICU tzdata (${icu_tzdata_version}), please run '$0 ${local_tzdata_version}'"
  56. else
  57. echo "INFO: ICU tzdata ${icu_tzdata_version} is newer than local tzdata (${local_tzdata_version})"
  58. fi
  59. else
  60. echo "INFO: No local tzdata files found"
  61. fi
  62. exit 0
  63. fi
  64. # Find icu_pkg if not provided as an argument.
  65. icu_pkg=${icu_pkg:-`which icupkg`}
  66. # Test if we can execute icupkg.
  67. if [ ! -x "${icu_pkg}" ]; then
  68. echo >&2 "ERROR: icupkg is not an executable"
  69. exit 1
  70. fi
  71. # Check ICU tzdata version.
  72. if [ ${icu_tzdata_version} \> ${tzdata_version} ]; then
  73. if [ $force = false ]; then
  74. echo >&2 "ERROR: ICU tzdata (${icu_tzdata_version}) is newer than requested version ${tzdata_version}, use -f to force replacing"
  75. exit 1
  76. fi
  77. fi
  78. # Check tzdata version from last checkout.
  79. if [ -n ${local_tzdata_version} -a ${local_tzdata_version} \> ${tzdata_version} ]; then
  80. if [ $force = false ]; then
  81. echo >&2 "ERROR: Local tzdata (${local_tzdata_version}) is newer than requested version ${tzdata_version}, use -f to force replacing"
  82. exit 1
  83. fi
  84. fi
  85. echo "INFO: Updating tzdata from ${local_tzdata_version:-$icu_tzdata_version} to ${tzdata_version}"
  86. # Search for ICU data files.
  87. # Little endian data files.
  88. icudata_file_le=`find "${icudata_dir}" -type f -name 'icudt*l.dat'`
  89. if [ -f "${icudata_file_le}" ]; then
  90. icudata_file_le=`cd "$(dirname "${icudata_file_le}")" && pwd -P`/`basename "${icudata_file_le}"`
  91. echo "INFO: ICU data file (little endian): ${icudata_file_le}"
  92. else
  93. echo >&2 "ERROR: ICU data (little endian) file not found"
  94. exit 1
  95. fi
  96. # Big endian data files.
  97. # Optional until https://bugzilla.mozilla.org/show_bug.cgi?id=1264836 is fixed.
  98. icudata_file_be=`find "${icudata_dir}" -type f -name 'icudt*b.dat'`
  99. if [ -f "${icudata_file_be}" ]; then
  100. icudata_file_be=`cd "$(dirname "${icudata_file_be}")" && pwd -P`/`basename "${icudata_file_be}"`
  101. echo "INFO: ICU data file (big endian): ${icudata_file_be}"
  102. else
  103. echo "INFO: ICU data file (big endian) not found, skipping..."
  104. fi
  105. # Retrieve tzdata from git.
  106. if [ $dry = false ]; then
  107. echo "INFO: Downloading tzdata${tzdata_version}"
  108. # Remove intl/tzdata/source, then replace it with a clean export.
  109. rm -r "${tzdata_dir}"/source
  110. git clone --depth 1 "${tzdata_url}" "${tzdata_dir}"/source
  111. git -C "${tzdata_dir}"/source filter-branch --prune-empty --subdirectory-filter tzdata/icunew/${tzdata_version}/44 HEAD
  112. # Record `git log` and the tzdata version.
  113. git -C "${tzdata_dir}"/source log -1 > "${tzdata_dir}"/GIT-INFO
  114. echo "${tzdata_version}" > "${tzdata_dir}"/VERSION
  115. # Remove the .git directory.
  116. rm -rf "${tzdata_dir}"/source/.git
  117. fi
  118. # Update ICU data.
  119. update_icu_data() {
  120. set +e
  121. local type="$1"
  122. local file="$2"
  123. local cmd="${icu_pkg} --add ${tzdata_files} --sourcedir ${tzdata_dir}/source/${type} ${file}"
  124. eval "${cmd}"
  125. local exit_status=$?
  126. if [ $exit_status -ne 0 ]; then
  127. echo >&2 "ERROR: Error updating tzdata"
  128. echo >&2 "ERROR: If you see an error message like 'format version 03.00 not supported',\n"\
  129. " ensure your icupkg version matches the current ICU version."
  130. exit $exit_status
  131. fi
  132. set -e
  133. }
  134. if [ $dry = false ]; then
  135. update_icu_data "le" "${icudata_file_le}"
  136. if [ -n "${icudata_file_be}" ]; then
  137. update_icu_data "be" "${icudata_file_be}"
  138. fi
  139. echo "INFO: Successfully updated tzdata!"
  140. echo "INFO: Please run js/src/builtin/intl/make_intl_data.py to update additional time zone files for SpiderMonkey."
  141. fi