Skip to content

Instantly share code, notes, and snippets.

@zerok
Created February 14, 2010 15:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zerok/304083 to your computer and use it in GitHub Desktop.
Save zerok/304083 to your computer and use it in GitHub Desktop.
"""
Fetches an umlgraph from a github wikipage and converts it into a
png using graphviz and UMLGraph.
The script searches on a given page for a pre+code compbination with the class
"uml", which is then converted into a png file.
Requirements:
* BeautifulSoup
* graphviz
* UMLGraph <http://www.umlgraph.org/>
* java
"""
from __future__ import with_statement
import sys, shutil, urllib2, tempfile, subprocess, re, optparse, os
from os.path import dirname, join
from BeautifulSoup import BeautifulStoneSoup as BeautifulSoup
parser = optparse.OptionParser()
parser.usage = "%prog [OPTIONS] SRC TARGET"
parser.add_option('--umlgraph-path', dest='umlgraph', default=None, help="Path to UMLGraph's .jar file")
parser.add_option('--java-path', dest='java', default='java')
opts, args = parser.parse_args()
if opts.umlgraph is None:
raise RuntimeError, "No UMLGraph path given"
url = args[0]
target = args[1]
tools_jar = join(os.environ['JAVA_HOME'], 'lib', 'tools.jar')
data = urllib2.urlopen(url).read()
data = data.replace('value="Search",', '') # Fix a minor markup issue on github
soup = BeautifulSoup(data)
uml = None
try:
uml = u''.join(soup.find('pre', attrs={'class': re.compile('uml')}).find('code').contents)
except AttributeError, e:
print "Couldn't find any UML data on the given page"
sys.exit(1)
dir_ = tempfile.mkdtemp()
try:
uml_file = join(dir_, 'uml.java')
dot_file = join(dir_, 'uml.dot')
png_file = join(dir_, 'uml.png')
with open(uml_file, 'w+') as fp:
fp.write(uml)
subprocess.call([opts.java, "-classpath", ':'.join([opts.umlgraph, tools_jar]), "org.umlgraph.doclet.UmlGraph", "-quiet", "-output", dot_file, "-package", "uml", uml_file])
subprocess.call(['dot', '-Tpng', '-o'+png_file, dot_file])
shutil.copy(png_file, target)
finally:
shutil.rmtree(dir_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment