forked from defold/defold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
101 lines (87 loc) · 3.34 KB
/
run.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright 2020-2024 The Defold Foundation
# Copyright 2014-2020 King
# Copyright 2009-2014 Ragnar Svensson, Christian Murray
# Licensed under the Defold License version 1.0 (the "License"); you may not use
# this file except in compliance with the License.
#
# You may obtain a copy of the License, together with FAQs at
# https://www.defold.com/license
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
from log import log
import subprocess
import sys
class ExecException(Exception):
def __init__(self, retcode, output):
self.retcode = retcode
self.output = output
def __init__(self, retcode):
self.retcode = retcode
self.output = ''
def _to_str(x):
if x is None:
return ''
elif isinstance(x, (bytes, bytearray)):
x = str(x, encoding='utf-8')
return x
def _exec_command(arg_list, **kwargs):
silent = False
if 'silent' in kwargs:
silent = True
del kwargs['silent']
arg_str = arg_list
if not isinstance(arg_str, str):
arg_list = [_to_str(x) for x in arg_list]
arg_str = ' '.join(arg_list)
if not silent: log('[exec] %s' % arg_str)
if sys.stdout.isatty():
# If not on CI, we want the colored output, and we get the output as it runs, in order to preserve the colors
if not 'stdout' in kwargs:
kwargs['stdout'] = subprocess.PIPE # Only way to get output from the command
process = subprocess.Popen(arg_list, **kwargs)
output = process.communicate()[0]
if process.returncode != 0:
if not silent: log(_to_str(output))
else:
# On the CI machines, we make sure we produce a steady stream of output
# However, this also makes us lose the color information
if 'stdout' in kwargs:
del kwargs['stdout']
process = subprocess.Popen(arg_list, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, **kwargs)
output = ''
while True:
line = process.stdout.readline().decode(errors='replace')
if line != '':
output += line
if not silent: log(line.rstrip())
else:
break
if process.wait() != 0:
e = ExecException(process.returncode)
e.output = output
log('[exec] %s' % arg_str)
log("Error: %s" % _to_str(output))
raise e
output = _to_str(output)
return output.strip()
def command(args, **kwargs):
if kwargs.get("shell") is None:
kwargs["shell"] = False
# Executes a command, and exits if it fails
try:
return _exec_command(args, **kwargs)
except ExecException as e:
sys.exit(e.retcode)
def shell_command(args, **kwargs):
# Executes a command, and exits if it fails
try:
return _exec_command(args, shell = True, **kwargs)
except ExecException as e:
sys.exit(e.retcode)
def env_command(env, args, **kwargs):
return _exec_command(args, shell = False, stdout = None, env = env, **kwargs)
def env_shell_command(env, args, **kwargs):
return _exec_command(args, shell = True, env = env, **kwargs)