本文整理匯總了Python中subprocess.CalledProcessError方法的典型用法代碼示例。如果您正苦於以下問題:Python subprocess.CalledProcessError方法的具體用法?Python subprocess.CalledProcessError怎麽用?Python subprocess.CalledProcessError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類subprocess
的用法示例。
在下文中一共展示了subprocess.CalledProcessError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load_addresses
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def load_addresses():
"""
Separate function to avoid announce when loading addresses from keystore.
"""
global ADDRESSES
try:
response = subprocess.check_output(["hmy", "keys", "list"], env=ENVIRONMENT).decode()
except subprocess.CalledProcessError as err:
raise RuntimeError(f"Could not list keys.\n"
f"\tGot exit code {err.returncode}. Msg: {err.output}") from err
lines = response.split("\n")
if "NAME" not in lines[0] or "ADDRESS" not in lines[0]:
raise RuntimeError(f"Name or Address not found on first line if key list.")
for line in lines[1:]:
if not line:
continue
try:
name, address = line.split("\t")
except ValueError:
raise RuntimeError(f"Unexpected key list format.")
ADDRESSES[name.strip()] = address
示例2: _call_gti
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def _call_gti(self, command, num_values):
try:
response_json = check_output(command, shell=True)
result_dict = json.loads(response_json[0:len(response_json) - 1])
responses = result_dict['a']
return responses
except CalledProcessError as e:
self._logger.error("Error calling McAfee GTI client in gti module: " + e.output)
error_resp = [{self.REP_KEY: self.DEFAULT_REP}] * num_values
return error_resp
except ValueError as e:
self._logger.error("Error reading JSON response in gti module: " + e.message)
error_resp = [{self.REP_KEY: self.DEFAULT_REP}] * num_values
return error_resp
示例3: run_command
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def run_command(command, args=[], cwd=None, env=None, name='command'):
def cmd_args_to_str(cmd_args):
return ' '.join([arg if not ' ' in arg else '"%s"' % arg for arg in cmd_args])
assert isinstance(command, str) and isinstance(args, list)
args = [command] + args
check_call_args = {}
if cwd is not None:
check_call_args['cwd'] = cwd
if env is not None:
check_call_args['env'] = env
import subprocess
try:
print('Running command \'%s\': %s' % (name, subprocess.list2cmdline(args)))
subprocess.check_call(args, **check_call_args)
print('Command \'%s\' completed successfully' % name)
except subprocess.CalledProcessError as e:
raise BuildError('\'%s\' exited with error code: %s' % (name, e.returncode))
示例4: call
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def call(self, cmd, **kwargs):
print('Running "{}"'.format(cmd), file=sys.stderr)
expect = kwargs.pop("expect", [dict(return_codes=[os.EX_OK], stdout=None, stderr=None)])
process = subprocess.Popen(cmd, stdin=kwargs.get("stdin", subprocess.PIPE), stdout=subprocess.PIPE,
stderr=subprocess.PIPE, **kwargs)
out, err = process.communicate()
return_code = process.poll()
out = out.decode(sys.stdin.encoding)
err = err.decode(sys.stdin.encoding)
def match(return_code, out, err, expected):
exit_ok = return_code in expected["return_codes"]
stdout_ok = re.search(expected.get("stdout") or "", out)
stderr_ok = re.search(expected.get("stderr") or "", err)
return exit_ok and stdout_ok and stderr_ok
if not any(match(return_code, out, err, exp) for exp in expect):
print(err)
e = subprocess.CalledProcessError(return_code, cmd, output=out)
e.stdout, e.stderr = out, err
raise e
return self.SubprocessResult(out, err, return_code)
示例5: manage
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def manage():
def call(*args, **kwargs):
ignore_errors = kwargs.pop("ignore_errors", False)
assert not kwargs
cmd = [
sys.executable,
os.path.join(os.path.dirname(__file__), "testprj", "manage.py"),
] + list(args)
try:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if not ignore_errors:
raise
return e.output
return call
示例6: test_django_traceback
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def test_django_traceback(manage):
with pytest.raises(subprocess.CalledProcessError) as e:
manage("errcmd")
assert e.value.output == b"CommandError: Raised error description\n"
assert e.value.returncode == 1
with pytest.raises(subprocess.CalledProcessError) as e:
manage("errcmd", "--traceback")
e = e.value
lines = e.output.splitlines()
assert lines[0] == b"Traceback (most recent call last):"
for line in lines[1:-1]:
assert line.startswith(b" ")
# Use `.endswith()` because of differences between CPython and pypy
assert lines[-1].endswith(b"CommandError: Raised error description")
assert e.returncode == 1
示例7: _verify_docker_image_size
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def _verify_docker_image_size(self, image_name):
"""Verifies size of Docker image.
Args:
image_name: name of the Docker image.
Returns:
True if image size is withing the limits, False otherwise.
"""
shell_call(['docker', 'pull', image_name])
try:
image_size = subprocess.check_output(
['docker', 'inspect', '--format={{.Size}}', image_name]).strip()
image_size = int(image_size) if PY3 else long(image_size)
except (ValueError, subprocess.CalledProcessError) as e:
logging.error('Failed to determine docker image size: %s', e)
return False
logging.info('Size of docker image %s is %d', image_name, image_size)
if image_size > MAX_DOCKER_IMAGE_SIZE:
logging.error('Image size exceeds limit %d', MAX_DOCKER_IMAGE_SIZE)
return image_size <= MAX_DOCKER_IMAGE_SIZE
示例8: run
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def run(self):
"""Runs validation of all submissions."""
cmd = ['gsutil', 'ls', os.path.join(self.source_dir, '**')]
try:
files_list = subprocess.check_output(cmd).split('\n')
except subprocess.CalledProcessError:
logging.error('Can''t read source directory')
all_submissions = [
s for s in files_list
if s.endswith('.zip') or s.endswith('.tar') or s.endswith('.tar.gz')
]
for submission_path in all_submissions:
self.validate_and_copy_one_submission(submission_path)
self.stats.log_stats()
self.save_id_to_path_mapping()
if self.containers_file:
with open(self.containers_file, 'w') as f:
f.write('\n'.join(sorted(self.list_of_containers)))
示例9: _verify_docker_image_size
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def _verify_docker_image_size(self, image_name):
"""Verifies size of Docker image.
Args:
image_name: name of the Docker image.
Returns:
True if image size is within the limits, False otherwise.
"""
shell_call(['docker', 'pull', image_name])
try:
image_size = subprocess.check_output(
['docker', 'inspect', '--format={{.Size}}', image_name]).strip()
image_size = int(image_size) if PY3 else long(image_size)
except (ValueError, subprocess.CalledProcessError) as e:
logging.error('Failed to determine docker image size: %s', e)
return False
logging.info('Size of docker image %s is %d', image_name, image_size)
if image_size > MAX_DOCKER_IMAGE_SIZE:
logging.error('Image size exceeds limit %d', MAX_DOCKER_IMAGE_SIZE)
return image_size <= MAX_DOCKER_IMAGE_SIZE
示例10: delete_local_docker_cache
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def delete_local_docker_cache(docker_tag):
"""
Delete the local docker cache for the entire docker image chain
:param docker_tag: Docker tag
:return: None
"""
history_cmd = ['docker', 'history', '-q', docker_tag]
try:
image_ids_b = subprocess.check_output(history_cmd)
image_ids_str = image_ids_b.decode('utf-8').strip()
layer_ids = [id.strip() for id in image_ids_str.split('\n') if id != '<missing>']
delete_cmd = ['docker', 'image', 'rm', '--force']
delete_cmd.extend(layer_ids)
subprocess.check_call(delete_cmd)
except subprocess.CalledProcessError as error:
# Could be caused by the image not being present
logging.debug('Error during local cache deletion %s', error)
示例11: check_project
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def check_project(project_name):
if 'token' in if_config_vars and len(if_config_vars['token']) != 0:
logger.debug(project_name)
try:
# check for existing project
check_url = urlparse.urljoin(if_config_vars['if_url'], '/api/v1/getprojectstatus')
output_check_project = subprocess.check_output('curl "' + check_url + '?userName=' + if_config_vars['user_name'] + '&token=' + if_config_vars['token'] + '&projectList=%5B%7B%22projectName%22%3A%22' + project_name + '%22%2C%22customerName%22%3A%22' + if_config_vars['user_name'] + '%22%2C%22projectType%22%3A%22CUSTOM%22%7D%5D&tzOffset=-14400000"', shell=True)
# create project if no existing project
if project_name not in output_check_project:
logger.debug('creating project')
create_url = urlparse.urljoin(if_config_vars['if_url'], '/api/v1/add-custom-project')
output_create_project = subprocess.check_output('no_proxy= curl -d "userName=' + if_config_vars['user_name'] + '&token=' + if_config_vars['token'] + '&projectName=' + project_name + '&instanceType=PrivateCloud&projectCloudType=PrivateCloud&dataType=' + get_data_type_from_project_type() + '&samplingInterval=' + str(if_config_vars['sampling_interval'] / 60) + '&samplingIntervalInSeconds=' + str(if_config_vars['sampling_interval']) + '&zone=&email=&access-key=&secrete-key=&insightAgentType=' + get_insight_agent_type_from_project_type() + '" -H "Content-Type: application/x-www-form-urlencoded" -X POST ' + create_url + '?tzOffset=-18000000', shell=True)
# set project name to proposed name
if_config_vars['project_name'] = project_name
# try to add new project to system
if 'system_name' in if_config_vars and len(if_config_vars['system_name']) != 0:
system_url = urlparse.urljoin(if_config_vars['if_url'], '/api/v1/projects/update')
output_update_project = subprocess.check_output('no_proxy= curl -d "userName=' + if_config_vars['user_name'] + '&token=' + if_config_vars['token'] + '&operation=updateprojsettings&projectName=' + project_name + '&systemName=' + if_config_vars['system_name'] + '" -H "Content-Type: application/x-www-form-urlencoded" -X POST ' + system_url + '?tzOffset=-18000000', shell=True)
except subprocess.CalledProcessError as e:
logger.error('Unable to create project for ' + project_name + '. Data will be sent to ' + if_config_vars['project_name'])
示例12: invalid_virtualenv_reason
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def invalid_virtualenv_reason(venv_path, source_python, destination_python, options):
try:
orig_path = get_original_path(venv_path)
except CalledProcessError:
return 'could not inspect metadata'
if not samefile(orig_path, venv_path):
return 'virtualenv moved %s -> %s' % (timid_relpath(orig_path), timid_relpath(venv_path))
elif has_system_site_packages(destination_python) != options.system_site_packages:
return 'system-site-packages changed, to %s' % options.system_site_packages
if source_python is None:
return
destination_version = get_python_version(destination_python)
source_version = get_python_version(source_python)
if source_version != destination_version:
return 'python version changed %s -> %s' % (destination_version, source_version)
示例13: runTail
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def runTail(logFile):
cmd = ['tail', '-n', '100', '-f', LOG_FILE]
while (True):
try:
proc = subprocess.Popen(cmd, \
stdout=subprocess.PIPE, \
universal_newlines=True)
for line in iter(proc.stdout.readline, ''):
yield line
proc.stdout.close()
proc.wait()
except subprocess.CalledProcessError:
print('Failed to open the log file, will try again...')
sleep(1)
continue
sleep(2)
示例14: test_and_load_keystore_directory
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def test_and_load_keystore_directory():
"""
CRITICAL TEST
"""
global KEYSTORE_PATH
try:
response = subprocess.check_output(["hmy", "keys", "location"], env=ENVIRONMENT).decode().strip()
except subprocess.CalledProcessError as err:
log(f"Failed: Could not get keystore path.\n"
f"\tGot exit code {err.returncode}. Msg: {err.output}")
return False
if not os.path.exists(response):
log(f"Failed: '{response}' is not a valid path")
return False
KEYSTORE_PATH = response
log("Passed", error=False)
return True
示例15: get_url
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import CalledProcessError [as 別名]
def get_url():
# Getting the json with all data regarding the stream
try:
response = subprocess.Popen(
["livestreamer.exe", "--http-header", "Client-ID=ewvlchtxgqq88ru9gmfp1gmyt6h2b93",
channel_url, "-j"], stdout=subprocess.PIPE).communicate()[0]
except subprocess.CalledProcessError:
print "An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?"
sys.exit(1)
except OSError:
print "An error has occurred while trying to use livestreamer package. Is it installed? Do you have Python in your PATH variable?"
# Decoding the url to the worst quality of the stream
try:
url = json.loads(response)['streams']['audio_only']['url']
except:
try:
url = json.loads(response)['streams']['worst']['url']
except (ValueError, KeyError):
print "An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?"
sys.exit(1)
return url