Created
March 11, 2013 09:49
-
-
Save digulla/5133131 to your computer and use it in GitHub Desktop.
Small Python script to generate site.xml files for a multi-module build. Run this after every change to the project structure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from xml.etree.ElementTree import ElementTree, tostring | |
import os | |
import sys | |
import re | |
template = '''\ | |
<project xmlns="http://maven.apache.org/DECORATION/1.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd"> | |
<publishDate position="bottom" /> | |
<version position="right" /> | |
<skin> | |
<groupId>org.apache.maven.skins</groupId> | |
<artifactId>maven-fluido-skin</artifactId> | |
<version>1.3.0</version> | |
</skin> | |
<custom> | |
<fluidoSkin> | |
<sourceLineNumbersEnabled>true</sourceLineNumbersEnabled> | |
</fluidoSkin> | |
</custom> | |
<body> | |
<links> | |
<item name="CI Server" href="${{project.ciManagement.url}}" /> | |
<item name="Hudson" href="https://hudson.dev.java.net/" /> | |
<item name="Maven 2" href="http://maven.apache.org/index.html" /> | |
</links> | |
<breadcrumbs> | |
<item name="CI Server" href="${{project.ciManagement.url}}" /> | |
<item name="{breadcrumb}" href="${{web-root}}/{path}" /> | |
</breadcrumbs> | |
{projectMenu} | |
<menu ref="modules" /> | |
<menu ref="reports" /> | |
</body> | |
</project> | |
''' | |
URL_PATTERN = re.compile(r'(http://[-a-zA-Z0-9_/.]+)') | |
def text(elem): | |
if elem is None: return u'' | |
s = unicode(elem.findtext('.')) | |
s = URL_PATTERN.sub(u'{{\\1}}', s) | |
print s | |
return s | |
class POM(object): | |
def __init__(self, parent, path): | |
self.parent = parent | |
self.path = path | |
self._modules = None | |
self.load() | |
def load(self): | |
fileName = os.path.join(self.path, 'pom.xml') | |
dom = ElementTree() | |
dom.parse(fileName) | |
self.dom = dom | |
def modules(self): | |
if self._modules is None: | |
self.loadModules() | |
return self._modules | |
def loadModules(self): | |
modulesElem = self.dom.find('/{http://maven.apache.org/POM/4.0.0}modules') | |
result = [] | |
if modulesElem: | |
for module in modulesElem.getiterator('{http://maven.apache.org/POM/4.0.0}module'): | |
modPath = os.path.join(self.path, module.text) | |
print modPath | |
pom = POM(self, modPath) | |
result.append(pom) | |
self._modules = result | |
def name(self): | |
elem = self.dom.find('/{http://maven.apache.org/POM/4.0.0}name') | |
return text(elem) | |
def desc(self): | |
elem = self.dom.find('//{http://maven.apache.org/POM/4.0.0}description') | |
#print self.name(),elem,text(elem) | |
#print tostring(elem) | |
return text(elem) | |
root = POM(None, '.') | |
print root.name() | |
for mod in root.modules(): | |
print mod.name() | |
projectMenu = [ | |
' <menu name="Projects">', | |
' <item name="%s" href="${web-root}/" collapse="false">' % root.name() | |
] | |
def collectItems(pom, level): | |
global projectMenu | |
d = {} | |
names = [] | |
for module in pom.modules(): | |
names.append(module.name()) | |
d[module.name()] = module | |
names.sort() | |
for name in names: | |
module = d[name] | |
projectMenu.append('%s<item name="%s" href="${web-root}/%s" collapse="false">' % ( | |
' ' * level, | |
module.name(), | |
module.path[2:] | |
)) | |
collectItems(module, level+1) | |
projectMenu.append('%s</item>' % (' ' * level, )) | |
collectItems(root, 3) | |
projectMenu.append(' </item>') | |
projectMenu.append(' </menu>') | |
projectMenu = '\n'.join(projectMenu) | |
def writeSiteXml(pom): | |
dir = os.path.join(pom.path, 'src/site') | |
if not os.path.exists(dir): | |
os.makedirs(dir) | |
fileName = os.path.join(dir, 'site.xml') | |
print fileName | |
path = pom.path | |
if path.startswith('.'): | |
path = path[1:] | |
if path.startswith('/'): | |
path = path[1:] | |
args = { | |
'projectMenu': projectMenu, | |
'breadcrumb': pom.name(), | |
'path': path | |
} | |
s = template.format(**args) | |
with open(fileName, 'w') as fh: | |
fh.write(s) | |
for module in pom.modules(): | |
writeSiteXml(module) | |
writeSiteXml(root) | |
def writeProjectList(fh, pom, prefix): | |
for module in pom.modules(): | |
fh.write('%s* {{{${web-root}/%s}%s}} - %s\n\n' % ( | |
prefix, module.path[2:], module.name(), module.desc() | |
)) | |
writeProjectList(fh, module, prefix+' ') | |
with open('src/site/apt/index.apt.vm', 'w') as fh: | |
fh.write('''Welcome to XXX. | |
''') | |
writeProjectList(fh, root, ' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment