blob: 3771ad5b93f28b8198936bdb7af29658beb728cd [file] [edit]
#!/usr/bin/env python3
# Copyright (C) 2023 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
try:
import webkitpy
except Exception as e:
sys.stderr.write(f'Error while importing webkitpy: {e}... trying to continue\n')
import argparse
import os
import requests
def upload(filename, url, max_attempts=2, content_type=None):
if not os.path.isfile(filename):
sys.stderr.write(f'ERROR: File not found: {filename}\n')
return -1
if not url:
sys.stderr.write(f'Error: missing url, either pass --url or set UPLOAD_URL environment variable.\n')
return -1
filesize = os.stat(filename).st_size / 1024 / 1024;
sys.stderr.write(f'Uploading {filename}, size: {filesize:.2f} MB')
if content_type:
sys.stderr.write(f', content-type {content_type}')
sys.stderr.write('\n')
with open(filename, 'rb') as f:
try:
data = f.read()
except Exception as e:
sys.stderr.write(f'Exception: {e}\n')
return -1
for attempt in range(1, max_attempts + 1):
try:
headers = None
if content_type:
headers={'Content-Type': content_type}
response = requests.put(url, data=data, headers=headers, timeout=15*60)
sys.stderr.write(f'Response: {response}, status_code: {response.status_code}, {response.reason}\n')
if response and response.status_code // 100 == 2:
return 0
except Exception as e:
sys.stderr.write(f'Exception: {e}\n')
if attempt < max_attempts:
sys.stderr.write(f'Retrying, attempt {attempt + 1} of {max_attempts}\n')
return -1
def main():
sys.stderr.write(f'Starting to upload\n')
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('--filename', action="store", required=True, help='Path to the file. [path/to/123456.zip]')
parser.add_argument('--url', action="store", required=False, help='url to upload to')
parser.add_argument('--content-type', action='store', required=False, default=None, help='Content type of uploaded file')
args = parser.parse_args()
url = args.url or os.getenv('UPLOAD_URL')
rc = upload(args.filename, url, content_type=args.content_type)
return rc
if __name__ == '__main__':
sys.exit(main())