update_ios_bundle_data.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Helper to update the "net_unittests_bundle_data" section of
  6. //net/BUILD.gn so that it lists all of the current files (based on
  7. simpler globbing rules).
  8. """
  9. import glob
  10. import os
  11. import re
  12. import sys
  13. # ------------------------------------------
  14. # test_support_bundle_data
  15. # ------------------------------------------
  16. # This is a bit more expansive than it needs to be (includes README). Meh.
  17. test_support_bundle_data_globs = [
  18. "data/quic_http_response_cache_data/test.example.com/*",
  19. "data/quic_http_response_cache_data_with_push/test.example.com/*",
  20. "data/ssl/certificates/*",
  21. ]
  22. # This regular expression identifies the "sources" section of
  23. # test_support_bundle_data.
  24. test_support_bundle_data_regex = re.compile(r"""
  25. bundle_data\("test_support_bundle_data"\) \{
  26. visibility = \[ ":test_support" \]
  27. testonly = true
  28. sources = \[
  29. (.+?)
  30. \]
  31. outputs = \[""", re.MULTILINE | re.DOTALL)
  32. # ------------------------------------------
  33. # net_unittest_bundle_data
  34. # ------------------------------------------
  35. net_unittest_bundle_data_globs = [
  36. "data/cert_issuer_source_aia_unittest/*.pem",
  37. "data/cert_issuer_source_static_unittest/*.pem",
  38. "data/certificate_policies_unittest/*.pem",
  39. "data/crl_unittest/*.pem",
  40. "data/embedded_test_server/*",
  41. "data/filter_unittests/*",
  42. "data/name_constraints_unittest/*.pem",
  43. "data/ocsp_unittest/*.pem",
  44. "data/ov_name_constraints/*.pem",
  45. "data/path_builder_unittest/**/*.pem",
  46. "data/parse_certificate_unittest/**/*.pem",
  47. "data/parse_certificate_unittest/*.pem",
  48. "data/parse_certificate_unittest/*.pk8",
  49. "data/test.html",
  50. "data/trial_comparison_cert_verifier_unittest/**/*.pem",
  51. "data/url_request_unittest/*",
  52. "data/verify_certificate_chain_unittest/**/*.pem",
  53. "data/verify_certificate_chain_unittest/**/*.test",
  54. "data/verify_certificate_chain_unittest/pkits_errors/*.txt",
  55. "data/verify_name_match_unittest/names/*.pem",
  56. "data/verify_signed_data_unittest/*.pem",
  57. "third_party/nist-pkits/certs/*.crt",
  58. "third_party/nist-pkits/crls/*.crl",
  59. ]
  60. # This regular expression identifies the "sources" section of
  61. # net_unittests_bundle_data
  62. net_unittest_bundle_data_regex = re.compile(r"""
  63. bundle_data\("net_unittests_bundle_data"\) \{
  64. testonly = true
  65. sources = \[
  66. (.+?)
  67. \]
  68. outputs = \[""", re.MULTILINE | re.DOTALL)
  69. # ------------------------------------------
  70. def get_net_path():
  71. """Returns the path to //net"""
  72. return os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir))
  73. def do_file_glob(rule):
  74. # Do the globbing relative to //net
  75. prefix = get_net_path()
  76. matches = glob.glob(prefix + os.sep + rule)
  77. # Strip off the prefix.
  78. return [f[len(prefix) + 1:] for f in matches]
  79. def resolve_file_globs(rules):
  80. paths = []
  81. for rule in rules:
  82. paths.extend(do_file_glob(rule))
  83. return paths
  84. def read_file_to_string(path):
  85. with open(path, 'r', encoding='utf-8') as f:
  86. return f.read()
  87. def write_string_to_file(data, path):
  88. with open(path, 'w', encoding='utf-8') as f:
  89. f.write(data)
  90. def fatal(message):
  91. print("FATAL: " + message)
  92. sys.exit(1)
  93. def format_file_list(files):
  94. # Keep the file list in sorted order.
  95. files = sorted(files)
  96. # Format to a string for GN (assume the filepaths don't contain
  97. # characters that need escaping).
  98. return ",\n".join(' "%s"' % f for f in files) + ","
  99. def replace_sources(data, sources_regex, globs):
  100. m = sources_regex.search(data)
  101. if not m:
  102. fatal("Couldn't find the sources section: %s" % sources_regex.pattern)
  103. formatted_files = format_file_list(resolve_file_globs(globs))
  104. return data[0:m.start(1)] + formatted_files + data[m.end(1):]
  105. def main():
  106. # Read in //net/BUILD.gn
  107. path = os.path.join(get_net_path(), "BUILD.gn")
  108. data = read_file_to_string(path)
  109. # Replace the sources part of "net_unittests_bundle_data" with
  110. # the current results of file globbing.
  111. data = replace_sources(data, test_support_bundle_data_regex,
  112. test_support_bundle_data_globs)
  113. # Replace the sources part of "net_unittests_bundle_data" with
  114. # the current results of file globbing.
  115. data = replace_sources(data, net_unittest_bundle_data_regex,
  116. net_unittest_bundle_data_globs)
  117. write_string_to_file(data, path)
  118. print("Wrote %s" % path)
  119. if __name__ == '__main__':
  120. sys.exit(main())