# encoding: utf-8 import os import shutil from django.core.management.base import NoArgsCommand # given that a management command belongs in an app, the base dir containing all # apps is three levels down (/apps/some-app/management/commands/this-file) APP_DIR = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../../')) SHORTCUTS_DIR = './shortcuts' TRACKED_FILENAMES = ["views", "models", "admin", "urls", "util", "ajax"] TRACKED_DIRS = ["templates", "templatetags", "management"] def ascertain_type_of_file(filename): if not filename.endswith('.py'): return None for type in TRACKED_FILENAMES: if filename.startswith(type): return type return None class Command(NoArgsCommand): help = """creates a bunch of symlinks in ./shortcuts to make browsing of your code by type (views, models, admin, ...) easier""" def handle_noargs(self, **options): # we recreate the directory every time this command is called # that way we're sure we'll never have any symlinks to files # that have since been deleted if os.path.exists(SHORTCUTS_DIR): shutil.rmtree(SHORTCUTS_DIR) os.mkdir(SHORTCUTS_DIR) for kind in TRACKED_FILENAMES + TRACKED_DIRS: pad = SHORTCUTS_DIR + '/' + kind os.mkdir(pad) # app code counter = 0 for root, subdirs, files in os.walk(APP_DIR): for file in files: type = ascertain_type_of_file(file) ancestor, parent = root.split('/')[-2:] if type: to = root + '/' + file frm = '%s/%s/%s_%s' % (SHORTCUTS_DIR, type, parent, file) os.symlink(to, frm) counter += 1 if parent in TRACKED_DIRS: to = root frm = SHORTCUTS_DIR + '/' + parent + '/' + ancestor if not os.path.exists(frm): os.symlink(to, frm) counter += 1 print "Made %i shortcuts in %i directories under ./shortcuts" % (counter, len(TRACKED_DIRS))