add missing file

This commit is contained in:
Yuxin Wu
2020-06-27 19:32:21 -07:00
parent d8ac9d8fad
commit ceec29afc9
2 changed files with 51 additions and 0 deletions
+5
View File
@@ -3,3 +3,8 @@
output/
resource
*.db
*2015*
*2017*
2020*
avatar.index
emoji.cache
+46
View File
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
import subprocess
def subproc_call(cmd, timeout=None):
"""
Execute a command with timeout, and return STDOUT and STDERR
Args:
cmd(str): the command to execute.
timeout(float): timeout in seconds.
Returns:
output(bytes), retcode(int). If timeout, retcode is -1.
"""
try:
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT,
shell=True, timeout=timeout)
return output, 0
except subprocess.TimeoutExpired as e:
logger.warn("Command '{}' timeout!".format(cmd))
if e.output:
logger.warn(e.output.decode('utf-8'))
return e.output, -1
else:
return "", -1
except subprocess.CalledProcessError as e:
logger.warn("Command '{}' failed, return code={}".format(cmd, e.returncode))
logger.warn(e.output.decode('utf-8'))
return e.output, e.returncode
except Exception:
logger.warn("Command '{}' failed to run.".format(cmd))
return "", -2
def subproc_succ(cmd):
"""
Like subproc_call, but expect the cmd to succeed.
"""
output, ret = subproc_call(cmd)
assert ret == 0
return output