parameters: ()
docstring: Main entry point.
docstring_summary: Main entry point.
docstring_tokens: Main entry point .
function: def main():
      """Main entry point."""
      # ========================================================================
      # Parse command line arguments.
      # ========================================================================
      parser = optparse.OptionParser("USAGE: %prog [options] -s file.cpp")
      parser.add_option('-I', '--include-dir', dest='include_dirs', default=[],
                        type='string', help='Specify include directories',
                        action='append')
      parser.add_option('-s', '--src-file', dest='source_files', default=[],
                        type='string', help='Specify compilation units.',
                        action='append')
      parser.add_option('--src-file-files', dest='source_files_files', default=[],
                        type='string', help='Specify file with paths to compilation units.',
                        action='append')
      parser.add_option('-l', '--location-file', dest='location_file',
                        default='locations.dat', type='string',
                        help='Path to file with compound statement locations.')
      parser.add_option('-g', '--gcov-file', dest='gcov_files', default=[],
                        type='string', help='Specify gcov files to process.',
                        action='append')
      parser.add_option('--gcov-files', dest='gcov_files_files', default=[],
                        type='string', help='Specify gcov files to process.',
                        action='append')
      parser.add_option('-q', '--quiet', dest='verbosity', default=1,
                        action='store_const', const=0, help='Fewer message.')
      parser.add_option('-v', '--verbose', dest='verbosity', default=1,
                        action='store_const', const=2, help='More messages.')
      options, args = parser.parse_args()
      if len(args) != 0:
          parser.error('Incorrect number of arguments!')
          return 1
  
      options.include_dirs += [os.path.abspath(os.path.dirname(s)) for s in options.source_files]
  
      # ========================================================================
      # Read in files with paths from arguments.
      # ========================================================================
  
      for path in options.source_files_files:
        with open(path, 'rb') as f:
          options.source_files += [x.strip() for x in f.readlines()]
      for path in options.gcov_files_files:
        with open(path, 'rb') as f:
          options.gcov_files += [x.strip() for x in f.readlines()]
  
      if not options.source_files and not options.gcov_files:
          parser.error('Neither source nor gcov file given!')
          return 1
  
      # ========================================================================
      # Collect interesting lines if any source files given.
      # ========================================================================
  
      if options.source_files:
          # If any source file is given, all given source files are parsed and all
          # lines with compound statements in all included files are written to
          # the location file.
          if options.verbosity >= 1:
              print >>sys.stderr, 'Building Locations'
          if options.verbosity >= 2:
              print >>sys.stderr, '=================='
  
          # Fire off AST traversal.
          if options.verbosity >= 1:
              print >>sys.stderr, 'AST Traversal'
          node_visitor = CollectCompoundStatementNodeVisitor(options)
          for src in options.source_files:
              if options.verbosity >= 2:
                  print >>sys.stderr, '  Compilation Unit', src
              AstTraverser.visitFile(src, node_visitor, options)
  
          # Convert locations into points.
          locations = {}
          for filename, start, stop in node_visitor.ranges:
              filename = os.path.abspath(filename)
              for i in range(start, stop + 1):
                  locations.setdefault(filename, set()).add(i)
  
          # Write out the source locations.
          if options.verbosity >= 1:
              print >>sys.stderr, 'Writing out locations to', options.location_file
          with open(options.location_file, 'wb') as f:
              pickle.dump(locations, f)
  
      # ========================================================================
      # Process GCOV files if any are given.
      # ========================================================================
  
      if options.gcov_files:
          # If no source files and gcov files are given then
          if options.verbosity >= 1:
              print >>sys.stderr, 'Updating gcov Results'
          if options.verbosity >= 2:
              print >>sys.stderr, '====================='
  
          if not options.source_files:
              if options.verbosity >= 1:
                  print >>sys.stderr, 'Loading locations from', options.location_file
              with open(options.location_file, 'rb') as f:
                  locations = pickle.load(f)
  
          for filename in options.gcov_files:
              filename = os.path.abspath(filename)
              if options.verbosity >= 2:
                  print >>sys.stderr, 'Processing', filename
              with open(filename, 'rb') as f:
                  lines = f.readlines()
              pos0 = lines[0].find(':')
              pos1 = lines[0].find(':', pos0 + 1)
              source = None
              result = []
              skip = False
              for i, line in enumerate(lines):
                  coverage = line[:pos0]
                  lineno = int(line[pos0 + 1:pos1].strip())
                  slineno = line[pos0 + 1:pos1]
                  txt = line[pos1 + 1:]
                  if txt.startswith('Source:'):
                      source = os.path.abspath(txt[len('Source:'):].strip())
                      if not locations.has_key(source):
                          if options.verbosity >= 2:
                              print >>sys.stderr, '  Skipping.'
                          skip = True
                          break
                  if not source or lineno == 0:
                      result.append(line)
                      continue  # Proceed only if in file.
                  if lineno in locations[source] and coverage.strip() == '-':
                      coverage = ('%%%ds' % pos0) % '#####'
                  result.append(':'.join([coverage, slineno, txt]))
              # Write back file if not skipped.
              if skip:
                  continue
              with open (filename, 'wb') as f:
                f.write(''.join(result))
