Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: pylib/gyp/MSVSVersion.py

Issue 724863002: win: Use the _winreg module for registry access (Closed) Base URL: http://gyp.googlecode.com/svn/trunk
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 for failure. Throws
176 ImportError if _winreg is unavailable.
177 """
178 import _winreg
179 try:
180 root,subkey = key.split('\\', 1)
scottmg 2014/11/13 18:05:35 nit; space after ,
181 assert('HKLM' == root) # Only need HKLM for now
scottmg 2014/11/13 18:05:35 nits; - no () - root == 'HKLM', rather than yoda-s
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.
scottmg 2014/11/13 18:05:35 add a comment explaining why we need to try both w
190
191 Args:
192 key: The registry key.
173 value: The particular registry value to read. 193 value: The particular registry value to read.
174 Return: 194 Return:
175 contents of the registry key's value, or None on failure. 195 contents of the registry key's value, or None on failure.
176 """ 196 """
197 try:
198 return _RegistryGetValueUsingWinReg(key, value)
199 except ImportError:
200 pass
201
202 # Fallback to reg.exe if we fail to import _winreg.
177 text = _RegistryQuery(key, value) 203 text = _RegistryQuery(key, value)
178 if not text: 204 if not text:
179 return None 205 return None
180 # Extract value. 206 # Extract value.
181 match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text) 207 match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text)
182 if not match: 208 if not match:
183 return None 209 return None
184 return match.group(1) 210 return match.group(1)
185 211
186 212
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): 213 def _CreateVersion(name, path, sdk_based=False):
201 """Sets up MSVS project generation. 214 """Sets up MSVS project generation.
202 215
203 Setup is based off the GYP_MSVS_VERSION environment variable or whatever is 216 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 217 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. 218 passed in that doesn't match a value in versions python will throw a error.
206 """ 219 """
207 if path: 220 if path:
208 path = os.path.normpath(path) 221 path = os.path.normpath(path)
209 versions = { 222 versions = {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) 415 versions = _DetectVisualStudioVersions(version_map[version], 'e' in version)
403 if not versions: 416 if not versions:
404 if not allow_fallback: 417 if not allow_fallback:
405 raise ValueError('Could not locate Visual Studio installation.') 418 raise ValueError('Could not locate Visual Studio installation.')
406 if version == 'auto': 419 if version == 'auto':
407 # Default to 2005 if we couldn't find anything 420 # Default to 2005 if we couldn't find anything
408 return _CreateVersion('2005', None) 421 return _CreateVersion('2005', None)
409 else: 422 else:
410 return _CreateVersion(version, None) 423 return _CreateVersion(version, None)
411 return versions[0] 424 return versions[0]
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698