summaryrefslogtreecommitdiff
path: root/chromium/net/tools/update_ios_bundle_data.py
blob: cba1d6bc742c8fa19dbcf71b648e4191ea7a8443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Helper to update the "net_unittests_bundle_data" section of
//net/BUILD.gn so that it lists all of the current files (based on
simpler globbing rules).
"""

import glob
import os
import re
import sys


# ------------------------------------------
# test_support_bundle_data
# ------------------------------------------

# This is a bit more expansive than it needs to be (includes README). Meh.
test_support_bundle_data_globs = [
    "data/quic_http_response_cache_data/test.example.com/*",
    "data/quic_http_response_cache_data_with_push/test.example.com/*",
    "data/ssl/certificates/*",
]

# This regular expression identifies the "sources" section of
# test_support_bundle_data.
test_support_bundle_data_regex = re.compile(r"""
bundle_data\("test_support_bundle_data"\) \{
  visibility = \[ ":test_support" \]
  testonly = true
  sources = \[
(.+?)
  \]
  outputs = \[""", re.MULTILINE | re.DOTALL)

# ------------------------------------------
# net_unittest_bundle_data
# ------------------------------------------

net_unittest_bundle_data_globs = [
    "data/cert_issuer_source_aia_unittest/*.pem",
    "data/cert_issuer_source_static_unittest/*.pem",
    "data/certificate_policies_unittest/*.pem",
    "data/crl_unittest/*.pem",
    "data/embedded_test_server/*",
    "data/filter_unittests/*",
    "data/name_constraints_unittest/*.pem",
    "data/ocsp_unittest/*.pem",
    "data/ov_name_constraints/*.pem",
    "data/path_builder_unittest/**/*.pem",
    "data/parse_certificate_unittest/**/*.pem",
    "data/parse_certificate_unittest/*.pem",
    "data/parse_certificate_unittest/*.pk8",
    "data/test.html",
    "data/trial_comparison_cert_verifier_unittest/**/*.pem",
    "data/url_request_unittest/*",
    "data/verify_certificate_chain_unittest/**/*.pem",
    "data/verify_certificate_chain_unittest/**/*.test",
    "data/verify_certificate_chain_unittest/pkits_errors/*.txt",
    "data/verify_name_match_unittest/names/*.pem",
    "data/verify_signed_data_unittest/*.pem",
    "third_party/nist-pkits/certs/*.crt",
    "third_party/nist-pkits/crls/*.crl",
]

# This regular expression identifies the "sources" section of
# net_unittests_bundle_data
net_unittest_bundle_data_regex = re.compile(r"""
bundle_data\("net_unittests_bundle_data"\) \{
  testonly = true
  sources = \[
(.+?)
  \]
  outputs = \[""", re.MULTILINE | re.DOTALL)

# ------------------------------------------

def get_net_path():
  """Returns the path to //net"""
  return os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir))


def do_file_glob(rule):
  # Do the globbing relative to //net
  prefix = get_net_path()
  matches = glob.glob(prefix + os.sep + rule)

  # Strip off the prefix.
  return [f[len(prefix) + 1:] for f in matches]


def resolve_file_globs(rules):
  paths = []
  for rule in rules:
    paths.extend(do_file_glob(rule))
  return paths


def read_file_to_string(path):
  with open(path, 'r', encoding='utf-8') as f:
    return f.read()


def write_string_to_file(data, path):
  with open(path, 'w', encoding='utf-8') as f:
    f.write(data)


def fatal(message):
  print("FATAL: " + message)
  sys.exit(1)


def format_file_list(files):
  # Keep the file list in sorted order.
  files = sorted(files)

  # Format to a string for GN (assume the filepaths don't contain
  # characters that need escaping).
  return ",\n".join('    "%s"' % f for f in files) + ","


def replace_sources(data, sources_regex, globs):
  m = sources_regex.search(data)
  if not m:
    fatal("Couldn't find the sources section: %s" % sources_regex.pattern)

  formatted_files = format_file_list(resolve_file_globs(globs))
  return data[0:m.start(1)] + formatted_files + data[m.end(1):]


def main():
  # Read in //net/BUILD.gn
  path = os.path.join(get_net_path(), "BUILD.gn")
  data = read_file_to_string(path)

  # Replace the sources part of "net_unittests_bundle_data" with
  # the current results of file globbing.
  data = replace_sources(data, test_support_bundle_data_regex,
                         test_support_bundle_data_globs)

  # Replace the sources part of "net_unittests_bundle_data" with
  # the current results of file globbing.
  data = replace_sources(data, net_unittest_bundle_data_regex,
                         net_unittest_bundle_data_globs)

  write_string_to_file(data, path)
  print("Wrote %s" % path)


if __name__ == '__main__':
  sys.exit(main())