Uniformize check_output calls to use moulinette helpers which shall now automatically decode() automatically + also strip() etc

This commit is contained in:
Alexandre Aubin 2021-01-01 05:03:55 +01:00
parent 0434bd5dc2
commit cce020daac
7 changed files with 24 additions and 25 deletions

View file

@ -149,7 +149,8 @@ class BaseSystemDiagnoser(Diagnoser):
# "missing some kernel info (see -v), accuracy might be reduced" # "missing some kernel info (see -v), accuracy might be reduced"
# Dunno what to do about that but we probably don't want to harass # Dunno what to do about that but we probably don't want to harass
# users with this warning ... # users with this warning ...
output, err = call.communicate() output, _ = call.communicate()
output = output.decode()
assert call.returncode in (0, 2, 3), "Return code: %s" % call.returncode assert call.returncode in (0, 2, 3), "Return code: %s" % call.returncode
# If there are multiple lines, sounds like there was some messages # If there are multiple lines, sounds like there was some messages

View file

@ -1,10 +1,11 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import psutil import psutil
import subprocess
import datetime import datetime
import re import re
from moulinette.utils.process import check_output
from yunohost.diagnosis import Diagnoser from yunohost.diagnosis import Diagnoser
@ -119,7 +120,7 @@ class SystemResourcesDiagnoser(Diagnoser):
def analyzed_kern_log(): def analyzed_kern_log():
cmd = 'tail -n 10000 /var/log/kern.log | grep "oom_reaper: reaped process" || true' cmd = 'tail -n 10000 /var/log/kern.log | grep "oom_reaper: reaped process" || true'
out = subprocess.check_output(cmd, shell=True).strip() out = check_output(cmd)
lines = out.split("\n") if out else [] lines = out.split("\n") if out else []
now = datetime.datetime.now() now = datetime.datetime.now()

View file

