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

Side by Side Diff: gclient.py

Issue 115040: Slowly start moving XML parsing function in gcl to glcient. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools/
Patch Set: Created 11 years, 7 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 | 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 #!/usr/bin/python 1 #!/usr/bin/python
2 # 2 #
3 # Copyright 2008 Google Inc. All Rights Reserved. 3 # Copyright 2008 Google Inc. All Rights Reserved.
4 # 4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License. 6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at 7 # You may obtain a copy of the License at
8 # 8 #
9 # http://www.apache.org/licenses/LICENSE-2.0 9 # http://www.apache.org/licenses/LICENSE-2.0
10 # 10 #
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 #\"data/really_large_component\": None, 251 #\"data/really_large_component\": None,
252 }, 252 },
253 \"safesync_url\": \"%s\" 253 \"safesync_url\": \"%s\"
254 } 254 }
255 ] 255 ]
256 """) 256 """)
257 257
258 258
259 ## Generic utils 259 ## Generic utils
260 260
261
262 def getText(nodelist):
263 """
264 Return the concatenated text for the children of a list of DOM nodes.
265 """
266 rc = []
267 for node in nodelist:
268 if node.nodeType == node.TEXT_NODE:
269 rc.append(node.data)
270 else:
271 rc.append(getText(node.childNodes))
272 return ''.join(rc)
273
274
275 def ParseXML(output): 261 def ParseXML(output):
276 try: 262 try:
277 return xml.dom.minidom.parseString(output) 263 return xml.dom.minidom.parseString(output)
278 except xml.parsers.expat.ExpatError: 264 except xml.parsers.expat.ExpatError:
279 return None 265 return None
280 266
281 267
268 def GetNamedNodeText(node, node_name):
269 child_nodes = node.getElementsByTagName(node_name)
270 if not child_nodes:
271 return None
272 assert len(child_nodes) == 1 and child_nodes[0].childNodes.length == 1
273 return child_nodes[0].firstChild.nodeValue
274
275
276 def GetNodeNamedAttributeText(node, node_name, attribute_name):
277 child_nodes = node.getElementsByTagName(node_name)
278 if not child_nodes:
279 return None
280 assert len(child_nodes) == 1
281 return child_nodes[0].getAttribute(attribute_name)
282
283
282 class Error(Exception): 284 class Error(Exception):
283 """gclient exception class.""" 285 """gclient exception class."""
284 pass 286 pass
285 287
286 class PrintableObject(object): 288 class PrintableObject(object):
287 def __str__(self): 289 def __str__(self):
288 output = '' 290 output = ''
289 for i in dir(self): 291 for i in dir(self):
290 if i.startswith('__'): 292 if i.startswith('__'):
291 continue 293 continue
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 """Runs 'svn info' on an existing path. 560 """Runs 'svn info' on an existing path.
559 561
560 Args: 562 Args:
561 relpath: The directory where the working copy resides relative to 563 relpath: The directory where the working copy resides relative to
562 the directory given by in_directory. 564 the directory given by in_directory.
563 in_directory: The directory where svn is to be run. 565 in_directory: The directory where svn is to be run.
564 566
565 Returns: 567 Returns:
566 An object with fields corresponding to the output of 'svn info' 568 An object with fields corresponding to the output of 'svn info'
567 """ 569 """
568 info = CaptureSVN(options, ["info", "--xml", relpath], in_directory) 570 dom = ParseXML(CaptureSVN(options, ["info", "--xml", relpath], in_directory))
569 dom = xml.dom.minidom.parseString(info)
570
571 # str() the getText() results because they may be returned as
572 # Unicode, which interferes with the higher layers matching up
573 # things in the deps dictionary.
574 result = PrintableObject() 571 result = PrintableObject()
575 result.root = str(getText(dom.getElementsByTagName('root'))) 572 if dom:
576 result.url = str(getText(dom.getElementsByTagName('url'))) 573 # /info/entry/
577 result.uuid = str(getText(dom.getElementsByTagName('uuid'))) 574 # url
578 result.revision = int(dom.getElementsByTagName('entry')[0].getAttribute( 575 # reposityory/(root|uuid)
579 'revision')) 576 # wc-info/(schedule|depth)
577 # commit/(author|date)
578 # str() the results because they may be returned as Unicode, which
579 # interferes with the higher layers matching up things in the deps
580 # dictionary.
581 result = PrintableObject()
582 result.root = str(GetNamedNodeText(dom, 'root'))
583 result.url = str(GetNamedNodeText(dom, 'url'))
584 result.uuid = str(GetNamedNodeText(dom, 'uuid'))
585 result.revision = int(GetNodeNamedAttributeText(dom, 'entry', 'revision'))
580 return result 586 return result
581 587
582 588
583 def CaptureSVNHeadRevision(options, url): 589 def CaptureSVNHeadRevision(options, url):
584 """Get the head revision of a SVN repository. 590 """Get the head revision of a SVN repository.
585 591
586 Returns: 592 Returns:
587 Int head revision 593 Int head revision
588 """ 594 """
589 info = CaptureSVN(options, ["info", "--xml", url], os.getcwd()) 595 info = CaptureSVN(options, ["info", "--xml", url], os.getcwd())
(...skipping 1106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1696 1702
1697 if "__main__" == __name__: 1703 if "__main__" == __name__:
1698 try: 1704 try:
1699 result = Main(sys.argv) 1705 result = Main(sys.argv)
1700 except Error, e: 1706 except Error, e:
1701 print "Error: %s" % str(e) 1707 print "Error: %s" % str(e)
1702 result = 1 1708 result = 1
1703 sys.exit(result) 1709 sys.exit(result)
1704 1710
1705 # vim: ts=2:sw=2:tw=80:et: 1711 # vim: ts=2:sw=2:tw=80:et:
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