Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / tools / moztreedocs / upload.py
bloba6071351744285a35ef4cfa5967300a8090af3e9
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, # You can obtain one at http://mozilla.org/MPL/2.0/.
5 import io
6 import mimetypes
7 import os
8 import sys
9 from concurrent import futures
10 from pprint import pprint
12 import boto3
13 import botocore
14 import requests
15 from mozbuild.util import memoize
18 @memoize
19 def create_aws_session():
20 """
21 This function creates an aws session that is
22 shared between upload and delete both.
23 """
24 region = "us-west-2"
25 level = os.environ.get("MOZ_SCM_LEVEL", "1")
26 bucket = {
27 "1": "gecko-docs.mozilla.org-l1",
28 "2": "gecko-docs.mozilla.org-l2",
29 "3": "gecko-docs.mozilla.org",
30 }[level]
31 secrets_url = "http://taskcluster/secrets/v1/secret/"
32 secrets_url += "project/releng/gecko/build/level-{}/gecko-docs-upload".format(level)
34 # Get the credentials from the TC secrets service. Note that these
35 # differ per SCM level
36 if "TASK_ID" in os.environ:
37 print("Using AWS credentials from the secrets service")
38 session = requests.Session()
39 res = session.get(secrets_url)
40 res.raise_for_status()
41 secret = res.json()["secret"]
42 session = boto3.session.Session(
43 aws_access_key_id=secret["AWS_ACCESS_KEY_ID"],
44 aws_secret_access_key=secret["AWS_SECRET_ACCESS_KEY"],
45 region_name=region,
47 else:
48 print("Trying to use your AWS credentials..")
49 session = boto3.session.Session(region_name=region)
51 s3 = session.client("s3", config=botocore.client.Config(max_pool_connections=20))
53 return s3, bucket
56 @memoize
57 def get_s3_keys(s3, bucket):
58 kwargs = {"Bucket": bucket}
59 all_keys = []
60 while True:
61 response = s3.list_objects_v2(**kwargs)
62 for obj in response["Contents"]:
63 all_keys.append(obj["Key"])
65 try:
66 kwargs["ContinuationToken"] = response["NextContinuationToken"]
67 except KeyError:
68 break
70 return all_keys
73 def s3_set_redirects(redirects):
74 s3, bucket = create_aws_session()
76 configuration = {"IndexDocument": {"Suffix": "index.html"}, "RoutingRules": []}
78 for path, redirect in redirects.items():
79 rule = {
80 "Condition": {"KeyPrefixEquals": path},
81 "Redirect": {"ReplaceKeyPrefixWith": redirect},
83 if os.environ.get("MOZ_SCM_LEVEL") == "3":
84 rule["Redirect"]["HostName"] = "firefox-source-docs.mozilla.org"
86 configuration["RoutingRules"].append(rule)
88 s3.put_bucket_website(
89 Bucket=bucket,
90 WebsiteConfiguration=configuration,
94 def s3_delete_missing(files, key_prefix=None):
95 """Delete files in the S3 bucket.
97 Delete files on the S3 bucket that doesn't match the files
98 given as the param. If the key_prefix is not specified, missing
99 files that has main/ as a prefix will be removed. Otherwise, it
100 will remove files with the same prefix as key_prefix.
102 s3, bucket = create_aws_session()
103 files_on_server = get_s3_keys(s3, bucket)
104 if key_prefix:
105 files_on_server = [
106 path for path in files_on_server if path.startswith(key_prefix)
108 else:
109 files_on_server = [
110 path for path in files_on_server if not path.startswith("main/")
112 files = [key_prefix + "/" + path if key_prefix else path for path, f in files]
113 files_to_delete = [path for path in files_on_server if path not in files]
115 query_size = 1000
116 while files_to_delete:
117 keys_to_remove = [{"Key": key} for key in files_to_delete[:query_size]]
118 response = s3.delete_objects(
119 Bucket=bucket,
120 Delete={
121 "Objects": keys_to_remove,
122 }, # NOQA
124 pprint(response, indent=2)
125 files_to_delete = files_to_delete[query_size:]
128 def s3_upload(files, key_prefix=None):
129 """Upload files to an S3 bucket.
131 ``files`` is an iterable of ``(path, BaseFile)`` (typically from a
132 mozpack Finder).
134 Keys in the bucket correspond to source filenames. If ``key_prefix`` is
135 defined, key names will be ``<key_prefix>/<path>``.
137 s3, bucket = create_aws_session()
139 def upload(f, path, bucket, key, extra_args):
140 # Need to flush to avoid buffering/interleaving from multiple threads.
141 sys.stdout.write("uploading %s to %s\n" % (path, key))
142 sys.stdout.flush()
143 s3.upload_fileobj(f, bucket, key, ExtraArgs=extra_args)
145 fs = []
146 with futures.ThreadPoolExecutor(20) as e:
147 for path, f in files:
148 content_type, content_encoding = mimetypes.guess_type(path)
149 extra_args = {}
150 if content_type:
151 if content_type.startswith("text/"):
152 content_type += '; charset="utf-8"'
153 extra_args["ContentType"] = content_type
154 if content_encoding:
155 extra_args["ContentEncoding"] = content_encoding
157 if key_prefix:
158 key = "%s/%s" % (key_prefix, path)
159 else:
160 key = path
162 # The file types returned by mozpack behave like file objects. But
163 # they don't accept an argument to read(). So we wrap in a BytesIO.
164 fs.append(
165 e.submit(upload, io.BytesIO(f.read()), path, bucket, key, extra_args)
168 s3_delete_missing(files, key_prefix)
169 # Need to do this to catch any exceptions.
170 for f in fs:
171 f.result()