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

Side by Side Diff: gclient_utils.py

Issue 2769011: Some preparation work towards the gclient.py refactor. (Closed)
Patch Set: Created 10 years, 6 months 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
« no previous file with comments | « gclient.py ('k') | 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 2009 Google Inc. All Rights Reserved. 1 # Copyright 2009 Google Inc. All Rights Reserved.
2 # 2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); 3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License. 4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at 5 # You may obtain a copy of the License at
6 # 6 #
7 # http://www.apache.org/licenses/LICENSE-2.0 7 # http://www.apache.org/licenses/LICENSE-2.0
8 # 8 #
9 # Unless required by applicable law or agreed to in writing, software 9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, 10 # distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 24 matching lines...) Expand all
35 self.retcode = retcode 35 self.retcode = retcode
36 self.stdout = stdout 36 self.stdout = stdout
37 self.stderr = stderr 37 self.stderr = stderr
38 38
39 39
40 def CheckCall(command, cwd=None, print_error=True): 40 def CheckCall(command, cwd=None, print_error=True):
41 """Like subprocess.check_call() but returns stdout. 41 """Like subprocess.check_call() but returns stdout.
42 42
43 Works on python 2.4 43 Works on python 2.4
44 """ 44 """
45 logging.debug("%s, cwd=%s" % (str(command), str(cwd))) 45 logging.debug('%s, cwd=%s' % (str(command), str(cwd)))
46 try: 46 try:
47 stderr = None 47 stderr = None
48 if not print_error: 48 if not print_error:
49 stderr = subprocess.PIPE 49 stderr = subprocess.PIPE
50 process = subprocess.Popen(command, cwd=cwd, 50 process = subprocess.Popen(command, cwd=cwd,
51 shell=sys.platform.startswith('win'), 51 shell=sys.platform.startswith('win'),
52 stdout=subprocess.PIPE, 52 stdout=subprocess.PIPE,
53 stderr=stderr) 53 stderr=stderr)
54 std_out, std_err = process.communicate() 54 std_out, std_err = process.communicate()
55 except OSError, e: 55 except OSError, e:
56 raise CheckCallError(command, cwd, e.errno, None) 56 raise CheckCallError(command, cwd, e.errno, None)
57 if process.returncode: 57 if process.returncode:
58 raise CheckCallError(command, cwd, process.returncode, std_out, std_err) 58 raise CheckCallError(command, cwd, process.returncode, std_out, std_err)
59 return std_out, std_err 59 return std_out, std_err
60 60
61 61
62 def SplitUrlRevision(url): 62 def SplitUrlRevision(url):
63 """Splits url and returns a two-tuple: url, rev""" 63 """Splits url and returns a two-tuple: url, rev"""
64 if url.startswith('ssh:'): 64 if url.startswith('ssh:'):
65 # Make sure ssh://test@example.com/test.git@stable works 65 # Make sure ssh://test@example.com/test.git@stable works
66 regex = r"(ssh://(?:[\w]+@)?[-\w:\.]+/[-\w\./]+)(?:@(.+))?" 66 regex = r'(ssh://(?:[\w]+@)?[-\w:\.]+/[-\w\./]+)(?:@(.+))?'
67 components = re.search(regex, url).groups() 67 components = re.search(regex, url).groups()
68 else: 68 else:
69 components = url.split("@") 69 components = url.split('@', 1)
70 if len(components) == 1: 70 if len(components) == 1:
71 components += [None] 71 components += [None]
72 return tuple(components) 72 return tuple(components)
73 73
74 74
75 def ParseXML(output): 75 def ParseXML(output):
76 try: 76 try:
77 return xml.dom.minidom.parseString(output) 77 return xml.dom.minidom.parseString(output)
78 except xml.parsers.expat.ExpatError: 78 except xml.parsers.expat.ExpatError:
79 return None 79 return None
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 Doing so would be hazardous, as it's not a directory slated for removal. 153 Doing so would be hazardous, as it's not a directory slated for removal.
154 In the ordinary case, this is not a problem: for our purposes, the user 154 In the ordinary case, this is not a problem: for our purposes, the user
155 will never lack write permission on *path's parent. 155 will never lack write permission on *path's parent.
156 """ 156 """
157 logging.debug(path) 157 logging.debug(path)
158 file_path = os.path.join(*path) 158 file_path = os.path.join(*path)
159 if not os.path.exists(file_path): 159 if not os.path.exists(file_path):
160 return 160 return
161 161
162 if os.path.islink(file_path) or not os.path.isdir(file_path): 162 if os.path.islink(file_path) or not os.path.isdir(file_path):
163 raise Error("RemoveDirectory asked to remove non-directory %s" % file_path) 163 raise Error('RemoveDirectory asked to remove non-directory %s' % file_path)
164 164
165 has_win32api = False 165 has_win32api = False
166 if sys.platform == 'win32': 166 if sys.platform == 'win32':
167 has_win32api = True 167 has_win32api = True
168 # Some people don't have the APIs installed. In that case we'll do without. 168 # Some people don't have the APIs installed. In that case we'll do without.
169 try: 169 try:
170 win32api = __import__('win32api') 170 win32api = __import__('win32api')
171 win32con = __import__('win32con') 171 win32con = __import__('win32con')
172 except ImportError: 172 except ImportError:
173 has_win32api = False 173 has_win32api = False
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 string argument, and it will be called with each line of the 244 string argument, and it will be called with each line of the
245 subprocess's output. Each line has had the trailing newline character 245 subprocess's output. Each line has had the trailing newline character
246 trimmed. 246 trimmed.
247 247
248 If the command fails, as indicated by a nonzero exit status, gclient will 248 If the command fails, as indicated by a nonzero exit status, gclient will
249 exit with an exit status of fail_status. If fail_status is None (the 249 exit with an exit status of fail_status. If fail_status is None (the
250 default), gclient will raise an Error exception. 250 default), gclient will raise an Error exception.
251 """ 251 """
252 logging.debug(command) 252 logging.debug(command)
253 if print_messages: 253 if print_messages:
254 print("\n________ running \'%s\' in \'%s\'" 254 print('\n________ running \'%s\' in \'%s\''
255 % (' '.join(command), in_directory)) 255 % (' '.join(command), in_directory))
256 256
257 # *Sigh*: Windows needs shell=True, or else it won't search %PATH% for the 257 # *Sigh*: Windows needs shell=True, or else it won't search %PATH% for the
258 # executable, but shell=True makes subprocess on Linux fail when it's called 258 # executable, but shell=True makes subprocess on Linux fail when it's called
259 # with a list because it only tries to execute the first item in the list. 259 # with a list because it only tries to execute the first item in the list.
260 kid = subprocess.Popen(command, bufsize=0, cwd=in_directory, 260 kid = subprocess.Popen(command, bufsize=0, cwd=in_directory,
261 shell=(sys.platform == 'win32'), stdout=subprocess.PIPE, 261 shell=(sys.platform == 'win32'), stdout=subprocess.PIPE,
262 stderr=subprocess.STDOUT) 262 stderr=subprocess.STDOUT)
263 263
264 # Also, we need to forward stdout to prevent weird re-ordering of output. 264 # Also, we need to forward stdout to prevent weird re-ordering of output.
265 # This has to be done on a per byte basis to make sure it is not buffered: 265 # This has to be done on a per byte basis to make sure it is not buffered:
266 # normally buffering is done for each line, but if svn requests input, no 266 # normally buffering is done for each line, but if svn requests input, no
267 # end-of-line character is output after the prompt and it would not show up. 267 # end-of-line character is output after the prompt and it would not show up.
268 in_byte = kid.stdout.read(1) 268 in_byte = kid.stdout.read(1)
269 in_line = "" 269 in_line = ''
270 while in_byte: 270 while in_byte:
271 if in_byte != "\r": 271 if in_byte != '\r':
272 if print_stdout: 272 if print_stdout:
273 if not print_messages: 273 if not print_messages:
274 print("\n________ running \'%s\' in \'%s\'" 274 print('\n________ running \'%s\' in \'%s\''
275 % (' '.join(command), in_directory)) 275 % (' '.join(command), in_directory))
276 print_messages = True 276 print_messages = True
277 sys.stdout.write(in_byte) 277 sys.stdout.write(in_byte)
278 if in_byte != "\n": 278 if in_byte != '\n':
279 in_line += in_byte 279 in_line += in_byte
280 if in_byte == "\n" and filter_fn: 280 if in_byte == '\n' and filter_fn:
281 filter_fn(in_line) 281 filter_fn(in_line)
282 in_line = "" 282 in_line = ''
283 in_byte = kid.stdout.read(1) 283 in_byte = kid.stdout.read(1)
284 rv = kid.wait() 284 rv = kid.wait()
285 285
286 if rv: 286 if rv:
287 msg = "failed to run command: %s" % " ".join(command) 287 msg = 'failed to run command: %s' % ' '.join(command)
288 288
289 if fail_status != None: 289 if fail_status != None:
290 print >>sys.stderr, msg 290 print >>sys.stderr, msg
291 sys.exit(fail_status) 291 sys.exit(fail_status)
292 292
293 raise Error(msg) 293 raise Error(msg)
294 294
295 295
296 def IsUsingGit(root, paths): 296 def IsUsingGit(root, paths):
297 """Returns True if we're using git to manage any of our checkouts. 297 """Returns True if we're using git to manage any of our checkouts.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 return None 339 return None
340 path = new_path 340 path = new_path
341 341
342 342
343 def GetGClientRootAndEntries(path=None): 343 def GetGClientRootAndEntries(path=None):
344 """Returns the gclient root and the dict of entries.""" 344 """Returns the gclient root and the dict of entries."""
345 config_file = '.gclient_entries' 345 config_file = '.gclient_entries'
346 config_path = FindFileUpwards(config_file, path) 346 config_path = FindFileUpwards(config_file, path)
347 347
348 if not config_path: 348 if not config_path:
349 print "Can't find", config_file 349 print 'Can\'t find', config_file
bradn 2010/06/11 17:17:52 Technically the style guide /gpylint allows altern
350 return None 350 return None
351 351
352 env = {} 352 env = {}
353 execfile(config_path, env) 353 execfile(config_path, env)
354 config_dir = os.path.dirname(config_path) 354 config_dir = os.path.dirname(config_path)
355 return config_dir, env['entries'] 355 return config_dir, env['entries']
OLDNEW
« no previous file with comments | « gclient.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698