OLD | NEW |
1 # Copyright (c) 2013 Google Inc. All rights reserved. | 1 # Copyright (c) 2013 Google Inc. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Handle version information related to Visual Stuio.""" | 5 """Handle version information related to Visual Stuio.""" |
6 | 6 |
7 import errno | 7 import errno |
8 import os | 8 import os |
9 import re | 9 import re |
10 import subprocess | 10 import subprocess |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 161 |
162 Arguments: | 162 Arguments: |
163 key: The registry key. | 163 key: The registry key. |
164 value: The particular registry value to read (optional). | 164 value: The particular registry value to read (optional). |
165 Return: | 165 Return: |
166 stdout from reg.exe, or None for failure. | 166 stdout from reg.exe, or None for failure. |
167 """ | 167 """ |
168 text = None | 168 text = None |
169 try: | 169 try: |
170 text = _RegistryQueryBase('Sysnative', key, value) | 170 text = _RegistryQueryBase('Sysnative', key, value) |
171 except OSError, e: | 171 except OSError as e: |
172 if e.errno == errno.ENOENT: | 172 if e.errno == errno.ENOENT: |
173 text = _RegistryQueryBase('System32', key, value) | 173 text = _RegistryQueryBase('System32', key, value) |
174 else: | 174 else: |
175 raise | 175 raise |
176 return text | 176 return text |
177 | 177 |
178 | 178 |
179 def _RegistryGetValueUsingWinReg(key, value): | 179 def _RegistryGetValueUsingWinReg(key, value): |
180 """Use the _winreg module to obtain the value of a registry key. | 180 """Use the _winreg module to obtain the value of a registry key. |
181 | 181 |
182 Args: | 182 Args: |
183 key: The registry key. | 183 key: The registry key. |
184 value: The particular registry value to read. | 184 value: The particular registry value to read. |
185 Return: | 185 Return: |
186 contents of the registry key's value, or None on failure. Throws | 186 contents of the registry key's value, or None on failure. Throws |
187 ImportError if _winreg is unavailable. | 187 ImportError if _winreg is unavailable. |
188 """ | 188 """ |
189 import _winreg | 189 try: |
| 190 import _winreg as winreg |
| 191 except ImportError: |
| 192 import winreg |
190 try: | 193 try: |
191 root, subkey = key.split('\\', 1) | 194 root, subkey = key.split('\\', 1) |
192 assert root == 'HKLM' # Only need HKLM for now. | 195 assert root == 'HKLM' # Only need HKLM for now. |
193 with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey: | 196 with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, subkey) as hkey: |
194 return _winreg.QueryValueEx(hkey, value)[0] | 197 return winreg.QueryValueEx(hkey, value)[0] |
195 except WindowsError: | 198 except WindowsError: |
196 return None | 199 return None |
197 | 200 |
198 | 201 |
199 def _RegistryGetValue(key, value): | 202 def _RegistryGetValue(key, value): |
200 """Use _winreg or reg.exe to obtain the value of a registry key. | 203 """Use _winreg or reg.exe to obtain the value of a registry key. |
201 | 204 |
202 Using _winreg is preferable because it solves an issue on some corporate | 205 Using _winreg is preferable because it solves an issue on some corporate |
203 environments where access to reg.exe is locked down. However, we still need | 206 environments where access to reg.exe is locked down. However, we still need |
204 to fallback to reg.exe for the case where the _winreg module is not available | 207 to fallback to reg.exe for the case where the _winreg module is not available |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) | 447 versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) |
445 if not versions: | 448 if not versions: |
446 if not allow_fallback: | 449 if not allow_fallback: |
447 raise ValueError('Could not locate Visual Studio installation.') | 450 raise ValueError('Could not locate Visual Studio installation.') |
448 if version == 'auto': | 451 if version == 'auto': |
449 # Default to 2005 if we couldn't find anything | 452 # Default to 2005 if we couldn't find anything |
450 return _CreateVersion('2005', None) | 453 return _CreateVersion('2005', None) |
451 else: | 454 else: |
452 return _CreateVersion(version, None) | 455 return _CreateVersion(version, None) |
453 return versions[0] | 456 return versions[0] |
OLD | NEW |