File: executor.py

package info (click to toggle)
python-os-brick 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,240 kB
  • sloc: python: 20,500; sh: 92; makefile: 23
file content (87 lines) | stat: -rw-r--r-- 3,123 bytes parent folder | download | duplicates (3)
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
# (c) Copyright 2013 Hewlett-Packard Development Company, L.P.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    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.

"""Generic exec utility that allows us to set the
   execute and root_helper attributes for putils.
   Some projects need their own execute wrapper
   and root_helper settings, so this provides that hook.
"""

from __future__ import annotations

import threading
from typing import Callable

from oslo_concurrency import processutils as putils
from oslo_context import context as context_utils
from oslo_utils import encodeutils

from os_brick.privileged import rootwrap as priv_rootwrap


class Executor(object):
    def __init__(self, root_helper, execute=None,
                 *args, **kwargs):
        if execute is None:
            execute = priv_rootwrap.execute
        self.set_execute(execute)
        self.set_root_helper(root_helper)

    @staticmethod
    def safe_decode(string) -> str:
        return string and encodeutils.safe_decode(string, errors='ignore')

    @classmethod
    def make_putils_error_safe(cls, exc: putils.ProcessExecutionError) -> None:
        """Converts ProcessExecutionError string attributes to unicode."""
        for field in ('stderr', 'stdout', 'cmd', 'description'):
            value = getattr(exc, field, None)
            if value:
                setattr(exc, field, cls.safe_decode(value))

    def _execute(self, *args, **kwargs) -> tuple[str, str]:
        try:
            result = self.__execute(*args, **kwargs)
            if result:
                result = (self.safe_decode(result[0]),
                          self.safe_decode(result[1]))
            return result
        except putils.ProcessExecutionError as e:
            self.make_putils_error_safe(e)
            raise

    def set_execute(self, execute: Callable) -> None:
        self.__execute = execute

    def set_root_helper(self, helper: str) -> None:
        self._root_helper = helper


class Thread(threading.Thread):
    """Thread class that inherits the parent's context.

    This is useful when you are spawning a thread and want LOG entries to
    display the right context information, such as the request.
    """

    def __init__(self, *args, **kwargs):
        # Store the caller's context as a private variable shared among threads
        self.__context__ = context_utils.get_current()
        super(Thread, self).__init__(*args, **kwargs)

    def run(self) -> None:
        # Store the context in the current thread's request store
        if self.__context__:
            self.__context__.update_store()
        super(Thread, self).run()