comparison mxtool/mx.py @ 5217:70777e50f1e6

replace monkey patch with subclassing instead
author Doug Simon <doug.simon@oracle.com>
date Sun, 08 Apr 2012 00:09:10 +0200
parents 887b45f6aa02
children ddccd4abdb09
comparison
equal deleted inserted replaced
5198:887b45f6aa02 5217:70777e50f1e6
399 existing = _libs.get(l.name) 399 existing = _libs.get(l.name)
400 if existing is not None: 400 if existing is not None:
401 abort('cannot redefine library ' + l.name) 401 abort('cannot redefine library ' + l.name)
402 _libs[l.name] = l 402 _libs[l.name] = l
403 403
404 404 class XMLElement(xml.dom.minidom.Element):
405 class XML(xml.dom.minidom.Document): 405 def writexml(self, writer, indent="", addindent="", newl=""):
406 writer.write(indent+"<" + self.tagName)
407
408 attrs = self._get_attributes()
409 a_names = attrs.keys()
410 a_names.sort()
411
412 for a_name in a_names:
413 writer.write(" %s=\"" % a_name)
414 xml.dom.minidom._write_data(writer, attrs[a_name].value)
415 writer.write("\"")
416 if self.childNodes:
417 if not self.ownerDocument.padTextNodeWithoutSiblings and len(self.childNodes) == 1 and isinstance(self.childNodes[0], xml.dom.minidom.Text):
418 # if the only child of an Element node is a Text node, then the
419 # text is printed without any indentation or new line padding
420 writer.write(">")
421 self.childNodes[0].writexml(writer)
422 writer.write("</%s>%s" % (self.tagName,newl))
423 else:
424 writer.write(">%s"%(newl))
425 for node in self.childNodes:
426 node.writexml(writer,indent+addindent,addindent,newl)
427 writer.write("%s</%s>%s" % (indent,self.tagName,newl))
428 else:
429 writer.write("/>%s"%(newl))
430
431 class XMLDoc(xml.dom.minidom.Document):
406 432
407 def __init__(self): 433 def __init__(self):
408 xml.dom.minidom.Document.__init__(self) 434 xml.dom.minidom.Document.__init__(self)
409 self.current = self 435 self.current = self
410 self.padTextNodeWithoutSiblings = False 436 self.padTextNodeWithoutSiblings = False
411 437
438 def createElement(self, tagName):
439 # overwritten to create XMLElement
440 e = XMLElement(tagName)
441 e.ownerDocument = self
442 return e
443
412 def open(self, tag, attributes={}, data=None): 444 def open(self, tag, attributes={}, data=None):
413 element = self.createElement(tag) 445 element = self.createElement(tag)
414 for key, value in attributes.items(): 446 for key, value in attributes.items():
415 element.setAttribute(key, value) 447 element.setAttribute(key, value)
416 self.current.appendChild(element) 448 self.current.appendChild(element)
434 if escape: 466 if escape:
435 entities = { '"': "&quot;", "'": "&apos;", '\n': '&#10;' } 467 entities = { '"': "&quot;", "'": "&apos;", '\n': '&#10;' }
436 result = xml.sax.saxutils.escape(result, entities) 468 result = xml.sax.saxutils.escape(result, entities)
437 return result 469 return result
438 470
439 @staticmethod
440 def _Element_writexml(self, writer, indent="", addindent="", newl=""):
441 # Monkey patch: if the only child of an Element node is a Text node, then the
442 # text is printed without any indentation or new line padding
443 writer.write(indent+"<" + self.tagName)
444
445 attrs = self._get_attributes()
446 a_names = attrs.keys()
447 a_names.sort()
448
449 for a_name in a_names:
450 writer.write(" %s=\"" % a_name)
451 xml.dom.minidom._write_data(writer, attrs[a_name].value)
452 writer.write("\"")
453 if self.childNodes:
454 if not self.ownerDocument.padTextNodeWithoutSiblings and len(self.childNodes) == 1 and isinstance(self.childNodes[0], xml.dom.minidom.Text):
455 writer.write(">")
456 self.childNodes[0].writexml(writer)
457 writer.write("</%s>%s" % (self.tagName,newl))
458 else:
459 writer.write(">%s"%(newl))
460 for node in self.childNodes:
461 node.writexml(writer,indent+addindent,addindent,newl)
462 writer.write("%s</%s>%s" % (indent,self.tagName,newl))
463 else:
464 writer.write("/>%s"%(newl))
465
466 xml.dom.minidom.Element.writexml = XML._Element_writexml
467
468 def get_os(): 471 def get_os():
469 """ 472 """
470 Get a canonical form of sys.platform. 473 Get a canonical form of sys.platform.
471 """ 474 """
472 if sys.platform.startswith('darwin'): 475 if sys.platform.startswith('darwin'):
1438 print '"' + p.name + '"->"' + dep + '"' 1441 print '"' + p.name + '"->"' + dep + '"'
1439 print '}' 1442 print '}'
1440 1443
1441 1444
1442 def _source_locator_memento(deps): 1445 def _source_locator_memento(deps):
1443 slm = XML() 1446 slm = XMLDoc()
1444 slm.open('sourceLookupDirector') 1447 slm.open('sourceLookupDirector')
1445 slm.open('sourceContainers', {'duplicates' : 'false'}) 1448 slm.open('sourceContainers', {'duplicates' : 'false'})
1446 1449
1447 for dep in deps: 1450 for dep in deps:
1448 if dep.isLibrary(): 1451 if dep.isLibrary():
1449 if hasattr(dep, 'eclipse.container'): 1452 if hasattr(dep, 'eclipse.container'):
1450 memento = XML().element('classpathContainer', {'path' : getattr(dep, 'eclipse.container')}).xml() 1453 memento = XMLDoc().element('classpathContainer', {'path' : getattr(dep, 'eclipse.container')}).xml()
1451 slm.element('classpathContainer', {'memento' : memento, 'typeId':'org.eclipse.jdt.launching.sourceContainer.classpathContainer'}) 1454 slm.element('classpathContainer', {'memento' : memento, 'typeId':'org.eclipse.jdt.launching.sourceContainer.classpathContainer'})
1452 else: 1455 else:
1453 memento = XML().element('javaProject', {'name' : dep.name}).xml() 1456 memento = XMLDoc().element('javaProject', {'name' : dep.name}).xml()
1454 slm.element('container', {'memento' : memento, 'typeId':'org.eclipse.jdt.launching.sourceContainer.javaProject'}) 1457 slm.element('container', {'memento' : memento, 'typeId':'org.eclipse.jdt.launching.sourceContainer.javaProject'})
1455 1458
1456 slm.close('sourceContainers') 1459 slm.close('sourceContainers')
1457 slm.close('sourceLookupDirector') 1460 slm.close('sourceLookupDirector')
1458 return slm 1461 return slm
1460 def make_eclipse_attach(hostname, port, name=None, deps=[]): 1463 def make_eclipse_attach(hostname, port, name=None, deps=[]):
1461 """ 1464 """
1462 Creates an Eclipse launch configuration file for attaching to a Java process. 1465 Creates an Eclipse launch configuration file for attaching to a Java process.
1463 """ 1466 """
1464 slm = _source_locator_memento(deps) 1467 slm = _source_locator_memento(deps)
1465 launch = XML() 1468 launch = XMLDoc()
1466 launch.open('launchConfiguration', {'type' : 'org.eclipse.jdt.launching.remoteJavaApplication'}) 1469 launch.open('launchConfiguration', {'type' : 'org.eclipse.jdt.launching.remoteJavaApplication'})
1467 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_id', 'value' : 'org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector'}) 1470 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_id', 'value' : 'org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector'})
1468 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_memento', 'value' : '%s'}) 1471 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_memento', 'value' : '%s'})
1469 launch.element('booleanAttribute', {'key' : 'org.eclipse.jdt.launching.ALLOW_TERMINATE', 'value' : 'true'}) 1472 launch.element('booleanAttribute', {'key' : 'org.eclipse.jdt.launching.ALLOW_TERMINATE', 'value' : 'true'})
1470 launch.open('mapAttribute', {'key' : 'org.eclipse.jdt.launching.CONNECT_MAP'}) 1473 launch.open('mapAttribute', {'key' : 'org.eclipse.jdt.launching.CONNECT_MAP'})
1529 deps += [p for p in s.projects if e == p.output_dir()] 1532 deps += [p for p in s.projects if e == p.output_dir()]
1530 deps += [l for l in s.libs if e == l.get_path(False)] 1533 deps += [l for l in s.libs if e == l.get_path(False)]
1531 1534
1532 slm = _source_locator_memento(deps) 1535 slm = _source_locator_memento(deps)
1533 1536
1534 launch = XML() 1537 launch = XMLDoc()
1535 launch.open('launchConfiguration', {'type' : 'org.eclipse.jdt.launching.localJavaApplication'}) 1538 launch.open('launchConfiguration', {'type' : 'org.eclipse.jdt.launching.localJavaApplication'})
1536 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_id', 'value' : 'org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector'}) 1539 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_id', 'value' : 'org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector'})
1537 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_memento', 'value' : '%s'}) 1540 launch.element('stringAttribute', {'key' : 'org.eclipse.debug.core.source_locator_memento', 'value' : '%s'})
1538 launch.element('stringAttribute', {'key' : 'org.eclipse.jdt.launching.JRE_CONTAINER', 'value' : 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/' + jre}) 1541 launch.element('stringAttribute', {'key' : 'org.eclipse.jdt.launching.JRE_CONTAINER', 'value' : 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/' + jre})
1539 launch.element('stringAttribute', {'key' : 'org.eclipse.jdt.launching.MAIN_TYPE', 'value' : mainClass}) 1542 launch.element('stringAttribute', {'key' : 'org.eclipse.jdt.launching.MAIN_TYPE', 'value' : mainClass})
1559 continue 1562 continue
1560 1563
1561 if not exists(p.dir): 1564 if not exists(p.dir):
1562 os.makedirs(p.dir) 1565 os.makedirs(p.dir)
1563 1566
1564 out = XML() 1567 out = XMLDoc()
1565 out.open('classpath') 1568 out.open('classpath')
1566 1569
1567 for src in p.srcDirs: 1570 for src in p.srcDirs:
1568 srcDir = join(p.dir, src) 1571 srcDir = join(p.dir, src)
1569 if not exists(srcDir): 1572 if not exists(srcDir):
1600 out.close('classpath') 1603 out.close('classpath')
1601 update_file(join(p.dir, '.classpath'), out.xml(indent='\t', newl='\n')) 1604 update_file(join(p.dir, '.classpath'), out.xml(indent='\t', newl='\n'))
1602 1605
1603 csConfig = join(project(p.checkstyleProj).dir, '.checkstyle_checks.xml') 1606 csConfig = join(project(p.checkstyleProj).dir, '.checkstyle_checks.xml')
1604 if exists(csConfig): 1607 if exists(csConfig):
1605 out = XML() 1608 out = XMLDoc()
1606 1609
1607 dotCheckstyle = join(p.dir, ".checkstyle") 1610 dotCheckstyle = join(p.dir, ".checkstyle")
1608 checkstyleConfigPath = '/' + p.checkstyleProj + '/.checkstyle_checks.xml' 1611 checkstyleConfigPath = '/' + p.checkstyleProj + '/.checkstyle_checks.xml'
1609 out.open('fileset-config', {'file-format-version' : '1.2.0', 'simple-config' : 'true'}) 1612 out.open('fileset-config', {'file-format-version' : '1.2.0', 'simple-config' : 'true'})
1610 out.open('local-check-config', {'name' : 'Checks', 'location' : checkstyleConfigPath, 'type' : 'project', 'description' : ''}) 1613 out.open('local-check-config', {'name' : 'Checks', 'location' : checkstyleConfigPath, 'type' : 'project', 'description' : ''})
1630 out.close('filter') 1633 out.close('filter')
1631 1634
1632 out.close('fileset-config') 1635 out.close('fileset-config')
1633 update_file(dotCheckstyle, out.xml(indent='\t', newl='\n')) 1636 update_file(dotCheckstyle, out.xml(indent='\t', newl='\n'))
1634 1637
1635 out = XML() 1638 out = XMLDoc()
1636 out.open('projectDescription') 1639 out.open('projectDescription')
1637 out.element('name', data=p.name) 1640 out.element('name', data=p.name)
1638 out.element('comment', data='') 1641 out.element('comment', data='')
1639 out.element('projects', data='') 1642 out.element('projects', data='')
1640 out.open('buildSpec') 1643 out.open('buildSpec')
1684 continue 1687 continue
1685 1688
1686 if not exists(join(p.dir, 'nbproject')): 1689 if not exists(join(p.dir, 'nbproject')):
1687 os.makedirs(join(p.dir, 'nbproject')) 1690 os.makedirs(join(p.dir, 'nbproject'))
1688 1691
1689 out = XML() 1692 out = XMLDoc()
1690 out.open('project', {'name' : p.name, 'default' : 'default', 'basedir' : '.'}) 1693 out.open('project', {'name' : p.name, 'default' : 'default', 'basedir' : '.'})
1691 out.element('description', data='Builds, tests, and runs the project ' + p.name + '.') 1694 out.element('description', data='Builds, tests, and runs the project ' + p.name + '.')
1692 out.element('import', {'file' : 'nbproject/build-impl.xml'}) 1695 out.element('import', {'file' : 'nbproject/build-impl.xml'})
1693 out.close('project') 1696 out.close('project')
1694 updated = update_file(join(p.dir, 'build.xml'), out.xml(indent='\t', newl='\n')) or updated 1697 updated = update_file(join(p.dir, 'build.xml'), out.xml(indent='\t', newl='\n')) or updated
1695 1698
1696 out = XML() 1699 out = XMLDoc()
1697 out.open('project', {'xmlns' : 'http://www.netbeans.org/ns/project/1'}) 1700 out.open('project', {'xmlns' : 'http://www.netbeans.org/ns/project/1'})
1698 out.element('type', data='org.netbeans.modules.java.j2seproject') 1701 out.element('type', data='org.netbeans.modules.java.j2seproject')
1699 out.open('configuration') 1702 out.open('configuration')
1700 out.open('data', {'xmlns' : 'http://www.netbeans.org/ns/j2se-project/3'}) 1703 out.open('data', {'xmlns' : 'http://www.netbeans.org/ns/j2se-project/3'})
1701 out.element('name', data=p.name) 1704 out.element('name', data=p.name)