11cb0ef41Sopenharmony_ci#!/usr/bin/env python3
21cb0ef41Sopenharmony_ci# Copyright 2017 the V8 project authors. All rights reserved.
31cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
41cb0ef41Sopenharmony_ci# found in the LICENSE file.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci# for py2/py3 compatibility
71cb0ef41Sopenharmony_cifrom __future__ import print_function
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciimport os
101cb0ef41Sopenharmony_ciimport pipes
111cb0ef41Sopenharmony_ciimport shutil
121cb0ef41Sopenharmony_ciimport stat
131cb0ef41Sopenharmony_ciimport subprocess
141cb0ef41Sopenharmony_ciimport sys
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciDEPOT_TOOLS_URL = \
171cb0ef41Sopenharmony_ci  "https://chromium.googlesource.com/chromium/tools/depot_tools.git"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cidef EnsureDepotTools(v8_path, fetch_if_not_exist):
201cb0ef41Sopenharmony_ci  def _Get(v8_path):
211cb0ef41Sopenharmony_ci    depot_tools = os.path.join(v8_path, "_depot_tools")
221cb0ef41Sopenharmony_ci    try:
231cb0ef41Sopenharmony_ci      gclient_path = os.path.join(depot_tools, "gclient.py")
241cb0ef41Sopenharmony_ci      if os.path.isfile(gclient_path):
251cb0ef41Sopenharmony_ci        return depot_tools
261cb0ef41Sopenharmony_ci    except:
271cb0ef41Sopenharmony_ci      pass
281cb0ef41Sopenharmony_ci    if fetch_if_not_exist:
291cb0ef41Sopenharmony_ci      print("Checking out depot_tools.")
301cb0ef41Sopenharmony_ci      # shell=True needed on Windows to resolve git.bat.
311cb0ef41Sopenharmony_ci      subprocess.check_call("git clone {} {}".format(
321cb0ef41Sopenharmony_ci          pipes.quote(DEPOT_TOOLS_URL),
331cb0ef41Sopenharmony_ci          pipes.quote(depot_tools)), shell=True)
341cb0ef41Sopenharmony_ci      # Using check_output to hide warning messages.
351cb0ef41Sopenharmony_ci      subprocess.check_output(
361cb0ef41Sopenharmony_ci          [sys.executable, gclient_path, "metrics", "--opt-out"],
371cb0ef41Sopenharmony_ci          cwd=depot_tools)
381cb0ef41Sopenharmony_ci      return depot_tools
391cb0ef41Sopenharmony_ci    return None
401cb0ef41Sopenharmony_ci  depot_tools = _Get(v8_path)
411cb0ef41Sopenharmony_ci  assert depot_tools is not None
421cb0ef41Sopenharmony_ci  print("Using depot tools in %s" % depot_tools)
431cb0ef41Sopenharmony_ci  return depot_tools
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cidef UninitGit(v8_path):
461cb0ef41Sopenharmony_ci  print("Uninitializing temporary git repository")
471cb0ef41Sopenharmony_ci  target = os.path.join(v8_path, ".git")
481cb0ef41Sopenharmony_ci  if os.path.isdir(target):
491cb0ef41Sopenharmony_ci    print(">> Cleaning up %s" % target)
501cb0ef41Sopenharmony_ci    def OnRmError(func, path, exec_info):
511cb0ef41Sopenharmony_ci      # This might happen on Windows
521cb0ef41Sopenharmony_ci      os.chmod(path, stat.S_IWRITE)
531cb0ef41Sopenharmony_ci      os.unlink(path)
541cb0ef41Sopenharmony_ci    shutil.rmtree(target, onerror=OnRmError)
55