Redirect stderr to null during psutil's import.

On some systems /proc might be unavailable and psutil would freak out
printing several exceptions to stderr but strangely not raising the
exceptions for upper scope to handle
This commit is contained in:
luukas 2022-05-28 19:00:25 +03:00
parent 47fc398f26
commit d1beb2f125
No known key found for this signature in database
GPG Key ID: CC4915E8D71FC044
5 changed files with 32 additions and 6 deletions

View File

@ -1,7 +1,12 @@
from contextlib import redirect_stderr
import os import os
import socket import socket
import time import time
import psutil
from app.classes.shared.null_writer import NullWriter
with redirect_stderr(NullWriter()):
import psutil
class BedrockPing: class BedrockPing:

View File

@ -1,16 +1,20 @@
from __future__ import annotations from __future__ import annotations
from contextlib import redirect_stderr
import json import json
import logging import logging
import datetime import datetime
import base64 import base64
import typing as t import typing as t
import psutil
from app.classes.shared.null_writer import NullWriter
from app.classes.minecraft.mc_ping import ping from app.classes.minecraft.mc_ping import ping
from app.classes.models.management import HostStats from app.classes.models.management import HostStats
from app.classes.models.servers import HelperServers from app.classes.models.servers import HelperServers
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
with redirect_stderr(NullWriter()):
import psutil
if t.TYPE_CHECKING: if t.TYPE_CHECKING:
from app.classes.shared.main_controller import Controller from app.classes.shared.main_controller import Controller

View File

@ -16,15 +16,18 @@ import pathlib
import ctypes import ctypes
from datetime import datetime from datetime import datetime
from socket import gethostname from socket import gethostname
from contextlib import suppress from contextlib import redirect_stderr, suppress
import psutil
from app.classes.shared.null_writer import NullWriter
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.installer import installer from app.classes.shared.installer import installer
from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.file_helpers import FileHelpers
from app.classes.shared.translation import Translation from app.classes.shared.translation import Translation
from app.classes.web.websocket_helper import WebSocketHelper from app.classes.web.websocket_helper import WebSocketHelper
with redirect_stderr(NullWriter()):
import psutil
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
try: try:

View File

@ -0,0 +1,10 @@
import logging
import os
logger = logging.getLogger(__name__)
class NullWriter:
def write(self, data):
if os.environ["CRAFTY_LOG_NULLWRITER"] == "true":
logger.debug(data)

View File

@ -1,3 +1,4 @@
from contextlib import redirect_stderr
import os import os
import re import re
import time import time
@ -8,8 +9,6 @@ import logging.config
import subprocess import subprocess
import html import html
import tempfile import tempfile
import psutil
from psutil import NoSuchProcess
# TZLocal is set as a hidden import on win pipeline # TZLocal is set as a hidden import on win pipeline
from tzlocal import get_localzone from tzlocal import get_localzone
@ -25,6 +24,11 @@ from app.classes.models.server_permissions import PermissionsServers
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.file_helpers import FileHelpers
from app.classes.shared.null_writer import NullWriter
with redirect_stderr(NullWriter()):
import psutil
from psutil import NoSuchProcess
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)