Skip to content

Commit

Permalink
Add a method to detect CPU arch type (e.g. ARM64)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699009519
  • Loading branch information
mjoliver authored and copybara-github committed Nov 22, 2024
1 parent 2ca6a23 commit eeda7a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
35 changes: 35 additions & 0 deletions gwinpy/wmi/hw_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,41 @@ def MacAddresses(self, pci_only=False):
addresses.append(address)
return addresses

def Architecture(self):
"""Get the architecture of the local machine.
Returns:
The architecture string if found; else None.
"""
# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor
architecture_map = {
0: 'x86',
1: 'MIPS',
2: 'Alpha',
3: 'PowerPC',
5: 'ARM',
6: 'ia64',
9: 'x64',
12: 'ARM64',
}
query = 'SELECT Architecture FROM Win32_Processor'
results = self.wmi.Query(query)
if results:
logging.debug(
'Win32_ComputerSystem/win32-processor: %s',
results[0].Architecture.rstrip(),
)
try:
archtype = architecture_map[int(results[0].Architecture.rstrip())]
except KeyError:
logging.warning(
'Unknown architecture %s.', results[0].Architecture.rstrip()
)
return None
return archtype
logging.warning('No results for %s.', query)
return None

def PciDevices(self):
"""Get local PCI devices.
Expand Down
8 changes: 8 additions & 0 deletions gwinpy/wmi/hw_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def testBiosSerial(self):
self.hwinfo.wmi.Query.return_value = None
self.assertIsNone(self.hwinfo.BiosSerial())

def testArchType(self):
self.hwinfo.wmi.Query.return_value = [mock.Mock(Architecture='0')]
self.assertEqual(self.hwinfo.Architecture(), 'x86')
self.hwinfo.wmi.Query.return_value = [mock.Mock(Architecture='12')]
self.assertEqual(self.hwinfo.Architecture(), 'ARM64')
self.hwinfo.wmi.Query.return_value = [mock.Mock(Architecture='1337')]
self.assertIsNone(self.hwinfo.Architecture())

def testBIOSVersion(self):
self.hwinfo.wmi.Query.return_value = [mock.Mock(SMBIOSBIOSVersion='12345')]
self.assertEqual(self.hwinfo.BIOSVersion(), '12345')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

distutils.core.setup(
name='gwinpy',
version='0.3.7',
version='0.3.8',
packages=['gwinpy', 'gwinpy.net', 'gwinpy.registry', 'gwinpy.wmi'],
license='Apache License',
url='https://github.com/google/winops/',
Expand Down

0 comments on commit eeda7a7

Please sign in to comment.