TestWin.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2014 Google Inc. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """
  5. TestWin.py: a collection of helpers for testing on Windows.
  6. """
  7. import errno
  8. import os
  9. import re
  10. import sys
  11. import subprocess
  12. class Registry(object):
  13. def _QueryBase(self, sysdir, key, value):
  14. """Use reg.exe to read a particular key.
  15. While ideally we might use the win32 module, we would like gyp to be
  16. python neutral, so for instance cygwin python lacks this module.
  17. Arguments:
  18. sysdir: The system subdirectory to attempt to launch reg.exe from.
  19. key: The registry key to read from.
  20. value: The particular value to read.
  21. Return:
  22. stdout from reg.exe, or None for failure.
  23. """
  24. # Skip if not on Windows or Python Win32 setup issue
  25. if sys.platform not in ('win32', 'cygwin'):
  26. return None
  27. # Setup params to pass to and attempt to launch reg.exe
  28. cmd = [os.path.join(os.environ.get('WINDIR', ''), sysdir, 'reg.exe'),
  29. 'query', key]
  30. if value:
  31. cmd.extend(['/v', value])
  32. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  33. # Get the stdout from reg.exe, reading to the end so p.returncode is valid
  34. # Note that the error text may be in [1] in some cases
  35. text = p.communicate()[0]
  36. # Check return code from reg.exe; officially 0==success and 1==error
  37. if p.returncode:
  38. return None
  39. return text
  40. def Query(self, key, value=None):
  41. r"""Use reg.exe to read a particular key through _QueryBase.
  42. First tries to launch from %WinDir%\Sysnative to avoid WoW64 redirection. If
  43. that fails, it falls back to System32. Sysnative is available on Vista and
  44. up and available on Windows Server 2003 and XP through KB patch 942589. Note
  45. that Sysnative will always fail if using 64-bit python due to it being a
  46. virtual directory and System32 will work correctly in the first place.
  47. KB 942589 - http://support.microsoft.com/kb/942589/en-us.
  48. Arguments:
  49. key: The registry key.
  50. value: The particular registry value to read (optional).
  51. Return:
  52. stdout from reg.exe, or None for failure.
  53. """
  54. text = None
  55. try:
  56. text = self._QueryBase('Sysnative', key, value)
  57. except OSError, e:
  58. if e.errno == errno.ENOENT:
  59. text = self._QueryBase('System32', key, value)
  60. else:
  61. raise
  62. return text
  63. def GetValue(self, key, value):
  64. """Use reg.exe to obtain the value of a registry key.
  65. Args:
  66. key: The registry key.
  67. value: The particular registry value to read.
  68. Return:
  69. contents of the registry key's value, or None on failure.
  70. """
  71. text = self.Query(key, value)
  72. if not text:
  73. return None
  74. # Extract value.
  75. match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text)
  76. if not match:
  77. return None
  78. return match.group(1)
  79. def KeyExists(self, key):
  80. """Use reg.exe to see if a key exists.
  81. Args:
  82. key: The registry key to check.
  83. Return:
  84. True if the key exists
  85. """
  86. if not self.Query(key):
  87. return False
  88. return True