blob: 8147aebfba58cd4a6da042d0e2a474a1d34ac553 [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import imp
import optparse
import os
import subprocess
import sys
DART_DIR = os.path.dirname(os.path.dirname(__file__))
GSUTIL = os.path.join(DART_DIR, 'third_party', 'gsutil', 'gsutil')
BOT_UTILS = os.path.join(DART_DIR, 'tools', 'bots', '')
BASENAME_PATTERN = 'darteditor-%(system)s-%(bits)s'
DRY_RUN = False
bot_utils = imp.load_source('bot_utils', BOT_UTILS)
class ChangedWorkingDirectory(object):
def __init__(self, working_directory):
self._working_directory = working_directory
def __enter__(self):
self._old_cwd = os.getcwd()
print "Enter directory = ", self._working_directory
if not DRY_RUN:
def __exit__(self, *_):
print "Enter directory = ", self._old_cwd
def GetOptionsParser():
parser = optparse.OptionParser("usage: %prog [options]")
help="Scratch directory to use.")
parser.add_option("--revision", type="int",
help="Revision we want to sign.")
parser.add_option("--channel", type="string",
help="Channel we want to sign.")
parser.add_option("--dry-run", action="store_true", dest="dry_run",
help="Do a dry run and do not execute any commands.")
parser.add_option("--prepare", action="store_true", dest="prepare",
help="Prepare the .exe/.zip files to sign.")
parser.add_option("--deploy", action="store_true", dest="deploy",
help="Pack the signed .exe/.zip files and deploy.")
return parser
def die(msg, withOptions=True):
print msg
if withOptions:
def run(command):
"""We use run() instead of builtin python methods, because not all
functionality can easily be done by python and we can support --dry-run"""
print "Running: ", command
if not DRY_RUN:
process = subprocess.Popen(command,
(stdout, stderr) = process.communicate()
if process.returncode != 0:
print "DEBUG: failed to run command '%s'" % command
print "DEBUG: stdout = ", stdout
print "DEBUG: stderr = ", stderr
print "DEBUG: returncode = ", process.returncode
raise OSError(process.returncode)
def clean_directory(directory):
if os.path.exists(directory):
run(['rm', '-r', directory])
run(['mkdir', '-p', directory])
def rm_tree(directory):
if os.path.exists(directory):
run(['rm', '-r', directory])
def copy_tree(from_dir, to_dir):
if os.path.exists(to_dir):
run(['rm', '-r', to_dir])
run(['cp', '-Rp', from_dir, to_dir])
def copy_file(from_file, to_file):
if os.path.exists(to_file):
run(['rm', to_file])
run(['cp', '-p', from_file, to_file])
def copy_and_zip(from_dir, to_dir):
copy_tree(from_dir, to_dir)
dirname = os.path.basename(to_dir)
with ChangedWorkingDirectory(os.path.dirname(to_dir)):
run(['zip', '-r9', dirname + '.zip', dirname])
def unzip_and_copy(extracted_zipfiledir, to_dir):
run(['unzip', extracted_zipfiledir + '.zip', '-d',
copy_tree(extracted_zipfiledir, to_dir)
def download_from_new_location(channel, config, destination):
namer = bot_utils.GCSNamer(channel,
bucket = namer.editor_zipfilepath(config['revision'], config['system'],
run([GSUTIL, 'cp', bucket, destination])
def download_msi_installer_from_new_location(channel, config, destination):
namer = bot_utils.GCSNamer(channel, bot_utils.ReleaseType.RAW)
bucket = namer.editor_installer_filepath(
config['revision'], config['system'], config['bits'], 'msi')
run([GSUTIL, 'cp', bucket, destination])
def upload_to_new_location(channel, config, source_zip):
namer = bot_utils.GCSNamer(channel,
zipfilename = namer.editor_zipfilename(config['system'], config['bits'])
bucket = namer.editor_zipfilepath(config['revision'], config['system'],
if not DRY_RUN:
bot_utils.CreateChecksumFile(source_zip, mangled_filename=zipfilename)
md5_zip_file = source_zip + '.md5sum'
run([GSUTIL, 'cp', source_zip, bucket])
run([GSUTIL, 'cp', md5_zip_file, bucket + '.md5sum'])
run([GSUTIL, 'setacl', 'public-read', bucket])
run([GSUTIL, 'setacl', 'public-read', bucket + '.md5sum'])
def upload_msi_installer_to_new_location(channel, config, signed_msi):
namer = bot_utils.GCSNamer(channel, bot_utils.ReleaseType.SIGNED)
bucket = namer.editor_installer_filepath(
config['revision'], config['system'], config['bits'], 'msi')
run([GSUTIL, 'cp', '-a', 'public-read', signed_msi, bucket])
def main():
if sys.platform != 'linux2':
print "This script was only tested on linux. Please run it on linux!"
parser = GetOptionsParser()
(options, args) = parser.parse_args()
if not options.scratch_dir:
die("No scratch directory given.")
if not options.revision:
die("No revision given.")
if not options.prepare and not options.deploy:
die("No prepare/deploy parameter given.")
if options.prepare and options.deploy:
die("Can't have prepare and deploy parameters at the same time.")
if len(args) > 0:
die("Invalid additional arguments: %s." % args)
if not
die("You need to specify a channel with --channel.")
global DRY_RUN
DRY_RUN = options.dry_run
downloads_dir = os.path.join(options.scratch_dir, 'downloads')
presign_dir = os.path.join(options.scratch_dir, 'presign')
postsign_dir = os.path.join(options.scratch_dir, 'postsign')
uploads_dir = os.path.join(options.scratch_dir, 'uploads')
if options.prepare:
# Clean all directories
elif options.deploy:
# These are the locations where we can find the *.app folders and *.exe files
# and the names we use inside the scratch directory.
locations = {
'macos' : {
'editor' : os.path.join('dart', ''),
'chrome' : os.path.join('dart', 'chromium', ''),
'editor_scratch' : 'DartEditor%(bits)',
'chrome_scratch' : 'Chromium%(bits)',
'zip' : True,
'win32' : {
'editor' : os.path.join('dart', 'DartEditor.exe'),
'chrome' : os.path.join('dart', 'chromium', 'chrome.exe'),
'msi_scratch' : 'darteditor-installer-windows-%(bits)s.msi',
'editor_scratch' : 'DartEditor%(bits)s.exe',
'chrome_scratch' : 'chromium%(bits)s.exe',
'zip' : False,
# Desitination of zip files we download
for system in ('macos', 'win32'):
for bits in ('32', '64'):
config = {
'revision' : options.revision,
'system' : system,
'bits' : bits,
destination = os.path.join(downloads_dir, FILENAME_PATTERN % config)
destination_dir = os.path.join(downloads_dir, BASENAME_PATTERN % config)
deploy = os.path.join(uploads_dir, FILENAME_PATTERN % config)
deploy_dir = os.path.join(uploads_dir, BASENAME_PATTERN % config)
if options.prepare:
# Download *.zip files from GCS buckets
download_from_new_location(, config, destination)
run(['unzip', destination, '-d', destination_dir])
for name in ['editor', 'chrome']:
from_path = os.path.join(destination_dir, locations[system][name])
to_path = os.path.join(
presign_dir, locations[system]['%s_scratch' % name] % config)
if locations[system]['zip']:
# We copy a .app directory directory and zip it
copy_and_zip(from_path, to_path)
# We copy an .exe file
copy_file(from_path, to_path)
# Download .*msi installer from GCS to presign directory
if system == 'win32':
presign_msi = os.path.join(
presign_dir, locations[system]['msi_scratch'] % config)
download_msi_installer_from_new_location(, config, presign_msi)
elif options.deploy:
copy_tree(destination_dir, deploy_dir)
for name in ['editor', 'chrome']:
from_path = os.path.join(
postsign_dir, locations[system]['%s_scratch' % name] % config)
to_path = os.path.join(deploy_dir, locations[system][name])
if locations[system]['zip']:
# We unzip a zip file and copy the resulting signed .app directory
unzip_and_copy(from_path, to_path)
# We copy the signed .exe file
copy_file(from_path, to_path)
deploy_zip_file = os.path.abspath(deploy)
with ChangedWorkingDirectory(deploy_dir):
run(['zip', '-r9', deploy_zip_file, 'dart'])
# Upload *.zip/*.zip.md5sum and set 'public-read' ACL
upload_to_new_location(, config, deploy_zip_file)
# Upload *.msi installer and set 'public-read ACL
if system == 'win32':
postsign_msi = os.path.join(
postsign_dir, locations[system]['msi_scratch'] % config)
upload_msi_installer_to_new_location(, config, postsign_msi)
if __name__ == '__main__':