Dirk Pranke | 5e4f072 | 2021-11-02 14:55:55 -0700 | [diff] [blame] | 1 | #!/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 | |
| 18 | This script exists because the node_modules archive currently contains |
| 19 | a node_modules/.bin directory with a bunch of symlinked files in it, |
| 20 | and download_from_google_storage.py won't let you have archives with |
| 21 | symlinks. |
| 22 | |
| 23 | In theory we should probably rebuild the node_modules distro without |
| 24 | the ./bin directory (using `npm install --no-bin-lnks`) but that would |
| 25 | cause the build scripts to fail and we'd have to replace `npmw` with |
| 26 | something else. |
| 27 | """ |
| 28 | |
| 29 | import argparse |
| 30 | import hashlib |
| 31 | import os |
| 32 | import subprocess |
| 33 | import sys |
| 34 | import tarfile |
| 35 | |
| 36 | SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 37 | |
| 38 | def 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 Pranke | 1068178 | 2021-11-04 16:13:55 -0700 | [diff] [blame] | 59 | 'gsutil.py', |
Dirk Pranke | 5e4f072 | 2021-11-02 14:55:55 -0700 | [diff] [blame] | 60 | '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 | |
| 79 | if __name__ == '__main__': |
| 80 | sys.exit(main()) |