aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/py_utils/py_utils/shell_util.py
blob: 2a529c89748717b98dd0a9871006e1ef59fb4077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright 2017 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.
#
# Shell scripting helpers (created for Telemetry dependency roll scripts).

import os as _os
import shutil as _shutil
import subprocess as _subprocess
import tempfile as _tempfile
from contextlib import contextmanager as _contextmanager

@_contextmanager
def ScopedChangeDir(new_path):
  old_path = _os.getcwd()
  _os.chdir(new_path)
  print '> cd', _os.getcwd()
  try:
    yield
  finally:
    _os.chdir(old_path)
    print '> cd', old_path

@_contextmanager
def ScopedTempDir():
  temp_dir = _tempfile.mkdtemp()
  try:
    with ScopedChangeDir(temp_dir):
      yield
  finally:
    _shutil.rmtree(temp_dir)

def CallProgram(path_parts, *args, **kwargs):
  '''Call an executable os.path.join(*path_parts) with the arguments specified
  by *args. Any keyword arguments are passed as environment variables.'''
  args = [_os.path.join(*path_parts)] + list(args)
  env = dict(_os.environ)
  env.update(kwargs)
  print '>', ' '.join(args)
  _subprocess.check_call(args, env=env)