def tplmapper():
    optsparser = TemplatesOptionParser()
    options, args = optsparser.parse_args()

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    logger.addHandler(ch)

    topdir = args

    # 1) walk filesystem and locate every .dtml file
    # 2) parse ast of each .py file to detect invocations of each .dtml file

    dtml_templates = DtmlTemplates()  # a place to collect information about DTML templates
    dtml_templates.load(topdir)       # locate and make note of every DTML file found

    # dpage = self.views.SOME_TEMPLATE_NAME

    walker = TemplateReferenceAstRecorder(dtml_templates)

    for pyfilename in walktrees(rootdirs=topdir, filepatts=('*.py', ), recurse=True):
        if options.verbose:
            logger.info("EVALUATING: ", pyfilename)

        with file(pyfilename) as f:

            if 'ZeSentinelCtrl.py' in pyfilename:
                pysrc = f.read()
                tree = ast.parse(pysrc, filename=pyfilename)
                walker.visit(tree, src_filename=pyfilename)

                dtml_templates.grep_for_unused_templates(pysrc, pyfilename)

    if options.unused_templates:
        dtml_templates.report_unused_templates()
    def load(self, topdirs):
        """Load information about all DTML templates found under a directory tree.
        """

        for filename in walktrees(topdirs, suffixes=('.dtml', ), recurse=True):
            t = self.DtmlTemplate(filename)
            self.tpls[t.basename] = t

        print "Identified %d .dtml files" % len(self.tpls)
        for tpl_basename, tpl_obj  in self.tpls.items():
            print tpl_basename
def imports():

    optsparser = ImportsOptionParser()
    options, args = optsparser.parse_args()

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    logger.addHandler(ch)

    topdir = args

    # 1) run pyflake over each .py file to determine unused imports
    # 2) parse ast of each .py file to determine names/places (not dirs) of imports
    # ?) run entire app, after patching pyenv, to refine dirs of imports
    # 4) report imports used
    # 5) report imports unused
    # ?) report imports never executed at all

    # option to ignore distname='stdlib' to/from
    # option to ignore internal imports
    # option to collapse imports into distnames and show which import which
    # reverse option to report who imports which distnames/modules

    distnames = DistributionNames()
    imports = ImportOccurrences(distnames)

    walker = ImportsAstRecorder(imports)

    # Do Static Analysis First and Accumulate Results

    for pyfilename in walktrees(rootdirs=topdir, filepatts=("*.py",), recurse=True):
        if options.verbose:
            logger.info("EVALUATING: ", pyfilename)

        if options.unused_imports:
            imports.discover_unused_imports(pyfilename)

        if options.static_imports:
            with file(pyfilename) as f:
                tree = ast.parse(f.read(), filename=pyfilename)
                walker.visit(tree, src_filename=pyfilename)

    # Now Optionally Run Dynamic Analysis to Refine Information
    if options.run:
        walker = ImportsRunRecorder(imports)
        walker.run(options.run)
        imports.report_found_imports(options)

    if options.unused_imports:
        imports.report_unused_imports()

    if options.static_imports:
        imports.report_found_imports(options)
def srcmapper():
    optsparser = TemplatesOptionParser()
    options, args = optsparser.parse_args()

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    log.addHandler(ch)

    topdir = args

    # 1) walk filesystem and locate every .dtml file
    # 2) parse ast of each .py file to detect invocations of each .dtml file

    #    dtml_templates = DtmlTemplates()  # a place to collect information about DTML templates
    #    dtml_templates.load(topdir)       # locate and make note of every DTML file found

    # dpage = self.views.SOME_TEMPLATE_NAME

    srcmapper = SourceMapper()

    for filename in walktrees(rootdirs=topdir, filepatts=('*.py', '*.dtml'), recurse=True):
        if options.verbose:
            log.info("FOUND: ", filename)

        if filename.endswith('.py'):
            srcmapper.add_pysrc_file(filename)

        if filename.endswith('.dtml'):
            srcmapper.add_dtml_file(filename)

    print "Identified %d .dtml files" % len(srcmapper.dtml_files)
    for tpl_basename in srcmapper.dtml_files.keys():
        print tpl_basename

    srcmapper.analyze()

    if options.unused_templates:
        report_unused_templates(srcmapper)