blob: f2e5b03de37a0e84dc84a256ebcc51af318d7991 [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Runs hello_world.py, through hello_world.isolated, locally in a temporary
directory with the files fetched from the remote Content-Addressed Datastore.
"""
import hashlib
import optparse
import os
import shutil
import subprocess
import sys
import tempfile
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
def run(cmd):
print('Running: %s' % ' '.join(cmd))
cmd = [sys.executable, os.path.join(ROOT_DIR, '..', cmd[0])] + cmd[1:]
if sys.platform != 'win32':
cmd = ['time', '-p'] + cmd
subprocess.check_call(cmd)
def main():
parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
parser.add_option(
'-I', '--isolate-server',
metavar='URL', default='',
help='Isolate server to use')
parser.add_option('-v', '--verbose', action='count', default=0)
options, args = parser.parse_args()
if args:
parser.error('Unsupported argument %s' % args)
if not options.isolate_server:
parser.error('--isolate-server is required.')
os.environ['ISOLATE_DEBUG'] = str(options.verbose)
try:
# All the files are put in a temporary directory. This is optional and
# simply done so the current directory doesn't have the following files
# created:
# - hello_world.isolated
# - hello_world.isolated.state
# - cache/
tempdir = tempfile.mkdtemp(prefix='hello_world')
cachedir = os.path.join(tempdir, 'cache')
isolateddir = os.path.join(tempdir, 'isolated')
isolated = os.path.join(isolateddir, 'hello_world.isolated')
os.mkdir(isolateddir)
print('Archiving')
run(
[
'isolate.py',
'archive',
'--isolate', os.path.join(ROOT_DIR, 'hello_world.isolate'),
'--isolated', isolated,
'--outdir', options.isolate_server,
])
print('\nRunning')
hashval = hashlib.sha1(open(isolated, 'rb').read()).hexdigest()
run(
[
'run_isolated.py',
'--cache', cachedir,
'--isolate-server', options.isolate_server,
'--hash', hashval,
'--no-log',
])
return 0
except subprocess.CalledProcessError as e:
return e.returncode
finally:
shutil.rmtree(tempdir)
if __name__ == '__main__':
sys.exit(main())