blob: 2a2369c9b43b0fae9537c93419505ca728cd92c3 [file] [log] [blame]
Dirk Pranke7bbb5472021-11-02 16:33:21 -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
16import sys
17import optparse
18import os
19
Dirk Pranke10681782021-11-04 16:13:55 -070020for 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 Pranke7bbb5472021-11-02 16:33:21 -070024import upload_to_google_storage
25
Dirk Pranke221a47d2021-11-11 20:26:31 -080026import common
27
Dirk Pranke7bbb5472021-11-02 16:33:21 -070028# 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
33LOB_EXTENSIONS = [
34 '.ai',
35 '.bin',
36 '.bmp',
Dirk Pranked4a1f152021-11-03 12:37:42 -070037 '.brd',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070038 '.bz2',
Dirk Pranke4a3489a2021-11-03 12:45:42 -070039 '.config',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070040 '.crx',
41 '.dia',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070042 '.gif',
Dirk Pranked4a1f152021-11-03 12:37:42 -070043 '.graffle',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070044 '.ico',
45 '.jpg',
Dirk Pranke4a3489a2021-11-03 12:45:42 -070046 'jpg', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070047 '.jpeg',
48 '.mp4',
Dirk Pranked4a1f152021-11-03 12:37:42 -070049 '.msi',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070050 '.pdf',
Dirk Pranked4a1f152021-11-03 12:37:42 -070051 'pdf', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070052 '.png',
Dirk Pranked4a1f152021-11-03 12:37:42 -070053 'png', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070054 '.PNG',
55 '.swf',
Dirk Pranked4a1f152021-11-03 12:37:42 -070056 '.svg',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070057 '.tar.gz',
58 '.tiff',
Dirk Pranked4a1f152021-11-03 12:37:42 -070059 '_trace',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070060 '.webp',
61 '.xcf',
62 '.xlsx',
63 '.zip'
64 ]
65
66def 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
131def remove_lobs(lob_files):
132 for lob_file in lob_files:
133 if os.path.exists(lob_file):
134 os.remove(lob_file)
135
136def add_to_ignore(lob_files):
Dirk Pranke221a47d2021-11-11 20:26:31 -0800137 with open(common.SITE_DIR + "/.gitignore", 'r') as ignore_file:
Dirk Pranke7bbb5472021-11-02 16:33:21 -0700138 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 Pranke221a47d2021-11-11 20:26:31 -0800146 rel_path = os.path.relpath(lob_file, common.SITE_DIR)
Dirk Pranke7bbb5472021-11-02 16:33:21 -0700147
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 Pranke221a47d2021-11-11 20:26:31 -0800152 with open(common.SITE_DIR + "/.gitignore", 'w') as ignore_file:
Dirk Pranke7bbb5472021-11-02 16:33:21 -0700153 ignore_file.writelines(line + '\n' for line in file_lines)
154
155def 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
167if __name__ == '__main__':
168 try:
169 sys.exit(main())
170 except KeyboardInterrupt:
171 sys.stderr.write('interrupted\n')
172 sys.exit(1)