function_tokens: def main ( ) : # ======================================================================== # Parse command line arguments. # ======================================================================== parser = optparse . OptionParser ( "USAGE: %prog [options] -s file.cpp" ) parser . add_option ( '-I' , '--include-dir' , dest = 'include_dirs' , default = [ ] , type = 'string' , help = 'Specify include directories' , action = 'append' ) parser . add_option ( '-s' , '--src-file' , dest = 'source_files' , default = [ ] , type = 'string' , help = 'Specify compilation units.' , action = 'append' ) parser . add_option ( '--src-file-files' , dest = 'source_files_files' , default = [ ] , type = 'string' , help = 'Specify file with paths to compilation units.' , action = 'append' ) parser . add_option ( '-l' , '--location-file' , dest = 'location_file' , default = 'locations.dat' , type = 'string' , help = 'Path to file with compound statement locations.' ) parser . add_option ( '-g' , '--gcov-file' , dest = 'gcov_files' , default = [ ] , type = 'string' , help = 'Specify gcov files to process.' , action = 'append' ) parser . add_option ( '--gcov-files' , dest = 'gcov_files_files' , default = [ ] , type = 'string' , help = 'Specify gcov files to process.' , action = 'append' ) parser . add_option ( '-q' , '--quiet' , dest = 'verbosity' , default = 1 , action = 'store_const' , const = 0 , help = 'Fewer message.' ) parser . add_option ( '-v' , '--verbose' , dest = 'verbosity' , default = 1 , action = 'store_const' , const = 2 , help = 'More messages.' ) options , args = parser . parse_args ( ) if len ( args ) != 0 : parser . error ( 'Incorrect number of arguments!' ) return 1 options . include_dirs += [ os . path . abspath ( os . path . dirname ( s ) ) for s in options . source_files ] # ======================================================================== # Read in files with paths from arguments. # ======================================================================== for path in options . source_files_files : with open ( path , 'rb' ) as f : options . source_files += [ x . strip ( ) for x in f . readlines ( ) ] for path in options . gcov_files_files : with open ( path , 'rb' ) as f : options . gcov_files += [ x . strip ( ) for x in f . readlines ( ) ] if not options . source_files and not options . gcov_files : parser . error ( 'Neither source nor gcov file given!' ) return 1 # ======================================================================== # Collect interesting lines if any source files given. # ======================================================================== if options . source_files : # If any source file is given, all given source files are parsed and all # lines with compound statements in all included files are written to # the location file. if options . verbosity >= 1 : print >> sys . stderr , 'Building Locations' if options . verbosity >= 2 : print >> sys . stderr , '==================' # Fire off AST traversal. if options . verbosity >= 1 : print >> sys . stderr , 'AST Traversal' node_visitor = CollectCompoundStatementNodeVisitor ( options ) for src in options . source_files : if options . verbosity >= 2 : print >> sys . stderr , '  Compilation Unit' , src AstTraverser . visitFile ( src , node_visitor , options ) # Convert locations into points. locations = { } for filename , start , stop in node_visitor . ranges : filename = os . path . abspath ( filename ) for i in range ( start , stop + 1 ) : locations . setdefault ( filename , set ( ) ) . add ( i ) # Write out the source locations. if options . verbosity >= 1 : print >> sys . stderr , 'Writing out locations to' , options . location_file with open ( options . location_file , 'wb' ) as f : pickle . dump ( locations , f ) # ======================================================================== # Process GCOV files if any are given. # ======================================================================== if options . gcov_files : # If no source files and gcov files are given then if options . verbosity >= 1 : print >> sys . stderr , 'Updating gcov Results' if options . verbosity >= 2 : print >> sys . stderr , '=====================' if not options . source_files : if options . verbosity >= 1 : print >> sys . stderr , 'Loading locations from' , options . location_file with open ( options . location_file , 'rb' ) as f : locations = pickle . load ( f ) for filename in options . gcov_files : filename = os . path . abspath ( filename ) if options . verbosity >= 2 : print >> sys . stderr , 'Processing' , filename with open ( filename , 'rb' ) as f : lines = f . readlines ( ) pos0 = lines [ 0 ] . find ( ':' ) pos1 = lines [ 0 ] . find ( ':' , pos0 + 1 ) source = None result = [ ] skip = False for i , line in enumerate ( lines ) : coverage = line [ : pos0 ] lineno = int ( line [ pos0 + 1 : pos1 ] . strip ( ) ) slineno = line [ pos0 + 1 : pos1 ] txt = line [ pos1 + 1 : ] if txt . startswith ( 'Source:' ) : source = os . path . abspath ( txt [ len ( 'Source:' ) : ] . strip ( ) ) if not locations . has_key ( source ) : if options . verbosity >= 2 : print >> sys . stderr , '  Skipping.' skip = True break if not source or lineno == 0 : result . append ( line ) continue # Proceed only if in file. if lineno in locations [ source ] and coverage . strip ( ) == '-' : coverage = ( '%%%ds' % pos0 ) % '#####' result . append ( ':' . join ( [ coverage , slineno , txt ] ) ) # Write back file if not skipped. if skip : continue with open ( filename , 'wb' ) as f : f . write ( '' . join ( result ) )