compileplanter.py from gzz at Krugle
Show compileplanter.py syntax highlighted
#!/usr/bin/python
#
# Copyright (c) 2003, Vesa Kaihlavirta
#
# This file is part of Gzz.
#
# Gzz is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Gzz is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
# Public License for more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with Gzz; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
#
"""
Creates a standard directory hierarchy from a list of
.java-files in standard input or parameters.
'Standard' in the sense that eg. class Foo in package
x.y is always in a file x/y/Foo.java.
Usage:
# find src/ -name '*.java' | ./compile-planter.py
"""
import fileinput, os, os.path
class CompilePlanter:
"""...one plants trees, you know."""
root = 'COMPILE/'
cache = {}
def getPkg(self, fn):
"""Get package from specified .java-file"""
f = file(fn)
for l in f:
a = l.find('package')
if a != -1: return l[a:].split()[1][:-1]
def readTree(self):
"""Read all specified .java-files to cache"""
for l in fileinput.input():
l = l.strip()
pkg = self.getPkg(l)
self.cache[l] = (pkg, os.path.split(l)[1])
def plant(self):
"""Symlinks the standard hierarchy from cache."""
for a in self.cache:
dir = os.path.join(self.root, self.cache[a][0]).replace('.', '/')
try: # these throw if file/directory already exists
os.makedirs(dir)
os.symlink(os.path.abspath(a), os.path.join(dir, self.cache[a][1]))
except OSError: pass
if __name__ == '__main__':
cp = CompilePlanter()
cp.readTree()
cp.plant()
See more files for this project here