blob: 0aef2faccf78d93dc540c5cc7bafd28195784077 [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
32import subprocess
33import sys
34import tarfile
35
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
58 retcode = subprocess.call([
Dirk Pranke10681782021-11-04 16:13:55 -070059 'gsutil.py',
Dirk Pranke5e4f0722021-11-02 14:55:55 -070060 'cp',
61 'gs://chromium-website-lob-storage/%s' % expected_sha1,
62 tgz
63 ])
64 if retcode:
65 return retcode
66
67 try:
68 # TODO(dpranke): download_from_google_storage puts in a fair amount
69 # of effort to not clobber an existing directory until it is sure it
70 # can extract the archive completely. Consider whether we should do
71 # the same.
72 with tarfile.open(tgz, 'r:gz') as tar:
73 tar.extractall(path=SRC_ROOT)
74 return 0
75 except Exception as e:
76 print(e)
77 return 1
78
79if __name__ == '__main__':
80 sys.exit(main())