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