Esempio n. 1
0
File: cpp.py Progetto: csarn/cpip
def main(argv=None):
    """Command line options."""

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date)
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
    program_license = """%s

  Created by Paul Ross on %s.
  Copyright 2015. All rights reserved.

  Licensed under the GPL License 2.0

USAGE
""" % (program_shortdesc, str(__date__))

    # Setup argument parser
    parser = argparse.ArgumentParser(description=program_license,
                            formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("-v", "--verbose", dest="verbose",
                        action="count", default=0,
                        help="set verbosity level [default: %(default)s]")
    parser.add_argument("-t", '--tokens', dest="tokens", action="store_true",
                        help="Show actual preprocessing tokens.")
    parser.add_argument('-V', '--version', action='version',
                        version=program_version_message)
    parser.add_argument(dest="path", metavar="path", nargs='?',
                        help="Paths to source file. If absent then stdin is processed."
                        " [default: %(default)s]")
    
    # cpp like options
    parser.add_argument("-d", dest="macroOptions", action='append',
                        default=[],
                        help="Pre-processor options M, D and N."
                        " [default: %(default)s]")
    parser.add_argument("-E", dest="preprocess", action="store_true",
                        required=True,
                        help="Pre-process, required.")
    Cpp.addStandardArguments(parser)
    args = parser.parse_args()
    if args.path is None:
        # stdin
        myIncH = IncludeHandler.CppIncludeStdin(
                    theUsrDirs=args.incUsr or [],
                    theSysDirs=args.incSys or [],
        )
        ituName = 'stdin'
    else:
        myIncH = IncludeHandler.CppIncludeStdOs(
                    theUsrDirs=args.incUsr or [],
                    theSysDirs=args.incSys or [],
        )
        ituName = args.path
    _processFile(ituName,
                 myIncH,
                 Cpp.stdPredefinedMacros(args),
                 Cpp.predefinedFileObjects(args),
                 args.tokens,
                 args.macroOptions)
    return 0
Esempio n. 2
0
def main():
    """Processes command line to preprocess a file or a directory."""
    program_version = "v%s" % __version__
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
    program_license = """%s
  Created by Paul Ross on %s.
  Copyright 2008-2015. All rights reserved.
  Licensed under GPL 2.0
USAGE
""" % (program_shortdesc, str(__date__))
    parser = argparse.ArgumentParser(
        description=program_license,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        "-c",
        action="store_true",
        dest="plot_conditional",
        default=False,
        help=
        "Add conditionally included files to the plots. [default: %(default)s]"
    )
    parser.add_argument("-d",
                        "--dump",
                        action="append",
                        dest="dump",
                        default=[],
                        help="""Dump output, additive. Can be:
C - Conditional compilation graph.
F - File names encountered and their count.
I - Include graph.
M - Macro environment.
T - Token count.
R - Macro dependencies as an input to DOT.
[default: %(default)s]""")
    parser.add_argument(
        "-g",
        "--glob",
        type=str,
        dest="glob",
        default="*.*",
        help=
        "Pattern match to use when processing directories. [default: %(default)s]"
    )
    parser.add_argument("--heap",
                        action="store_true",
                        dest="heap",
                        default=False,
                        help="Profile memory usage. [default: %(default)s]")
    parser.add_argument(
            "-j", "--jobs",
            type=int,
            dest="jobs",
            default=0,
            help="""Max simultaneous processes when pre-processing
directories. Zero uses number of native CPUs [%d].
1 means no multiprocessing."""
                    % multiprocessing.cpu_count() \
                    + " [default: %(default)s]"
        )
    parser.add_argument("-k",
                        "--keep-going",
                        action="store_true",
                        dest="keep_going",
                        default=False,
                        help="Keep going. [default: %(default)s]")
    parser.add_argument(
            "-l", "--loglevel",
            type=int,
            dest="loglevel",
            default=30,
            help="Log Level (debug=10, info=20, warning=30, error=40, critical=50)" \
            " [default: %(default)s]"
        )
    parser.add_argument("-o",
                        "--output",
                        type=str,
                        dest="output",
                        default="out",
                        help="Output directory. [default: %(default)s]")
    parser.add_argument(
        "-p",
        action="store_true",
        dest="ignore_pragma",
        default=False,
        help="Ignore pragma statements. [default: %(default)s]")
    parser.add_argument(
        "-r",
        "--recursive",
        action="store_true",
        dest="recursive",
        default=False,
        help="Recursively process directories. [default: %(default)s]")
    parser.add_argument(
        "-t",
        "--dot",
        action="store_true",
        dest="include_dot",
        default=False,
        help="""Write an DOT include dependency table and execute DOT
on it to create a SVG file. [default: %(default)s]""")
    parser.add_argument(
        "-G",
        action="store_true",
        dest="gcc_extensions",
        default=False,
        help=
        """Support GCC extensions. Currently only #include_next. [default: %(default)s]"""
    )
    parser.add_argument(dest="path", nargs=1, help="Path to source file.")
    Cpp.addStandardArguments(parser)
    args = parser.parse_args()
    #     print(' ARGS '.center(75, '-'))
    #     print(args)
    #     print(' END: ARGS '.center(75, '-'))
    clkStart = time.clock()
    # Initialise logging etc.
    inPath = args.path[0]
    if args.jobs != 1 and os.path.isdir(inPath):
        # Multiprocessing
        logFormat = '%(asctime)s %(levelname)-8s [%(process)5d] %(message)s'
    else:
        logFormat = '%(asctime)s %(levelname)-8s %(message)s'
    logging.basicConfig(
        level=args.loglevel,
        format=logFormat,
        # datefmt='%y-%m-%d % %H:%M:%S',
        stream=sys.stdout)
    # Memory usage dump
    if args.heap:
        try:
            from guppy import hpy
        except ImportError:
            print('Can not profile memory as you do not have guppy installed:' \
                  ' http://guppy-pe.sourceforge.net/')
            args.heap = False
    # Start memory profiling if requested
    if args.heap:
        myHeap = hpy()
        myHeap.setrelheap()
    else:
        myHeap = None
    # Create objects to pass to pre-processor
    myIncH = IncludeHandler.CppIncludeStdOs(
        theUsrDirs=args.incUsr or [],
        theSysDirs=args.incSys or [],
    )
    preDefMacros = {}
    if args.predefines:
        for d in args.predefines:
            _tup = d.split('=')
            if len(_tup) == 2:
                preDefMacros[_tup[0]] = _tup[1] + '\n'
            elif len(_tup) == 1:
                preDefMacros[_tup[0]] = '\n'
            else:
                raise ValueError('Can not read macro definition: %s' % d)
    # Create the job specification
    jobSpec = MainJobSpec(
        incHandler=myIncH,
        preDefMacros=preDefMacros,
        preIncFiles=Cpp.predefinedFileObjects(args),
        diagnostic=CppDiagnostic.PreprocessDiagnosticKeepGoing()
        if args.keep_going else None,
        pragmaHandler=PragmaHandler.PragmaHandlerNull()
        if args.ignore_pragma else None,
        keepGoing=args.keep_going,
        conditionalLevel=2 if args.plot_conditional else 0,
        dumpList=args.dump,
        helpMap=retOptionMap(parser, args),
        includeDOT=args.include_dot,
        cmdLine=' '.join(sys.argv),
        gccExtensions=args.gcc_extensions,
    )
    if os.path.isfile(inPath):
        preprocessFileToOutput(inPath, args.output, jobSpec)
        writeIndexHtml([inPath], args.output, jobSpec)
    elif os.path.isdir(inPath):
        preprocessDirToOutput(
            inPath,
            args.output,
            jobSpec,
            globMatch=args.glob,
            recursive=args.recursive,
            numJobs=args.jobs,
        )
    else:
        logging.fatal('%s is neither a file or a directory!' % inPath)
        return 1
    if args.heap and myHeap is not None:
        print('Dump of heap:')
        h = myHeap.heap()
        print(h)
        print()
        print('Dump of heap byrcs:')
        print(h.byrcs)
        print()
    clkExec = time.clock() - clkStart
    print('CPU time = %8.3f (S)' % clkExec)
    print('Bye, bye!')
    return 0
Esempio n. 3
0
 def testMacroDefinitionString_02(self):
     self.assertEqual('#define FOO BAR\n', Cpp.macroDefinitionString(['FOO=BAR']))
Esempio n. 4
0
 def testMacroDefinitionString_03(self):
     self.assertEqual('#define FOO(n) n * BAR\n', Cpp.macroDefinitionString(['FOO(n)=n * BAR']))
Esempio n. 5
0
 def testMacroDefinitionString_00(self):
     self.assertEqual('', Cpp.macroDefinitionString([]))
Esempio n. 6
0
 def testMacroDefinitionString_01(self):
     self.assertEqual('#define FOO\n', Cpp.macroDefinitionString(['FOO']))
Esempio n. 7
0
 def testMacroDefinitionDict_03(self):
     self.assertEqual({'FOO(n)' : 'n * BAR\n'}, Cpp.macroDefinitionDict(['FOO(n)=n * BAR']))
Esempio n. 8
0
 def testMacroDefinitionDict_02(self):
     self.assertEqual({'FOO' : 'BAR\n'}, Cpp.macroDefinitionDict(['FOO=BAR']))
Esempio n. 9
0
 def testMacroDefinitionDict_01(self):
     self.assertEqual({'FOO' : '\n'}, Cpp.macroDefinitionDict(['FOO']))
Esempio n. 10
0
 def testMacroDefinitionDict_00(self):
     self.assertEqual({}, Cpp.macroDefinitionDict([]))
Esempio n. 11
0
    def testCppInvoke(self):
        result = Cpp.invokeCppForPlatformMacros('-dM')
#         print(result)
        self.assertTrue(len(result) > 0)