Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -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 | import sys |
| 17 | import optparse |
| 18 | import os |
| 19 | |
Dirk Pranke | 1068178 | 2021-11-04 16:13:55 -0700 | [diff] [blame] | 20 | for path in os.environ['PATH'].split(os.path.pathsep): |
| 21 | if path.endswith('depot_tools') and path not in sys.path: |
| 22 | sys.path.insert(0, path) |
| 23 | |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 24 | import upload_to_google_storage |
| 25 | |
Dirk Pranke | 221a47d | 2021-11-11 20:26:31 -0800 | [diff] [blame] | 26 | import common |
| 27 | |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 28 | # This list must be kept in sync with the lists in //.eleventy.js and |
| 29 | # //PRESUBMIT.py. |
| 30 | # TODO(dpranke): Figure out how to share these lists to eliminate the |
| 31 | # duplication and need to keep them in sync. |
| 32 | |
| 33 | LOB_EXTENSIONS = [ |
| 34 | '.ai', |
| 35 | '.bin', |
| 36 | '.bmp', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 37 | '.brd', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 38 | '.bz2', |
Dirk Pranke | 4a3489a | 2021-11-03 12:45:42 -0700 | [diff] [blame] | 39 | '.config', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 40 | '.crx', |
| 41 | '.dia', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 42 | '.gif', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 43 | '.graffle', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 44 | '.ico', |
| 45 | '.jpg', |
Dirk Pranke | 4a3489a | 2021-11-03 12:45:42 -0700 | [diff] [blame] | 46 | 'jpg', # Some files are missing the '.' :(. |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 47 | '.jpeg', |
| 48 | '.mp4', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 49 | '.msi', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 50 | '.pdf', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 51 | 'pdf', # Some files are missing the '.' :(. |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 52 | '.png', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 53 | 'png', # Some files are missing the '.' :(. |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 54 | '.PNG', |
| 55 | '.swf', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 56 | '.svg', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 57 | '.tar.gz', |
| 58 | '.tiff', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 59 | '_trace', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 60 | '.webp', |
| 61 | '.xcf', |
| 62 | '.xlsx', |
| 63 | '.zip' |
| 64 | ] |
| 65 | |
| 66 | def main(): |
| 67 | parser = optparse.OptionParser(upload_to_google_storage.USAGE_STRING) |
| 68 | parser.add_option('-b', '--bucket', |
| 69 | default='chromium-website-lob-storage', |
| 70 | help='Google Storage bucket to upload to.') |
| 71 | parser.add_option('-e', '--boto', help='Specify a custom boto file.') |
| 72 | parser.add_option('-f', '--force', action='store_true', |
| 73 | help='Force upload even if remote file exists.') |
| 74 | parser.add_option('-g', '--gsutil_path', |
| 75 | default=upload_to_google_storage.GSUTIL_DEFAULT_PATH, |
| 76 | help='Path to the gsutil script.') |
| 77 | parser.add_option('-m', '--use_md5', action='store_true', |
| 78 | help='Generate MD5 files when scanning, and don\'t check ' |
| 79 | 'the MD5 checksum if a .md5 file is found.') |
| 80 | parser.add_option('-t', '--num_threads', default=1, type='int', |
| 81 | help='Number of uploader threads to run.') |
| 82 | parser.add_option('-s', '--skip_hashing', action='store_true', |
| 83 | help='Skip hashing if .sha1 file exists.') |
| 84 | parser.add_option('-0', '--use_null_terminator', action='store_true', |
| 85 | help='Use \\0 instead of \\n when parsing ' |
| 86 | 'the file list from stdin. This is useful if the input ' |
| 87 | 'is coming from "find ... -print0".') |
| 88 | parser.add_option('-z', '--gzip', metavar='ext', |
| 89 | help='Gzip files which end in ext. ' |
| 90 | 'ext is a comma-separated list') |
| 91 | parser.add_option('-d', '--directory', |
| 92 | help='The target is a directory. ') |
| 93 | parser.add_option('-r', '--remove', action='store_true', |
| 94 | help='Removes the file that was uploaded to storage. ') |
| 95 | (options, args) = parser.parse_args() |
| 96 | |
| 97 | if options.directory: |
| 98 | input_filenames = get_lobs_from_dir(options.directory) |
| 99 | if len(input_filenames) == 0: |
| 100 | print("No LOB files found in directory to upload") |
| 101 | return 0 |
| 102 | else: |
| 103 | # Enumerate our inputs. |
| 104 | input_filenames = upload_to_google_storage.get_targets(args, parser, |
| 105 | options.use_null_terminator) |
| 106 | |
| 107 | # Make sure we can find a working instance of gsutil. |
| 108 | if os.path.exists(upload_to_google_storage.GSUTIL_DEFAULT_PATH): |
| 109 | gsutil = upload_to_google_storage.Gsutil( |
| 110 | upload_to_google_storage.GSUTIL_DEFAULT_PATH, boto_path=options.boto) |
| 111 | else: |
| 112 | parser.error('gsutil not found in %s, bad depot_tools checkout?' % |
| 113 | upload_to_google_storage.GSUTIL_DEFAULT_PATH) |
| 114 | |
| 115 | base_url = 'gs://%s' % options.bucket |
| 116 | |
| 117 | add_to_ignore(input_filenames) |
| 118 | |
| 119 | upload_status = upload_to_google_storage.upload_to_google_storage( |
| 120 | input_filenames, base_url, gsutil, options.force, options.use_md5, |
| 121 | options.num_threads, options.skip_hashing, options.gzip) |
| 122 | |
| 123 | if upload_status: |
| 124 | return upload_status |
| 125 | |
| 126 | if options.remove: |
| 127 | remove_lobs(input_filenames) |
| 128 | |
| 129 | return 0 |
| 130 | |
| 131 | def remove_lobs(lob_files): |
| 132 | for lob_file in lob_files: |
| 133 | if os.path.exists(lob_file): |
| 134 | os.remove(lob_file) |
| 135 | |
| 136 | def add_to_ignore(lob_files): |
Dirk Pranke | 221a47d | 2021-11-11 20:26:31 -0800 | [diff] [blame] | 137 | with open(common.SITE_DIR + "/.gitignore", 'r') as ignore_file: |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 138 | file_lines = list(line.rstrip() for line in ignore_file.readlines()) |
| 139 | |
| 140 | end_tag_index = file_lines.index('#end_lob_ignore') |
| 141 | lob_ignores = set(file_lines[ |
| 142 | file_lines.index('#start_lob_ignore') + 1 : |
| 143 | end_tag_index]) |
| 144 | |
| 145 | for lob_file in lob_files: |
Dirk Pranke | 221a47d | 2021-11-11 20:26:31 -0800 | [diff] [blame] | 146 | rel_path = os.path.relpath(lob_file, common.SITE_DIR) |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 147 | |
| 148 | if os.path.exists(lob_file) and not rel_path in lob_ignores: |
| 149 | file_lines.insert(end_tag_index, rel_path) |
| 150 | end_tag_index+=1 |
| 151 | |
Dirk Pranke | 221a47d | 2021-11-11 20:26:31 -0800 | [diff] [blame] | 152 | with open(common.SITE_DIR + "/.gitignore", 'w') as ignore_file: |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 153 | ignore_file.writelines(line + '\n' for line in file_lines) |
| 154 | |
| 155 | def get_lobs_from_dir(directory): |
| 156 | lobs = [] |
| 157 | for (dirpath, _, filenames) in os.walk(directory): |
| 158 | for filename in filenames: |
| 159 | absolute_filename = os.path.join(dirpath, filename) |
| 160 | if os.path.isfile(absolute_filename): |
| 161 | for ext in LOB_EXTENSIONS: |
| 162 | if filename.endswith(ext): |
| 163 | lobs.append(absolute_filename) |
| 164 | break |
| 165 | return lobs |
| 166 | |
| 167 | if __name__ == '__main__': |
| 168 | try: |
| 169 | sys.exit(main()) |
| 170 | except KeyboardInterrupt: |
| 171 | sys.stderr.write('interrupted\n') |
| 172 | sys.exit(1) |