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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# Copyright (c) 2018 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "cgitize" project.
# For details, see https://github.com/egor-tensin/cgitize.
# Distributed under the MIT License.
import contextlib
import logging
import os
import subprocess
def run(*args, capture_output=False, **kwargs):
stdout = None
stderr = None
if capture_output:
stdout = subprocess.PIPE
stderr = subprocess.STDOUT
logging.debug('%s', args)
result = subprocess.run(args, check=True, stdout=stdout, stderr=stderr,
encoding='utf-8', **kwargs)
if result.stdout is not None:
logging.debug('\n%s', result.stdout)
return result.stdout
def try_run(*args, **kwargs):
try:
run(*args, **kwargs)
return True
except subprocess.CalledProcessError as e:
return e.returncode == 0
def run_capture(*args, **kwargs):
return run(*args, capture_output=True, **kwargs)
def try_run_capture(*args, **kwargs):
try:
return True, run(*args, capture_output=True, **kwargs)
except subprocess.CalledProcessError as e:
return e.returncode == 0, e.output
@contextlib.contextmanager
def chdir(new_cwd):
old_cwd = os.getcwd()
os.chdir(new_cwd)
try:
yield
finally:
os.chdir(old_cwd)
|