|
| 1 | +"""How to detect, publish, and query system hardware and software resources.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import platform |
| 5 | +from dataclasses import dataclass |
| 6 | + |
| 7 | +import nisyscfg |
| 8 | +import nisyscfg.component_info |
| 9 | +import nisyscfg.hardware_resource |
| 10 | +from ni.datastore.data import ( |
| 11 | + DataStoreClient, |
| 12 | + TestResult, |
| 13 | +) |
| 14 | +from ni.datastore.metadata import ( |
| 15 | + HardwareItem, |
| 16 | + MetadataStoreClient, |
| 17 | + Operator, |
| 18 | + SoftwareItem, |
| 19 | + TestStation, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@dataclass(frozen=True) |
| 24 | +class SystemMetadata: |
| 25 | + """Represents the available resources on a system.""" |
| 26 | + |
| 27 | + operator: Operator |
| 28 | + test_station: TestStation |
| 29 | + hardware_items: list[HardwareItem] |
| 30 | + software_items: list[SoftwareItem] |
| 31 | + |
| 32 | + |
| 33 | +def main() -> None: |
| 34 | + """Detect, publish, and query hardware and software resources from the local system.""" |
| 35 | + print("Scanning system for metadata...") |
| 36 | + system_metadata = detect_system_resources() |
| 37 | + |
| 38 | + print("Publishing detected system metadata...") |
| 39 | + test_result_id = publish_empty_test_result(system_metadata) |
| 40 | + |
| 41 | + print("Querying system metadata...") |
| 42 | + test_result = query_test_result(test_result_id) |
| 43 | + |
| 44 | + print() |
| 45 | + print(f"TestResult ID: {test_result.id}") |
| 46 | + print(f"- Operator: {test_result.operator_id}") |
| 47 | + print(f"- Test Station: {test_result.test_station_id}") |
| 48 | + print(f"- Installed Software: {len(test_result.software_item_ids)} packages") |
| 49 | + print(f"- Available Hardware: {len(test_result.hardware_item_ids)} devices") |
| 50 | + |
| 51 | + |
| 52 | +def detect_system_resources(system_target: str = "localhost") -> SystemMetadata: |
| 53 | + """Scan the specified system_target and return SystemMetadata describing the system.""" |
| 54 | + with nisyscfg.Session(target=system_target) as session: |
| 55 | + ni_device_filter = session.create_filter() |
| 56 | + ni_device_filter.is_ni_product = True |
| 57 | + ni_device_filter.is_device = True |
| 58 | + ni_device_filter.is_present = True |
| 59 | + |
| 60 | + operator = create_operator() |
| 61 | + test_station = create_test_station(session) |
| 62 | + hardware = [ |
| 63 | + create_hardware_item(entry) for entry in session.find_hardware(ni_device_filter) |
| 64 | + ] |
| 65 | + software = [ |
| 66 | + create_software_item(entry) for entry in session.get_installed_software_components() |
| 67 | + ] |
| 68 | + system_metadata = SystemMetadata(operator, test_station, hardware, software) |
| 69 | + return system_metadata |
| 70 | + |
| 71 | + |
| 72 | +def create_hardware_item( |
| 73 | + hardware_entry: nisyscfg.hardware_resource.HardwareResource, |
| 74 | +) -> HardwareItem: |
| 75 | + """Create a new HardwareItem instance from the specified nisyscfg entry.""" |
| 76 | + manufacturer = hardware_entry.vendor_name |
| 77 | + model = hardware_entry.product_name |
| 78 | + serial_number = hardware_entry.serial_number |
| 79 | + new_instance = HardwareItem(manufacturer=manufacturer, model=model, serial_number=serial_number) |
| 80 | + return new_instance |
| 81 | + |
| 82 | + |
| 83 | +def create_software_item(software_entry: nisyscfg.component_info.ComponentInfo) -> SoftwareItem: |
| 84 | + """Create a new SoftwareItem instance from the specified nisyscfg entry.""" |
| 85 | + new_instance = SoftwareItem(product=software_entry.id, version=software_entry.version) |
| 86 | + return new_instance |
| 87 | + |
| 88 | + |
| 89 | +def create_test_station(session: nisyscfg.Session) -> TestStation: |
| 90 | + """Create a new TestStation instance from the specified nisyscfg session.""" |
| 91 | + new_instance = TestStation(name=session.hostname) |
| 92 | + return new_instance |
| 93 | + |
| 94 | + |
| 95 | +def create_operator(name: str = "") -> Operator: |
| 96 | + """Create a new Operator instance using the specified name. Otherwise, use the active user.""" |
| 97 | + if not name: |
| 98 | + host_os = platform.system() |
| 99 | + if host_os == "Windows": |
| 100 | + username_variable = "USERNAME" |
| 101 | + elif host_os == "Linux": |
| 102 | + username_variable = "USER" |
| 103 | + else: |
| 104 | + raise NotImplementedError(f"{host_os} support not implemented") |
| 105 | + name = os.environ.get(username_variable, "Unknown operator") |
| 106 | + |
| 107 | + new_instance = Operator(name=name) |
| 108 | + return new_instance |
| 109 | + |
| 110 | + |
| 111 | +def publish_empty_test_result(system_metadata: SystemMetadata) -> str: |
| 112 | + """Publish a TestResult with the specified system_metadata and return its ID.""" |
| 113 | + with MetadataStoreClient() as metadata_store_client: |
| 114 | + operator_id = metadata_store_client.create_operator(system_metadata.operator) |
| 115 | + test_station_id = metadata_store_client.create_test_station(system_metadata.test_station) |
| 116 | + hardware_item_ids = [ |
| 117 | + metadata_store_client.create_hardware_item(entry) |
| 118 | + for entry in system_metadata.hardware_items |
| 119 | + ] |
| 120 | + software_item_ids = [ |
| 121 | + metadata_store_client.create_software_item(entry) |
| 122 | + for entry in system_metadata.software_items |
| 123 | + ] |
| 124 | + |
| 125 | + empty_result = TestResult( |
| 126 | + name="system metadata result", |
| 127 | + operator_id=operator_id, |
| 128 | + test_station_id=test_station_id, |
| 129 | + software_item_ids=software_item_ids, |
| 130 | + hardware_item_ids=hardware_item_ids, |
| 131 | + ) |
| 132 | + |
| 133 | + with DataStoreClient() as datastore_client: |
| 134 | + test_result_id = datastore_client.create_test_result(empty_result) |
| 135 | + |
| 136 | + return test_result_id |
| 137 | + |
| 138 | + |
| 139 | +def query_test_result(test_result_id: str) -> TestResult: |
| 140 | + """Query the NI DataStore Service for the specified TestResult and return it.""" |
| 141 | + with DataStoreClient() as datastore_client: |
| 142 | + test_result = datastore_client.get_test_result(test_result_id) |
| 143 | + return test_result |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments