blob: c8b4d348637b3924790c200f2ea329926723346a [file] [log] [blame]
Dirk Pranke5e4f0722021-11-02 14:55:55 -07001#!/usr/bin/env python3
2# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Simple script to download the pinned Node modules from GCS.
17
18This script exists because the node_modules archive currently contains
19a node_modules/.bin directory with a bunch of symlinked files in it,
20and download_from_google_storage.py won't let you have archives with
21symlinks.
22
23In theory we should probably rebuild the node_modules distro without
24the ./bin directory (using `npm install --no-bin-lnks`) but that would
25cause the build scripts to fail and we'd have to replace `npmw` with
26something else.
27"""
28
29import argparse
30import hashlib
31import os
Dirk Pranke5e4f0722021-11-02 14:55:55 -070032import sys
33import tarfile
Dirk Pranke3a5b4cb2021-12-09 16:51:34 -080034from urllib.request import urlopen
Dirk Pranke5e4f0722021-11-02 14:55:55 -070035
36SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
37
38def main():
39 parser = argparse.ArgumentParser(description=__doc__)
40 parser.parse_args()
41
42 with open(os.path.join(SRC_ROOT, 'node_modules.tar.gz.sha1')) as fp:
43 expected_sha1 = fp.read().strip()
44
45 actual_sha1 = None
46 tgz = os.path.join(SRC_ROOT, 'node_modules.tar.gz')
47 if os.path.exists(tgz):
48 with open(tgz, 'rb') as fp:
49 s = hashlib.sha1()
50 s.update(fp.read())
51 actual_sha1 = s.hexdigest()
52
53 # TODO(dpranke): Consider whether we should validate that node_modules/
54 # and all of the expected files exist as well.
55 if actual_sha1 == expected_sha1:
56 return 0
57
Dirk Pranke3a5b4cb2021-12-09 16:51:34 -080058 url = 'https://storage.googleapis.com/%s/%s' % (
59 'chromium-website-lob-storage', expected_sha1)
60 with urlopen(url) as url_fp, open(tgz, 'wb') as tgz_fp:
61 tgz_fp.write(url_fp.read())
Dirk Pranke5e4f0722021-11-02 14:55:55 -070062
63 try:
64 # TODO(dpranke): download_from_google_storage puts in a fair amount
65 # of effort to not clobber an existing directory until it is sure it
66 # can extract the archive completely. Consider whether we should do
67 # the same.
68 with tarfile.open(tgz, 'r:gz') as tar:
69 tar.extractall(path=SRC_ROOT)
70 return 0
71 except Exception as e:
72 print(e)
73 return 1
74
75if __name__ == '__main__':
76 sys.exit(main())