@ -39,7 +39,7 @@ from collections import OrderedDict
from moulinette import msignals, m18n, msettings from moulinette import msignals, m18n, msettings
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.network import download_json from moulinette.utils.network import download_json
from moulinette.utils.process import run_commands from moulinette.utils.process import run_commands, check_output
from moulinette.utils.filesystem import read_file, read_json, read_toml, read_yaml, write_to_file, write_to_json, write_to_yaml, chmod, chown, mkdir from moulinette.utils.filesystem import read_file, read_json, read_toml, read_yaml, write_to_file, write_to_json, write_to_yaml, chmod, chown, mkdir
from yunohost.service import service_status, _run_service_command from yunohost.service import service_status, _run_service_command
@ -411,10 +411,7 @@ def app_change_url(operation_logger, app, domain, path):
# grab nginx errors # grab nginx errors
# the "exit 0" is here to avoid check_output to fail because 'nginx -t' # the "exit 0" is here to avoid check_output to fail because 'nginx -t'
# will return != 0 since we are in a failed state # will return != 0 since we are in a failed state
nginx_errors = subprocess.check_output("nginx -t; exit 0", nginx_errors = check_output("nginx -t; exit 0")
stderr=subprocess.STDOUT,
shell=True).rstrip()
raise YunohostError("app_change_url_failed_nginx_reload", nginx_errors=nginx_errors) raise YunohostError("app_change_url_failed_nginx_reload", nginx_errors=nginx_errors)
logger.success(m18n.n("app_change_url_success", logger.success(m18n.n("app_change_url_success",
@ -2139,10 +2136,9 @@ def _get_git_last_commit_hash(repository, reference='HEAD'):
""" """
try: try:
commit = subprocess.check_output( cmd = "git ls-remote --exit-code {0} {1} | awk '{{print $1}}'"\
"git ls-remote --exit-code {0} {1} | awk '{{print $1}}'".format( .format(repository, reference)
repository, reference), commit = check_output(cmd)
shell=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
logger.exception("unable to get last commit from %s", repository) logger.exception("unable to get last commit from %s", repository)
raise ValueError("Unable to get last commit with git") raise ValueError("Unable to get last commit with git")

View file

@ -41,6 +41,7 @@ from moulinette import msignals, m18n, msettings
from moulinette.utils import filesystem from moulinette.utils import filesystem
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import read_file, mkdir, write_to_yaml, read_yaml from moulinette.utils.filesystem import read_file, mkdir, write_to_yaml, read_yaml
from moulinette.utils.process import check_output
from yunohost.app import ( from yunohost.app import (
app_info, _is_installed, app_info, _is_installed,
@ -2386,7 +2387,7 @@ def _recursive_umount(directory):
Args: Args:
directory -- a directory path directory -- a directory path
""" """
mount_lines = subprocess.check_output("mount").split("\n") mount_lines = check_output("mount").split("\n")
points_to_umount = [line.split(" ")[2] points_to_umount = [line.split(" ")[2]
for line in mount_lines for line in mount_lines
@ -2412,8 +2413,8 @@ def disk_usage(path):
# We don't do this in python with os.stat because we don't want # We don't do this in python with os.stat because we don't want
# to follow symlinks # to follow symlinks
du_output = subprocess.check_output(['du', '-sb', path]) du_output = check_output(['du', '-sb', path], shell=False)
return int(du_output.split()[0].decode('utf-8')) return int(du_output.split()[0])
def binary_to_human(n, customary=False): def binary_to_human(n, customary=False):

View file

@ -21,7 +21,6 @@
import os import os
import yaml import yaml
import subprocess
import shutil import shutil
import hashlib import hashlib
@ -30,6 +29,7 @@ from datetime import datetime
from moulinette import m18n from moulinette import m18n
from moulinette.utils import log, filesystem from moulinette.utils import log, filesystem
from moulinette.utils.process import check_output
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from yunohost.log import is_unit_operation from yunohost.log import is_unit_operation
@ -654,10 +654,10 @@ def manually_modified_files():
def manually_modified_files_compared_to_debian_default(ignore_handled_by_regenconf=False): def manually_modified_files_compared_to_debian_default(ignore_handled_by_regenconf=False):
# from https://serverfault.com/a/90401 # from https://serverfault.com/a/90401
files = subprocess.check_output("dpkg-query -W -f='${Conffiles}\n' '*' \ files = check_output("dpkg-query -W -f='${Conffiles}\n' '*' \
| awk 'OFS=\" \"{print $2,$1}' \ | awk 'OFS=\" \"{print $2,$1}' \
| md5sum -c 2>/dev/null \ | md5sum -c 2>/dev/null \
| awk -F': ' '$2 !~ /OK/{print $1}'", shell=True) | awk -F': ' '$2 !~ /OK/{print $1}'")
files = files.strip().split("\n") files = files.strip().split("\n")
if ignore_handled_by_regenconf: if ignore_handled_by_regenconf:

View file

@ -35,6 +35,7 @@ from datetime import datetime
from moulinette import m18n from moulinette import m18n
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from moulinette.utils.process import check_output
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import read_file, append_to_file, write_to_file from moulinette.utils.filesystem import read_file, append_to_file, write_to_file
@ -563,8 +564,7 @@ def _give_lock(action, service, p):
while son_PID == 0 and p.poll() is None: while son_PID == 0 and p.poll() is None:
# Call systemctl to get the PID # Call systemctl to get the PID
# Output of the command is e.g. ControlPID=1234 # Output of the command is e.g. ControlPID=1234
son_PID = subprocess.check_output(cmd_get_son_PID.split()) \ son_PID = check_output(cmd_get_son_PID).split("=")[1]
.strip().split("=")[1]
son_PID = int(son_PID) son_PID = int(son_PID)
time.sleep(1) time.sleep(1)
@ -720,7 +720,7 @@ def _get_journalctl_logs(service, number="all"):
services = _get_services() services = _get_services()
systemd_service = services.get(service, {}).get("actual_systemd_service", service) systemd_service = services.get(service, {}).get("actual_systemd_service", service)
try: try:
return subprocess.check_output("journalctl --no-hostname --no-pager -u {0} -n{1}".format(systemd_service, number), shell=True) return check_output("journalctl --no-hostname --no-pager -u {0} -n{1}".format(systemd_service, number))
except: except:
import traceback import traceback
return "error while get services logs from journalctl:\n%s" % traceback.format_exc() return "error while get services logs from journalctl:\n%s" % traceback.format_exc()

View file

@ -35,6 +35,7 @@ import copy
from moulinette import msignals, msettings, m18n from moulinette import msignals, msettings, m18n
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.process import check_output
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from yunohost.service import service_status from yunohost.service import service_status
@ -467,8 +468,7 @@ def user_info(username):
else: else:
try: try:
cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0]
cmd_result = subprocess.check_output(cmd, stderr=subprocess.STDOUT, cmd_result = check_output(cmd)
shell=True)
except Exception as e: except Exception as e:
cmd_result = "" cmd_result = ""
logger.warning("Failed to fetch quota info ... : %s " % str(e)) logger.warning("Failed to fetch quota info ... : %s " % str(e))