blob: 20dff221c3d6ed94633785c24078fe56bf15d8c2 [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.isolate, locally in a temporary
directory.
"""
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('-v', '--verbose', action='count', default=0)
options, args = parser.parse_args()
if args:
parser.error('Unsupported argument %s' % args)
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/
# - hashtable/
tempdir = tempfile.mkdtemp(prefix='hello_world')
cachedir = os.path.join(tempdir, 'cache')
hashtabledir = os.path.join(tempdir, 'hashtable')
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', hashtabledir,
])
print('\nRunning')
hashval = hashlib.sha1(open(isolated, 'rb').read()).hexdigest()
run(
[
'run_isolated.py',
'--cache', cachedir,
'--isolate-server', hashtabledir,
'--hash', hashval,
'--no-log',
])
return 0
except subprocess.CalledProcessError as e:
return e.returncode
finally:
shutil.rmtree(tempdir)
if __name__ == '__main__':
sys.exit(main())