• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
33def rebase_directories(args, abs_base):
34  """Rebases all paths to be relative to abs_base."""
35  def replace(index):
36    args[index] = os.path.relpath(os.path.abspath(args[index]), abs_base)
37  for i, arg in enumerate(args):
38    if arg in ['--isolate', '--isolated']:
39      replace(i + 1)
40    if arg == '--path-variable':
41      # Path variables have a triple form: --path-variable NAME <path>.
42      replace(i + 2)
43
44def main():
45  logging.basicConfig(level=logging.ERROR, format='%(levelname)7s %(message)s')
46  if len(sys.argv) < 2:
47    print >> sys.stderr, 'Internal failure; mode required'
48    return 1
49  mode = sys.argv[1]
50  args = sys.argv[1:]
51  isolate = None
52  isolated = None
53  for i, arg in enumerate(args):
54    if arg == '--isolate':
55      isolate = i + 1
56    if arg == '--isolated':
57      isolated = i + 1
58  if not isolate or not isolated:
59    print >> sys.stderr, 'Internal failure'
60    return 1
61
62  # Make sure all paths are relative to the isolate file. This is an
63  # expectation of the go binaries. In gn, this script is not called
64  # relative to the isolate file, but relative to the product dir.
65  new_base = os.path.abspath(os.path.dirname(args[isolate]))
66  rebase_directories(args, new_base)
67  assert args[isolate] == os.path.basename(args[isolate])
68  os.chdir(new_base)
69
70  # In 'prepare' mode just collect all required information for postponed
71  # isolated.py invocation later, store it in *.isolated.gen.json file.
72  if mode == 'prepare':
73    prepare_isolate_call(args[1:], args[isolated] + '.gen.json')
74    return 0
75
76  swarming_client = os.path.join(TOOLS_DIR, 'swarming_client')
77  sys.stdout.flush()
78  return subprocess.call(
79      [sys.executable, os.path.join(swarming_client, 'isolate.py')] + args)
80
81
82if __name__ == '__main__':
83  sys.exit(main())
84