| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 try: | 158 try: |
| 159 text = _RegistryQueryBase('Sysnative', key, value) | 159 text = _RegistryQueryBase('Sysnative', key, value) |
| 160 except OSError, e: | 160 except OSError, e: |
| 161 if e.errno == errno.ENOENT: | 161 if e.errno == errno.ENOENT: |
| 162 text = _RegistryQueryBase('System32', key, value) | 162 text = _RegistryQueryBase('System32', key, value) |
| 163 else: | 163 else: |
| 164 raise | 164 raise |
| 165 return text | 165 return text |
| 166 | 166 |
| 167 | 167 |
| 168 def _RegistryGetValue(key, value): | 168 def _RegistryGetValueUsingWinReg(key, value): |
| 169 """Use reg.exe to obtain the value of a registry key. | 169 """Use the _winreg module to obtain the value of a registry key. |
| 170 | 170 |
| 171 Args: | 171 Args: |
| 172 key: The registry key. | 172 key: The registry key. |
| 173 value: The particular registry value to read. |
| 174 Return: |
| 175 contents of the registry key's value, or None on failure. Throws |
| 176 ImportError if _winreg is unavailable. |
| 177 """ |
| 178 import _winreg |
| 179 try: |
| 180 root, subkey = key.split('\\', 1) |
| 181 assert root == 'HKLM' # Only need HKLM for now. |
| 182 with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey: |
| 183 return _winreg.QueryValueEx(hkey, value)[0] |
| 184 except WindowsError: |
| 185 return None |
| 186 |
| 187 |
| 188 def _RegistryGetValue(key, value): |
| 189 """Use _winreg or reg.exe to obtain the value of a registry key. |
| 190 |
| 191 Using _winreg is preferable because it solves an issue on some corporate |
| 192 environments where access to reg.exe is locked down. However, we still need |
| 193 to fallback to reg.exe for the case where the _winreg module is not available |
| 194 (for example in cygwin python). |
| 195 |
| 196 Args: |
| 197 key: The registry key. |
| 173 value: The particular registry value to read. | 198 value: The particular registry value to read. |
| 174 Return: | 199 Return: |
| 175 contents of the registry key's value, or None on failure. | 200 contents of the registry key's value, or None on failure. |
| 176 """ | 201 """ |
| 202 try: |
| 203 return _RegistryGetValueUsingWinReg(key, value) |
| 204 except ImportError: |
| 205 pass |
| 206 |
| 207 # Fallback to reg.exe if we fail to import _winreg. |
| 177 text = _RegistryQuery(key, value) | 208 text = _RegistryQuery(key, value) |
| 178 if not text: | 209 if not text: |
| 179 return None | 210 return None |
| 180 # Extract value. | 211 # Extract value. |
| 181 match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text) | 212 match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text) |
| 182 if not match: | 213 if not match: |
| 183 return None | 214 return None |
| 184 return match.group(1) | 215 return match.group(1) |
| 185 | 216 |
| 186 | 217 |
| 187 def _RegistryKeyExists(key): | |
| 188 """Use reg.exe to see if a key exists. | |
| 189 | |
| 190 Args: | |
| 191 key: The registry key to check. | |
| 192 Return: | |
| 193 True if the key exists | |
| 194 """ | |
| 195 if not _RegistryQuery(key): | |
| 196 return False | |
| 197 return True | |
| 198 | |
| 199 | |
| 200 def _CreateVersion(name, path, sdk_based=False): | 218 def _CreateVersion(name, path, sdk_based=False): |
| 201 """Sets up MSVS project generation. | 219 """Sets up MSVS project generation. |
| 202 | 220 |
| 203 Setup is based off the GYP_MSVS_VERSION environment variable or whatever is | 221 Setup is based off the GYP_MSVS_VERSION environment variable or whatever is |
| 204 autodetected if GYP_MSVS_VERSION is not explicitly specified. If a version is | 222 autodetected if GYP_MSVS_VERSION is not explicitly specified. If a version is |
| 205 passed in that doesn't match a value in versions python will throw a error. | 223 passed in that doesn't match a value in versions python will throw a error. |
| 206 """ | 224 """ |
| 207 if path: | 225 if path: |
| 208 path = os.path.normpath(path) | 226 path = os.path.normpath(path) |
| 209 versions = { | 227 versions = { |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) | 420 versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) |
| 403 if not versions: | 421 if not versions: |
| 404 if not allow_fallback: | 422 if not allow_fallback: |
| 405 raise ValueError('Could not locate Visual Studio installation.') | 423 raise ValueError('Could not locate Visual Studio installation.') |
| 406 if version == 'auto': | 424 if version == 'auto': |
| 407 # Default to 2005 if we couldn't find anything | 425 # Default to 2005 if we couldn't find anything |
| 408 return _CreateVersion('2005', None) | 426 return _CreateVersion('2005', None) |
| 409 else: | 427 else: |
| 410 return _CreateVersion(version, None) | 428 return _CreateVersion(version, None) |
| 411 return versions[0] | 429 return versions[0] |
| OLD | NEW |