<commit_before>#!/usr/bin/env python
import os, sys, argparse, string, subprocess, time
from argparse import RawTextHelpFormatter

def buildCMD(options):
  tmp_cmd = []
  # Store any additional directories supplied in a temp list
  if options.mode != 'combine' and options.mode != 'sync':
    tmp_additional_directories = str(' '.join(['--directory ' + os.getcwd() + '/' + app + '/src' for app in options.application[1:]])).split()

  # Initialize Mode (run initialize mode before run_tests, or application)
  if options.mode == 'initialize':
    if len(options.application) > 1:
      tmp_cmd.append([options.lcov_command[0],
                      '--capture',
                      '--initial',
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--output-file', 'initialize.info'
                      ])
      tmp_cmd[0].extend(tmp_additional_directories)
    else:
      tmp_cmd.append([options.lcov_command[0],
                      '--capture',
                      '--initial',
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--output-file', 'initialize.info'
                      ])

  # Generate Mode (run generate mode only after initialize mode, run_tests/application has ran)
  if options.mode == 'generate':
    if len(options.application) > 1:
      tmp_cmd.append([options.lcov_command[0],
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--capture',
                      '--ignore-errors', 'gcov,source',
                      '--output-file', 'covered.info'
                      ])
      tmp_cmd[0].extend(tmp_additional_directories)
    else:
      tmp_cmd.append([options.lcov_command[0],
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--capture',
                      '--ignore-errors', 'gcov,source',
                      '--output-file', 'covered.info'
                      ])

    # Build lcov combine command
    tmp_cmd.append([options.lcov_command[0],
                    '--add-tracefile', 'initialize.info',
                    '--add-tracefile', 'covered.info',
                    '--output-file', 'combined.info' ])

    # Build lcov filter command
    tmp_cmd.append([options.lcov_command[0],
                    '--extract', 'combined.info', '*' + options.application[0] + '/src*',
                    '--extract', 'combined.info', '*' + options.application[0] + '/include*',
                    '--output-file', 'final.info' ])

    # Build genhtml command if --generate-html was used
    if options.generate_html:
      tmp_cmd.append([options.genhtml_command[0], 'final.info',
                      '--title', options.title + ' Test Coverage',
                      '--num-spaces', '2',
                      '--legend',
                      '--no-branch-coverage',
                      '--output-directory', options.html_location])

    # Clean up old tracefiles if asked
    if options.cleanup:
      tmp_cmd.append(['rm', '-f', 'initialize.info', 'covered.info', 'combined.info'])

  # Combine Mode
  if options.mode == 'combine':
    # Build lcov tracefile command
    for single_tracefile in options.add_tracefile:
      options.lcov_command.extend(['--add-tracefile', single_tracefile])
    options.lcov_command.extend(['-o', options.outfile ])
    tmp_cmd.append(options.lcov_command)
    # Build genhtml command if --generate-html was used
    if options.generate_html:
      tmp_cmd.append([options.genhtml_command[0], options.outfile,
                      '--title', options.title + ' Test Coverage',
                      '--num-spaces', '2',
                      '--legend',
                      '--no-branch-coverage',
                      '--output-directory', options.html_location])

  # Sync Mode
  if options.mode == 'sync':
    tmp_cmd.append([options.rsync_command[0],
                    '-ro', options.html_location, options.sync_location ])

  # Run all built commands. This is where the magic happens
  for single_command in tmp_cmd:
    if options.debug:
      print '\n\nDry Run:', str(options.mode) + ' Mode\n', ' '.join(single_command)
    else:
      runCMD(single_command)

  # Run post-process stuff
  postProcess(options)

def postProcess(options):
  # Add exact time at the bottom of generated HTML coverage
  if options.generate_html and options.addtime:
    addBetterDate(options)

  # Clean up
  if options.cleanup and options.mode is not 'sync':
    cleanUp(options)

def cleanUp(options):
  if options.mode == 'combine':
    for tracefile in options.add_tracefile:
      if options.debug:
        print '\n\nDry Run: deleting file', tracefile
      else:
        try:
          os.remove(tracefile)
        except:
          pass
    if options.debug:
      print '\n\nDry Run: deleting file', options.outfile
    else:
      try:
        os.remove(options.outfile)
      except:
        pass
  if options.mode == 'generate':
    if options.debug:
      print '\n\nDry Run: deleting file', options.outfile
    else:
      try:
        os.remove(options.outfile)
      except:
        pass

def addBetterDate(options):
  if options.debug:
    print '\n\nDry Run: appending timestamp to generated HTML content\n', "echo '" + str(time.ctime(time.time())) + "'", '>>', str(options.html_location) + '/index.html'
  else:
    current_time = time.ctime(time.time())
    output_file = open(options.html_location + '/index.html', 'a')
    output_file.write(current_time)
    output_file.close()

