Exemple #1
0
    def _initialize_key():
        global key_event, registry_key_handle, registry_access_flags

        try:
            # make sure the registry key is not currently open
            if registry_key_handle:
                _close_key()

            # get an event for monitoring registry updates
            key_event = win32event.CreateEvent(None, True, True, None)
            #print(f"KEY_EVENT: {key_event}")

            # open the registry key
            registry_key_handle = win32api.RegOpenKeyEx(
                win32con.HKEY_CURRENT_USER,
                r"SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss", 0,
                registry_access_flags)
            #print(f"registry_key_handle: {registry_key_handle}")

            # register for registry change events
            win32api.RegNotifyChangeKeyValue(
                registry_key_handle, True, win32api.REG_NOTIFY_CHANGE_LAST_SET,
                key_event, True)

            # trigger reading the list for the first time
            win32event.SetEvent(key_event)
        except WindowsError:
            log_exception(f'[_initialize_key()] {sys.exc_info()[1]}')
Exemple #2
0
def _run_cmd(command_line):
    result = error = ""
    #print(f"_run_cmd(): RUNNING - command line is {command_line}.")
    try:
        # for testing
        #raise subprocess.CalledProcessError(-4294967295, command_line, termination_error.encode('UTF-16-LE'))

        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = subprocess.SW_HIDE

        tmp = subprocess.check_output(command_line,
                                      stderr=subprocess.STDOUT,
                                      startupinfo=startupinfo)
        result = _decode(tmp)
        #print(f"RESULT: command: {' '.join(command_line)}, result: {result}")
    except subprocess.CalledProcessError as exc:
        result = ""

        # decode the error
        error = _decode(exc.output)

        # log additional info for this particular case
        if error == termination_error:
            logging.error(
                f'_run_cmd(): failed to run command - error: {error}')
            logging.error(f'_run_cmd(): - {restart_message}')
    except:
        result = ""
        log_exception(f'[_run_cmd()] {sys.exc_info()[1]}')

    # return results for the last attempt
    #print(f'_run_cmd(): RETURNING - result: {result}, error: {error}')
    return [result, error]
Exemple #3
0
def _run_cmd(command_line):
    result = error = ""
    #print(f"_run_cmd(): RUNNING - command line is {command_line}.")
    try:
        # for testing
        #raise subprocess.CalledProcessError(-4294967295, command_line, 'The Windows Subsystem for Linux instance has terminated.'.encode('UTF-16-LE'))

        tmp = subprocess.check_output(command_line, stderr=subprocess.STDOUT)
        result = _decode(tmp)
        #print(f"RESULT: command: {' '.join(command_line)}, result: {result}")
    except subprocess.CalledProcessError as exc:
        result = ""

        # decode the error
        error = _decode(exc.output)

        # log additional info for this particular case
        if error == 'The Windows Subsystem for Linux instance has terminated.':
            logging.error(
                f'_run_cmd(): failed to run command - error: {error}')
            logging.error(f'_run_cmd(): - wsl path detection is offline')
            logging.error(
                f'_run_cmd(): - you need to restart your wsl session, e.g. "wsl --terminate <distro>; wsl"'
            )
    except:
        result = ""
        log_exception(f'[_run_cmd()] {sys.exc_info()[1]}')

    # return results for the last attempt
    #print(f'_run_cmd(): RETURNING - result: {result}, error: {error}')
    return [result, error]
Exemple #4
0
    def _update_wsl_distros():
        global ctx, registry_key_handle, wsl_distros, registry_access_flags

        # make sure registry is open
        if not registry_key_handle:
            _initialize_key()

        distro_handle = None
        try:
            # check for registry changes
            result = win32event.WaitForSingleObjectEx(key_event, 0, False)
            # for testing
            if False:
                print(
                    f"WAIT - {result=} (looking for 'win32con.WAIT_OBJECT_0')")
                print(f'WAIT - {win32con.WAIT_OBJECT_0=})')
                print(f'WAIT - {win32con.WAIT_ABANDONED=})')
                print(f'WAIT - {win32con.WAIT_TIMEOUT=})')
            if result == win32con.WAIT_OBJECT_0:
                # registry has changed since we last read it, load the distros
                subkeys = win32api.RegEnumKeyEx(registry_key_handle)
                for subkey in subkeys:
                    #print(f'{subkey=}')

                    distro_handle = win32api.RegOpenKeyEx(
                        registry_key_handle, subkey[0], 0,
                        registry_access_flags)
                    #print(f"{distro_handle=}")

                    distro_name = win32api.RegQueryValueEx(
                        distro_handle, 'DistributionName')[0]
                    #print(f'{distro_name=}')
                    wsl_distros.append(distro_name)

                    win32api.RegCloseKey(distro_handle)

                # reset the event, will be set by system if reg key changes
                win32event.ResetEvent(key_event)

            elif result != win32con.WAIT_TIMEOUT:
                # something unexpected happened
                error = win32api.GetLastError()
                _close_key()
                raise Exception(
                    'failed while checking for wsl registry updates: {result=}: {error=}'
                )
        except WindowsError:
            if distro_handle:
                win32api.RegCloseKey(distro_handle)
            log_exception(f'[_update_wsl_distros()] {sys.exc_info()[1]}')
 def load_patterns(self, pattern_path: str, classes: set[str]) -> list[NoisePattern]:
     """Load the patterns"""
     try:
         json_patterns = json.loads(resource.read(pattern_path))
         pattern_builder = PatternBuilder()
         patterns: list[NoisePattern] = []
         for key, config in json_patterns.items():
             pattern = pattern_builder.build(key, config)
             if pattern is not None:
                 patterns.append(pattern)
         return patterns
     except Exception:
         log_exception(f"[parrot] invalid pattern file: {pattern_path}")
         return []