1#!/usr/bin/env python
2# Copyright 2015 the V8 project authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Adaptor script called through build/isolate.gypi.
7
8Slimmed down version of chromium's isolate driver that doesn't process dynamic
9dependencies.
10"""
11
12import json
13import logging
14import os
15import subprocess
16import sys
17
18TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
19
20
21def prepare_isolate_call(args, output):
22  """Gathers all information required to run isolate.py later.
23
24  Dumps it as JSON to |output| file.
25  """
26  with open(output, 'wb') as f:
27    json.dump({
28      'args': args,
29      'dir': os.getcwd(),
30      'version': 1,
31    }, f, indent=2, sort_keys=True)
32
33
34def main():
35  logging.basicConfig(level=logging.ERROR, format='%(levelname)7s %(message)s')
36  if len(sys.argv) < 2:
37    print >> sys.stderr, 'Internal failure; mode required'
38    return 1
39  mode = sys.argv[1]
40  args = sys.argv[1:]
41  isolate = None
42  isolated = None
43  for i, arg in enumerate(args):
44    if arg == '--isolate':
45      isolate = i + 1
46    if arg == '--isolated':
47      isolated = i + 1
48  if not isolate or not isolated:
49    print >> sys.stderr, 'Internal failure'
50    return 1
51
52  # In 'prepare' mode just collect all required information for postponed
53  # isolated.py invocation later, store it in *.isolated.gen.json file.
54  if mode == 'prepare':
55    prepare_isolate_call(args[1:], args[isolated] + '.gen.json')
56    return 0
57
58  swarming_client = os.path.join(TOOLS_DIR, 'swarming_client')
59  sys.stdout.flush()
60  return subprocess.call(
61      [sys.executable, os.path.join(swarming_client, 'isolate.py')] + args)
62
63
64if __name__ == '__main__':
65  sys.exit(main())
66