def runCMD(cmd_opts):
  a_proc = subprocess.Popen(cmd_opts, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  retstr = a_proc.communicate()
  if not a_proc.poll() == 0:
    print 'Error:', retstr[1]
    sys.exit(1)
  else:
    return retstr[0]

def exit_error(error_list):
  print '\n\tThere were errors running build_coverage:\n'
  for sgl_error in error_list:
    print sgl_error + '\n'
  sys.exit(1)

def _find(myfile, matchFunc=os.path.isfile):
    paths = string.split(os.getenv('PATH'), os.pathsep)
    for eachpth in paths:
        candidate = os.path.join(eachpth, myfile)
        if matchFunc(candidate):
            return candidate
def find(myfile):
    return _find(myfile)

def _verifyOptions(options):
  error_list = []
  # TODO
  # print ignored commands as warnings?
  # warn_list = []

  # Discover and set the necessary binaries we need
  # find('app_name') returns None if not found
  options.lcov_command = [ find('lcov') ]
  options.genhtml_command = [ find('genhtml') ]
  options.rsync_command = [ find('rsync') ]

  # gererate and combine mode parsing options
  if options.mode == 'generate' or options.mode == 'combine':
    if options.generate_html and options.title is None:
      error_list.append('when generating HTML content, you must specify a title page with --title')
    if options.generate_html and options.html_location is None:
      error_list.append('when generating HTML content, you must specify a save location with --html-location')
    if options.html_location is not None and os.path.exists(options.html_location):
      if options.overwrite is False:
        error_list.append('html location specified already exists. Exiting for safty measures...')

  # generate specific parsing options
  if options.mode == 'initialize':
    if options.application is None:
      error_list.append('initialize mode requires a list of applications to zero counters: --application <list of directories>')

  # generate specific parsing options
  if options.mode == 'generate':
    if options.application is None:<commit_msg>We really do need output file specification to make generate mode work correctly. References #1676<commit_after>#!/usr/bin/env python
import os, sys, argparse, string, subprocess, time
from argparse import RawTextHelpFormatter

def buildCMD(options):
  tmp_cmd = []
  # Store any additional directories supplied in a temp list
  if options.mode != 'combine' and options.mode != 'sync':
    tmp_additional_directories = str(' '.join(['--directory ' + os.getcwd() + '/' + app + '/src' for app in options.application[1:]])).split()

  # Initialize Mode (run initialize mode before run_tests, or application)
  if options.mode == 'initialize':
    if len(options.application) > 1:
      tmp_cmd.append([options.lcov_command[0],
                      '--capture',
                      '--initial',
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--output-file', 'initialize.info'
                      ])
      tmp_cmd[0].extend(tmp_additional_directories)
    else:
      tmp_cmd.append([options.lcov_command[0],
                      '--capture',
                      '--initial',
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--output-file', 'initialize.info'
                      ])

  # Generate Mode (run generate mode only after initialize mode, run_tests/application has ran)
  if options.mode == 'generate':
    if len(options.application) > 1:
      tmp_cmd.append([options.lcov_command[0],
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--capture',
                      '--ignore-errors', 'gcov,source',
                      '--output-file', 'covered.info'
                      ])
      tmp_cmd[0].extend(tmp_additional_directories)
    else:
      tmp_cmd.append([options.lcov_command[0],
                      '--directory', os.getcwd() + '/' + options.application[0],
                      '--capture',
                      '--ignore-errors', 'gcov,source',
                      '--output-file', 'covered.info'
                      ])

    # Build lcov combine command
    tmp_cmd.append([options.lcov_command[0],
                    '--add-tracefile', 'initialize.info',
                    '--add-tracefile', 'covered.info',
                    '--output-file', 'combined.info' ])

    # Build lcov filter command
    tmp_cmd.append([options.lcov_command[0],
                    '--extract', 'combined.info', '*' + options.application[0] + '/src*',
                    '--extract', 'combined.info', '*' + options.application[0] + '/include*',
                    '--output-file', options.outfile ])

    # Build genhtml command if --generate-html was used
    if options.generate_html:
      tmp_cmd.append([options.genhtml_command[0], 'final.info',
                      '--title', options.title + ' Test Coverage',
                      '--num-spaces', '2',
                      '--legend',
                      '--no-branch-coverage',
                      '--output-directory', options.html_location])

    # Clean up old tracefiles if asked
    if options.cleanup:
      tmp_cmd.append(['rm', '-f', 'initialize.info', 'covered.info', 'combined.info'])

  # Combine Mode
  if options.mode == 'combine':
    # Build lcov tracefile command
    for single_tracefile in options.add_tracefile:
      options.lcov_command.extend(['--add-tracefile', single_tracefile])
    options.lcov_command.extend(['-o', options.outfile ])
    tmp_cmd.append(options.lcov_command)
    # Build genhtml command if --generate-html was used
    if options.generate_html:
      tmp_cmd.append([options.genhtml_command[0], options.outfile,
                      '--title', options.title + ' Test Coverage',
                      '--num-spaces', '2',
                      '--legend',
                      '--no-branch-coverage',
                      '--output-directory', options.html_location])

  # Sync Mode
  if options.mode == 'sync':
    tmp_cmd.append([options.rsync_command[0],
                    '-ro', options.html_location, options.sync_location ])

  # Run all built commands. This is where the magic happens
  for single_command in tmp_cmd:
    if options.debug:
      print '\n\nDry Run:', str(options.mode) + ' Mode\n', ' '.join(single_command)
    else:
      runCMD(single_command)

  # Run post-process stuff
  postProcess(options)

def postProcess(options):
  # Add exact time at the bottom of generated HTML coverage
  if options.generate_html and options.addtime:
    addBetterDate(options)

  # Clean up
  if options.cleanup and options.mode is not 'sync':
    cleanUp(options)

def cleanUp(options):
  if options.mode == 'combine':
    for tracefile in options.add_tracefile:
      if options.debug:
        print '\n\nDry Run: deleting file', tracefile
      else:
        try:
          os.remove(tracefile)
        except:
          pass
    if options.debug:
      print '\n\nDry Run: deleting file', options.outfile
    else:
      try:
        os.remove(options.outfile)
      except:
        pass
  if options.mode == 'generate':
    if options.debug:
      print '\n\nDry Run: deleting file', options.outfile
    else:
      try:
        os.remove(options.outfile)
      except:
        pass

def addBetterDate(options):
  if options.debug:
    print '\n\nDry Run: appending timestamp to generated HTML content\n', "echo '" + str(time.ctime(time.time())) + "'", '>>', str(options.html_location) + '/index.html'
  else:
    current_time = time.ctime(time.time())
    output_file = open(options.html_location + '/index.html', 'a')
    output_file.write(current_time)
    output_file.close()

def runCMD(cmd_opts):
  a_proc = subprocess.Popen(cmd_opts, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  retstr = a_proc.communicate()
  if not a_proc.poll() == 0:
    print 'Error:', retstr[1]
    sys.exit(1)
  else:
    return retstr[0]

def exit_error(error_list):
  print '\n\tThere were errors running build_coverage:\n'
  for sgl_error in error_list:
    print sgl_error + '\n'
  sys.exit(1)

def _find(myfile, matchFunc=os.path.isfile):
    paths = string.split(os.getenv('PATH'), os.pathsep)
    for eachpth in paths:
        candidate = os.path.join(eachpth, myfile)
        if matchFunc(candidate):
            return candidate
def find(myfile):
    return _find(myfile)

def _verifyOptions(options):
  error_list = []
  # TODO
  # print ignored commands as warnings?
  # warn_list = []

  # Discover and set the necessary binaries we need
  # find('app_name') returns None if not found
  options.lcov_command = [ find('lcov') ]
  options.genhtml_command = [ find('genhtml') ]
  options.rsync_command = [ find('rsync') ]

  # gererate and combine mode parsing options
  if options.mode == 'generate' or options.mode == 'combine':
    if options.generate_html and options.title is None:
      error_list.append('when generating HTML content, you must specify a title page with --title')
    if options.generate_html and options.html_location is None:
      error_list.append('when generating HTML content, you must specify a save location with --html-location')
    if options.html_location is not None and os.path.exists(options.html_location):
      if options.overwrite is False:
        error_list.append('html location specified already exists. Exiting for safty measures...')
    if options.outfile is not None and os.path.exists(options.outfile):
      if options.overwrite is False:
        error_list.append('output file specified already exists. Exiting for safty measures...')
    if options.outfile is None:
      error_list.append('you must specifiy an output file: --outfile')

  # generate specific parsing options
  if options.mode == 'initialize':
    if options.application is None:
      error_list.append('initialize mode requires a list of applications to zero counters: --application <list of directories>')

  # generate specific parsing options
  if options.mode == 'generate':
    if options.application is None:
{'fraction_non_alphanumeric': 0.12742911755336095, 'fraction_numerical': 0.0036954444090474673, 'mean_word_length': 2.3733075435203093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5306473', 'n_tokens_mistral': 4656, 'n_tokens_neox': 4495, 'n_words': 1128}