node_common.py 1.66 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# Copyright 2017 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

6 7 8
# for py2/py3 compatibility
from __future__ import print_function

9
import os
10
import pipes
11 12 13
import shutil
import stat
import subprocess
14
import sys
15 16 17 18 19 20 21 22

DEPOT_TOOLS_URL = \
  "https://chromium.googlesource.com/chromium/tools/depot_tools.git"

def EnsureDepotTools(v8_path, fetch_if_not_exist):
  def _Get(v8_path):
    depot_tools = os.path.join(v8_path, "_depot_tools")
    try:
23 24
      gclient_path = os.path.join(depot_tools, "gclient.py")
      if os.path.isfile(gclient_path):
25 26 27 28
        return depot_tools
    except:
      pass
    if fetch_if_not_exist:
29
      print("Checking out depot_tools.")
30 31 32 33
      # shell=True needed on Windows to resolve git.bat.
      subprocess.check_call("git clone {} {}".format(
          pipes.quote(DEPOT_TOOLS_URL),
          pipes.quote(depot_tools)), shell=True)
34 35 36 37
      # Using check_output to hide warning messages.
      subprocess.check_output(
          [sys.executable, gclient_path, "metrics", "--opt-out"],
          cwd=depot_tools)
38 39 40 41
      return depot_tools
    return None
  depot_tools = _Get(v8_path)
  assert depot_tools is not None
42
  print("Using depot tools in %s" % depot_tools)
43 44 45
  return depot_tools

def UninitGit(v8_path):
46
  print("Uninitializing temporary git repository")
47 48
  target = os.path.join(v8_path, ".git")
  if os.path.isdir(target):
49
    print(">> Cleaning up %s" % target)
50 51 52 53 54
    def OnRmError(func, path, exec_info):
      # This might happen on Windows
      os.chmod(path, stat.S_IWRITE)
      os.unlink(path)
    shutil.rmtree(target, onerror=OnRmError)