Skip to content

Instantly share code, notes, and snippets.

@apcj
Created July 1, 2014 14:00
Show Gist options
  • Save apcj/f2ca1803e9e9803117a7 to your computer and use it in GitHub Desktop.
Save apcj/f2ca1803e9e9803117a7 to your computer and use it in GitHub Desktop.
Emit cypher statements to express maven module structure
#!/usr/bin/python
import os
import xml.etree.ElementTree as xml
ns = '{http://maven.apache.org/POM/4.0.0}'
print 'MATCH (n) OPTIONAL MATCH (n) -[r]-> () DELETE n, r;'
def mergeProject(domElement, variableName):
groupElement = domElement.find('./{0}groupId'.format(ns))
groupId = 'org.neo4j' if groupElement is None else groupElement.text
label = 'Project' if groupId == 'org.neo4j' else 'Library'
artifactId = domElement.find('./{0}artifactId'.format(ns)).text
prettyName = artifactId.replace('-', ' ')
print 'MERGE ({0}:{1} {{ groupId: "{2}", artifactId: "{3}", prettyName: "{4}" }})' \
.format(variableName, label, groupId, artifactId, prettyName)
for root, subFolders, files in os.walk('.'):
if 'pom.xml' in files:
pom = xml.parse(os.path.join(root, 'pom.xml'))
for module in pom.findall('./{0}modules/{0}module'.format(ns)):
mergeProject(pom, 'project')
modulePom = xml.parse(os.path.join(root, module.text, 'pom.xml'))
mergeProject(modulePom, 'module')
print 'CREATE (project) -[:HAS_MODULE]-> (module);'
for dependency in pom.findall('./{0}dependencies/{0}dependency'.format(ns)):
mergeProject(pom, 'project')
mergeProject(dependency, 'dependency')
print 'CREATE (project) -[:DEPENDS_ON]-> (dependency);'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment