示例#1
0
def _run_reports(builder_args, additional_args):
    csv_file = builder_args.coverage_csv_file
    no_cs = 'Missing coverage_script from command line or CSV file: "{}"'.format(
        csv_file)
    no_dit = 'Missing default_interface_type from command line or CSV file: "{}"'.format(
        csv_file)
    no_ph = 'Missing product_hierarchy column from CSV file: "{}"'.format(
        csv_file)

    with open(csv_file, 'r') as csvfile:
        for row in DictReader(csvfile, skipinitialspace=True):
            coverage_script = row.pop('coverage_script',
                                      builder_args.coverage_script)
            if coverage_script is None:
                _exit(1, no_cs)

            default_interface_type = row.pop(
                'default_interface_type', builder_args.default_interface_type)
            if default_interface_type is None:
                _exit(1, no_dit)

            product_hierarchy = row.pop('product_hierarchy', None)
            if product_hierarchy is None:
                _exit(1, no_ph)

            coverage_command = [
                coverage_script,
                default_interface_type,
                product_hierarchy,
            ]
            for key, value in ((k, v) for k, v in row.items() if v):
                coverage_command.extend(['--{}'.format(key), value])
            coverage_command.extend(additional_args)
            safe_run(coverage_command)
示例#2
0
def install_hooks():
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                     description='Install helpful git hook templates')
    update_help = 'Optional list of directories to scan for git projects and update the hooks.'
    parser.add_argument('update_paths', nargs='*', help=update_help)
    parser.add_argument('--force', action='store_true',
                        help='Override git hooks in existing git projects')
    parser.add_argument('--template-path', default='~/.git-templates',
                        help='Path to install hook templates')
    args = parser.parse_args()
    # Update git config for template path
    config_command = ['git', 'config', '--global', 'init.templatedir', args.template_path]
    qecommon_tools.safe_run(config_command)
    # Create necessary directories
    destination_dir = os.path.expanduser(os.path.join(args.template_path, 'hooks'))
    if not os.path.exists(destination_dir):
        os.makedirs(destination_dir)
    # Copy hooks to template directory
    source_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'hooks')
    source_hooks = set(os.listdir(source_dir))
    for source_hook in source_hooks:
        shutil.copy(os.path.join(source_dir, source_hook), destination_dir)
    # (Optionally) Update any git repositories found in the provided path(s)
    for update_dir in args.update_paths:
        _update_hooks(update_dir, args.force, source_hooks)
示例#3
0
def _update_hooks(update_dir, force, source_hooks):
    '''Find existing repositories and install hooks'''
    for dir_path, dir_names, file_names in os.walk(update_dir):
        if '.git' in dir_names:
            if force:
                existing_dir = os.path.join(dir_path, '.git', 'hooks')
                for existing_hook in source_hooks.intersection(os.listdir(existing_dir)):
                    os.remove(os.path.join(existing_dir, existing_hook))
            qecommon_tools.safe_run(['git', 'init'], cwd=dir_path)
示例#4
0
def main():
    _method = ''
    # Determine how this is called, whether it's for unittest or opencafe based on the
    # script used
    calling_script = os.path.basename(sys.argv[0])
    if 'unittest' in calling_script:
        _method = 'Unittest'
    elif 'opencafe' in calling_script:
        _method = 'OpenCAFE'

    epilog = 'Note: Run this script from the root of the test tree being reported on'
    description = 'Collect and publish {} coverage report'
    parser = argparse.ArgumentParser(description=description.format(_method),
                                     epilog=epilog)
    parser.add_argument(
        '--data-injection-file-path',
        type=str,
        default='',
        help='A file containing data to inject into the coverage data')
    parser = update_parser(parser)
    parser.add_argument(
        'command',
        nargs='+',
        help='{} command and parameters for running the tests'.format(_method))
    args, coverage_kwargs = parser.parse_known_args()
    kwargs = vars(args)

    tmp_dir_name = mkdtemp()

    os.environ[TAGS_DIR_ENV_NAME] = kwargs['output_dir'] = tmp_dir_name

    args.command.extend(coverage_kwargs)
    safe_run(args.command)

    json_coverage_files = glob.glob('{}/*.json'.format(tmp_dir_name))

    if not json_coverage_files:
        print('')
        print('Error: No JSON report generated!')
        print('       {} command: {}'.format(_method, ' '.join(args.command)))
        sys.exit(-2)

    if len(json_coverage_files) != 1:
        print('')
        print('Error: Too many coverage files found!')
        print('       {}'.format(' '.join(json_coverage_files)))
        sys.exit(-3)

    del kwargs['command']  # Only needed for running test runner
    run_unittest_reports(json_coverage_files[0],
                         kwargs.pop('product_hierarchy'),
                         kwargs.pop('default_interface_type'), **kwargs)
def main():
    epilog = 'Must be run from the repo root with a full log history. (not `--depth 1`)'
    parser = argparse.ArgumentParser(
        description='Build historical coverage reports over a span of time.', epilog=epilog,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    start_help = (
        'The space-separated (quoted) measure and unit representing how far back to look'
        ' in the repo history. The measure must be an integer.'
        ' The unit must be one of: {}. E.g. "5 weeks"'.format(VALID_UNITS)
    )
    parser.add_argument('--start-delta', action=StartUnit, dest='start_unit', default=None,
                        metavar='"<measure> <unit>"', help=start_help)
    by_help = (
        'The unit of measure for the history increment "slices".'
        ' Must be a unit no bigger than the start unit.'
    )
    parser.add_argument('--by-unit', choices=VALID_UNITS, action=SizeVerifiedUnit,
                        default=DEFAULT_BY_UNIT, help=by_help)
    parser.add_argument('--output-dir', default='reports',
                        help='The relative path from repo root to store the reports.')
    args, coverage_args = parser.parse_known_args()
    assert coverage_args, 'No coverage script/args were provided to run after checkout!'
    depth = subprocess.check_output(['git', 'rev-list', '--count', 'HEAD'],
                                    universal_newlines=True).strip('\n')
    assert int(depth) > 1, 'History cannot be run on a "thin" log: depth was {}'.format(depth)
    unclean = subprocess.check_output(['git', 'status', '--porcelain'], universal_newlines=True)
    assert not unclean, 'The repo is not in a clean state: {}'.format(unclean)
    output_path = os.path.abspath(args.output_dir)
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    head_cache = subprocess.check_output(['cat', '.git/HEAD'], universal_newlines=True).strip('\n')
    head_cache = head_cache.split('/')[-1]
    try:
        for date in _generate_rev_dates(args):
            rev_command = [
                'git',
                'rev-list',
                '-n', '1',
                '--before="{:%Y-%m-%d 23:59:59}"'.format(date),
                'master'
            ]
            rev = subprocess.check_output(rev_command, universal_newlines=True).strip('\n')
            if not rev:
                exit(message='No rev found before date: {}'.format(date))
            checkout_command = [
                'git',
                'checkout',
                rev
            ]
            safe_run(checkout_command)
            safe_run(_prepare_coverage_args(coverage_args,
                                            _subdirectory_path(output_path, date),
                                            date))
    finally:
        safe_run(['git', 'checkout', head_cache])
def main():
    parser = argparse.ArgumentParser(
        description='Generate Coverate Reports for a cloned repo')
    parser.add_argument('repo_to_clone',
                        help='the name of the repo to clone.'
                        ' In the format: <organization>/<repo-name>.'
                        ' Do not include the URL or the trailing ".git"')
    parser.add_argument(
        'coverage_script',
        nargs='+',
        help='The command to generate and publish coverage and its parameters.'
        ' NOTE: coverage_script will be run from the top level directory'
        ' of the cloned repo.')
    args, coverage_kwargs = parser.parse_known_args()

    tmp_dir_name = mkdtemp()

    os.chdir(tmp_dir_name)

    clone_command = [
        'git', 'clone',
        '[email protected]:{}.git'.format(args.repo_to_clone),
        cloned_repo_dir_name
    ]
    # coverage-history requires the full-depth commit log
    if args.coverage_script != 'coverage-history':
        clone_command.extend(['--depth', '1'])

    safe_run(clone_command)

    os.chdir(cloned_repo_dir_name)

    safe_run(args.coverage_script + coverage_kwargs)

    os.chdir(starting_directory)
    cleanup_and_exit(dir_name=tmp_dir_name)