|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +#============================================================================= |
| 4 | +# qspy_info utility |
| 5 | +# Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved. |
| 6 | +# |
| 7 | +# SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial |
| 8 | +# |
| 9 | +# This software is dual-licensed under the terms of the open source GNU |
| 10 | +# General Public License version 3 (or any later version), or alternatively, |
| 11 | +# under the terms of one of the closed source Quantum Leaps commercial |
| 12 | +# licenses. |
| 13 | +# |
| 14 | +# The terms of the open source GNU General Public License version 3 |
| 15 | +# can be found at: <www.gnu.org/licenses/gpl-3.0> |
| 16 | +# |
| 17 | +# The terms of the closed source Quantum Leaps commercial licenses |
| 18 | +# can be found at: <www.state-machine.com/licensing> |
| 19 | +# |
| 20 | +# Redistributions in source code must retain this top-level comment block. |
| 21 | +# Plagiarizing this software to sidestep the license obligations is illegal. |
| 22 | +# |
| 23 | +# Contact information: |
| 24 | +# <www.state-machine.com> |
| 25 | +# <info@state-machine.com> |
| 26 | +#============================================================================= |
| 27 | + |
| 28 | +# pylint: disable=missing-module-docstring, |
| 29 | +# pylint: disable=missing-class-docstring, |
| 30 | +# pylint: disable=missing-function-docstring |
| 31 | +# pylint: disable=broad-except |
| 32 | + |
| 33 | +from platform import python_version |
| 34 | + |
| 35 | +import argparse |
| 36 | +import socket |
| 37 | +import struct |
| 38 | +import sys |
| 39 | + |
| 40 | +#============================================================================= |
| 41 | +# Helper class for communication with the QSpy front-end |
| 42 | +# |
| 43 | +class QSpy: |
| 44 | + |
| 45 | + # public class constants |
| 46 | + VERSION = 810 |
| 47 | + TIMEOUT = 1.000 # timeout value [seconds] |
| 48 | + |
| 49 | + # private class variables... |
| 50 | + _sock = None |
| 51 | + _is_attached = False |
| 52 | + _tx_seq = 0 |
| 53 | + _host_udp = ["localhost", 7701] # list, to be converted to a tuple |
| 54 | + _local_port = 0 # let the OS decide the best local port |
| 55 | + |
| 56 | + # packets to QSpy only... |
| 57 | + _QSPY_ATTACH = 128 |
| 58 | + _QSPY_DETACH = 129 |
| 59 | + _QSPY_SAVE_DICT = 130 |
| 60 | + _QSPY_TEXT_OUT = 131 |
| 61 | + _QSPY_BIN_OUT = 132 |
| 62 | + _QSPY_MATLAB_OUT = 133 |
| 63 | + _QSPY_SEQUENCE_OUT = 134 |
| 64 | + _QSPY_CLEAR_SCREEN = 140 |
| 65 | + _QSPY_SHOW_NOTE = 141 |
| 66 | + |
| 67 | + # records directly to the Target... |
| 68 | + TO_TRG_INFO = 0 |
| 69 | + TO_TRG_RESET = 2 |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def _init(): |
| 73 | + # Create socket |
| 74 | + QSpy._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 75 | + QSpy._sock.settimeout(QSpy.TIMEOUT) # timeout for blocking socket |
| 76 | + #bufsize = QSpy._sock.getsockopt(socket.SOL_UDP, socket.SO_RCVBUF) |
| 77 | + #print("SO_RCVBUF ", bufsize) |
| 78 | + try: |
| 79 | + QSpy._sock.bind(("0.0.0.0", QSpy._local_port)) |
| 80 | + #print("bind: ", ("0.0.0.0", QSpy._local_port)) |
| 81 | + except Exception: |
| 82 | + print("UDP Socket Error"\ |
| 83 | + "Can't bind the UDP socket\nto the specified local_host") |
| 84 | + sys.exit(-1) |
| 85 | + return 0 |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def _detach(): |
| 89 | + if QSpy._sock is None: |
| 90 | + return |
| 91 | + QSpy.send_to(struct.pack("<B", QSpy._QSPY_DETACH)) |
| 92 | + time.sleep(QUTest.TIMEOUT) |
| 93 | + #QSpy._sock.shutdown(socket.SHUT_RDWR) |
| 94 | + QSpy._sock.close() |
| 95 | + QSpy._sock = None |
| 96 | + QSpy._is_attached = False |
| 97 | + |
| 98 | + @staticmethod |
| 99 | + def send_to(packet, payload=None): |
| 100 | + tx_packet = bytearray([QSpy._tx_seq]) |
| 101 | + tx_packet.extend(packet) |
| 102 | + if payload is not None: |
| 103 | + tx_packet.extend(bytes(payload, "utf-8")) |
| 104 | + tx_packet.extend(b"\0") # zero-terminate |
| 105 | + QSpy._sock.sendto(tx_packet, QSpy._host_udp) |
| 106 | + QSpy._tx_seq = (QSpy._tx_seq + 1) & 0xFF |
| 107 | + #print("sendTo", QSpy._tx_seq) |
| 108 | + |
| 109 | +#============================================================================= |
| 110 | +# main entry point to qspy_info |
| 111 | +def main(): |
| 112 | + # pylint: disable=protected-access |
| 113 | + |
| 114 | + # parse command-line arguments... |
| 115 | + parser = argparse.ArgumentParser( |
| 116 | + prog="python qspy_info.py", |
| 117 | + description="QSPY-info", |
| 118 | + epilog="More info: https://www.state-machine.com/qtools/qspy.html#qspy_info") |
| 119 | + parser.add_argument('-v', '--version', |
| 120 | + action='version', |
| 121 | + version=f"QSPY-info {QSpy.VERSION//100}."\ |
| 122 | + f"{(QSpy.VERSION//10) % 10}.{QSpy.VERSION % 10} "\ |
| 123 | + f"on Python {python_version()}", |
| 124 | + help='Display QSPY-info version') |
| 125 | + |
| 126 | + parser.add_argument('-q', '--qspy', nargs='?', default='', const='', |
| 127 | + help="optional qspy host, [:ud_port]") |
| 128 | + args = parser.parse_args() |
| 129 | + #print(args) |
| 130 | + |
| 131 | + # process command-line argumens... |
| 132 | + if args.qspy != '': |
| 133 | + qspy_conf = args.qspy.split(":") |
| 134 | + if len(qspy_conf) > 0 and not qspy_conf[0] == '': |
| 135 | + QSpy._host_udp[0] = qspy_conf[0] |
| 136 | + if len(qspy_conf) > 1 and not qspy_conf[1] == '': |
| 137 | + QSpy._host_udp[1] = int(qspy_conf[1]) |
| 138 | + |
| 139 | + #print("host_udp:", QSpy._host_udp) |
| 140 | + #return 0 |
| 141 | + |
| 142 | + # convert to immutable tuple |
| 143 | + QSpy._host_udp = tuple(QSpy._host_udp) |
| 144 | + |
| 145 | + # init QSpy socket |
| 146 | + err = QSpy._init() |
| 147 | + if err: |
| 148 | + return sys.exit(err) |
| 149 | + |
| 150 | + QSpy.send_to(struct.pack("<B", QSpy.TO_TRG_INFO)) |
| 151 | + |
| 152 | + QSpy._detach() |
| 153 | + |
| 154 | + return 0 # report to the caller (e.g., make) |
| 155 | + |
| 156 | +#============================================================================= |
| 157 | +if __name__ == "__main__": |
| 158 | + print(f"\nQSPY-info "\ |
| 159 | + f"{QSpy.VERSION//100}.{(QSpy.VERSION//10) % 10}."\ |
| 160 | + f"{QSpy.VERSION % 10} running on Python {python_version()}") |
| 161 | + print("Copyright (c) 2005-2025 Quantum Leaps, www.state-machine.com") |
| 162 | + if sys.version_info >= (3,6): |
| 163 | + main() |
| 164 | + else: |
| 165 | + print("\nERROR: QSPY-info requires Python 3.6 or newer") |
| 166 | + sys.exit(-1) |
0 commit comments