| #!/usr/bin/env python3 |
| # Copyright 2019 The ChromiumOS Authors |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Update our pinned copy of node to match Chromium.""" |
| |
| import logging |
| import re |
| import sys |
| |
| import libdot |
| |
| |
| URI_BASE = "https://chromium.googlesource.com/chromium/src/+/HEAD" |
| |
| # Where in Chromium's tree they maintain the version. |
| # Look for 'node_linux64' here: |
| # https://chromium.googlesource.com/chromium/src/+/HEAD/DEPS |
| URI_VER = f"{URI_BASE}/DEPS" |
| |
| # Where in Chromium's tree they maintain their hashes. |
| URI_NODE_BASE = f"{URI_BASE}/third_party/node" |
| URI_LIN = "/".join((URI_NODE_BASE, "linux", "node-linux-x64.tar.gz.sha1")) |
| URI_MAC = "/".join((URI_NODE_BASE, "mac", "node-darwin-x64.tar.gz.sha1")) |
| URI_WIN = "/".join((URI_NODE_BASE, "win", "node.exe.sha1")) |
| |
| # File which records the node settings. |
| NODE_SCRIPT = libdot.BIN_DIR / "node" |
| |
| |
| def update_file(var, value): |
| """Update |var| in |path| with the new |value|.""" |
| # Read the current file. |
| lines = NODE_SCRIPT.read_text(encoding="utf-8").splitlines() |
| |
| # Try to update the variable in the file. |
| matcher = re.compile(rf'{var} = "(.*)"') |
| for i, line in enumerate(lines): |
| match = matcher.match(line) |
| if match: |
| old_value = match.group(1) |
| if value != old_value: |
| logging.info(" Old value was %s", old_value) |
| lines[i] = f'{var} = "{value}"' |
| break |
| else: |
| logging.info(" Value up-to-date already") |
| return |
| else: |
| logging.error("Unable to locate %s setting in %s", var, NODE_SCRIPT) |
| sys.exit(1) |
| |
| # Write out the new file content. |
| NODE_SCRIPT.write_text("\n".join(lines) + "\n", encoding="utf-8") |
| |
| |
| def update_ver(uri, var): |
| """Update |var| in our tree with the version data from |uri|.""" |
| deps = fetch_data(uri) |
| m = re.search(r"chromium-nodejs/([0-9.]+)", deps) |
| if not m: |
| logging.error("Unable to find node version in DEPS:\n%s", uri) |
| new_value = m.group(1) |
| logging.info("Updating %s with version %s", var, new_value) |
| update_file(var, new_value) |
| |
| |
| def update_hash(uri, var): |
| """Update |var| in our tree with the hash data from |uri|.""" |
| new_hash = fetch_data(uri) |
| logging.info("Updating %s with hash %s", var, new_hash) |
| update_file(var, new_hash) |
| |
| |
| def fetch_data(uri): |
| """Read the gitiles base64 encoded data from |uri|.""" |
| data = libdot.fetch_data(f"{uri}?format=TEXT", b64=True) |
| return data.getvalue().decode("utf-8").strip() |
| |
| |
| def get_parser(): |
| """Get a command line parser.""" |
| parser = libdot.ArgumentParser(description=__doc__) |
| return parser |
| |
| |
| def main(argv): |
| """The main func!""" |
| parser = get_parser() |
| _opts = parser.parse_args(argv) |
| |
| update_ver(URI_VER, "NODE_VER") |
| update_hash(URI_LIN, "NODE_LINUX_HASH") |
| update_hash(URI_MAC, "NODE_MAC_HASH") |
| update_hash(URI_WIN, "NODE_WIN_HASH") |
| |
| |
| if __name__ == "__main__": |
| sys.exit(main(sys.argv[1:])) |