Skip to content

Commit

Permalink
Add a tool to re-generate the "net_unittests_bundle_data" section of …
Browse files Browse the repository at this point in the history
…net/BUILD.gn.

Review-Url: https://codereview.chromium.org/2858113004
Cr-Commit-Position: refs/heads/master@{#469710}
  • Loading branch information
eroman authored and Commit bot committed May 5, 2017
1 parent 1ed3abd commit d5207a9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
4 changes: 3 additions & 1 deletion net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3134,12 +3134,15 @@ if (!is_ios) {
}
}

# This section can be updated from globbing rules using:
# python ./tools/update_ios_bundle_data.py
bundle_data("net_unittests_bundle_data") {
testonly = true
sources = [
"data/cert_issuer_source_aia_unittest/i.pem",
"data/cert_issuer_source_aia_unittest/i2.pem",
"data/cert_issuer_source_aia_unittest/i3.pem",
"data/cert_issuer_source_aia_unittest/root.pem",
"data/cert_issuer_source_aia_unittest/target_file_aia.pem",
"data/cert_issuer_source_aia_unittest/target_file_and_http_aia.pem",
"data/cert_issuer_source_aia_unittest/target_invalid_and_http_aia.pem",
Expand Down Expand Up @@ -3303,7 +3306,6 @@ bundle_data("net_unittests_bundle_data") {
"data/parse_certificate_unittest/tbs_validity_generalized_time_and_utc_time.pem",
"data/parse_certificate_unittest/tbs_validity_relaxed.pem",
"data/parse_certificate_unittest/tbs_validity_utc_time_and_generalized_time.pem",
"data/parse_certificate_unittest/v3_certificate_template.txt",
"data/parse_ocsp_unittest/bad_ocsp_type.pem",
"data/parse_ocsp_unittest/bad_signature.pem",
"data/parse_ocsp_unittest/bad_status.pem",
Expand Down
112 changes: 112 additions & 0 deletions net/tools/update_ios_bundle_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/python
# Copyright (c) 2017 The Chromium Authors. All rights reserved.
# 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


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


INCLUSIONS = [
"data/cert_issuer_source_aia_unittest/*.pem",
"data/cert_issuer_source_static_unittest/*.pem",
"data/certificate_policies_unittest/*.pem",
"data/filter_unittests/*",
"data/name_constraints_unittest/*.pem",
"data/parse_certificate_unittest/*.pem",
"data/parse_ocsp_unittest/*.pem",
"data/test.html",
"data/url_request_unittest/*",
"data/verify_certificate_chain_unittest/**/*.pem",
"data/verify_certificate_chain_unittest/**/*.test",
"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",
]


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 get_all_data_file_paths():
paths = []
for rule in INCLUSIONS:
paths.extend(do_file_glob(rule))
return paths


def read_file_to_string(path):
with open(path, 'r') as f:
return f.read()


def write_string_to_file(data, path):
with open(path, 'w') as f:
f.write(data)


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


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


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 main():
# Find all the data files.
all_files = get_all_data_file_paths()

# 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 newly collected file list.
m = bundle_data_regex.search(data)
if not m:
fatal("Couldn't find the net_unittests_bundle_data section")
data = data[0:m.start(1)] + format_file_list(all_files) + data[m.end(1):]

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


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

0 comments on commit d5207a9

Please sign in to comment.