예제 #1
0
def attrib(location, output, template, vartext, quiet, verbose):
    """
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.

LOCATION: Path to a file, directory or .zip archive containing .ABOUT files.

OUTPUT: Path where to write the attribution document.
    """
    if not quiet:
        print_version()
        click.echo('Generating attribution...')

    # accept zipped ABOUT files as input
    if location.lower().endswith('.zip'):
        location = extract_zip(location)

    errors, abouts = collect_inventory(location)

    attrib_errors = generate_attribution_doc(
        abouts=abouts,
        output_location=output,
        template_loc=template,
        variables=vartext,
    )
    errors.extend(attrib_errors)

    errors_count = report_errors(errors,
                                 quiet,
                                 verbose,
                                 log_file_loc=output + '-error.log')

    if not quiet:
        msg = 'Attribution generated in: {output}'.format(**locals())
        click.echo(msg)
    sys.exit(errors_count)
예제 #2
0
def inventory(location, output, mapping, quiet, format, show_all):
    """
Collect a JSON or CSV inventory of components from .ABOUT files.

LOCATION: Path to an .ABOUT file or a directory with .ABOUT files.

OUTPUT: Path to the JSON or CSV inventory file to create.
    """
    print_version()

    if not exists(os.path.dirname(output)):
        # FIXME: there is likely a better way to return an error
        click.echo('ERROR: <OUTPUT> path does not exists.')
        # FIXME: return error code?
        return

    click.echo(
        'Collecting inventory from: %(location)r and writing output to: %(output)r'
        % locals())

    # FIXME: do we really want to continue support zip as an input?
    if location.lower().endswith('.zip'):
        # accept zipped ABOUT files as input
        location = extract_zip(location)

    errors, abouts = model.collect_inventory(location, use_mapping=mapping)

    write_errors = model.write_output(abouts, output, format)
    for err in write_errors:
        errors.append(err)

    finalized_errors = ignore_about_resource_path_not_exist_error(errors)

    log_errors(finalized_errors, quiet, show_all, os.path.dirname(output))
    sys.exit(0)
예제 #3
0
def inventory(location, output, format, quiet, verbose):  # NOQA
    """
Collect the inventory of .ABOUT file data as CSV or JSON.

LOCATION: Path to an .ABOUT file or a directory with .ABOUT files.

OUTPUT: Path to the JSON or CSV inventory file to create.
    """
    if not quiet:
        print_version()
        click.echo('Collecting inventory from ABOUT files...')

    if location.lower().endswith('.zip'):
        # accept zipped ABOUT files as input
        location = extract_zip(location)
    errors, abouts = collect_inventory(location)
    write_errors = write_output(abouts=abouts, location=output, format=format)
    errors.extend(write_errors)
    errors_count = report_errors(errors,
                                 quiet,
                                 verbose,
                                 log_file_loc=output + '-error.log')
    if not quiet:
        msg = 'Inventory collected in {output}.'.format(**locals())
        click.echo(msg)
    sys.exit(errors_count)
예제 #4
0
def attrib(location, output, template, mapping, mapping_file, inventory,
           vartext, quiet, verbose):
    """
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.

LOCATION: Path to an .ABOUT file, a directory containing .ABOUT files or a .zip archive containing .ABOUT files.

OUTPUT: Path to output file to write the attribution to.
    """
    print_version()
    click.echo('Generating attribution...')

    # accept zipped ABOUT files as input
    if location.lower().endswith('.zip'):
        location = extract_zip(location)

    inv_errors, abouts = model.collect_inventory(location,
                                                 use_mapping=mapping,
                                                 mapping_file=mapping_file)
    no_match_errors = attrib_generate_and_save(abouts=abouts,
                                               output_location=output,
                                               use_mapping=mapping,
                                               mapping_file=mapping_file,
                                               template_loc=template,
                                               inventory_location=inventory,
                                               vartext=vartext)

    if not no_match_errors:
        # Check for template error
        with open(output, 'r') as output_file:
            first_line = output_file.readline()
            if first_line.startswith('Template'):
                click.echo(first_line)
                sys.exit(errno.ENOEXEC)

    for no_match_error in no_match_errors:
        inv_errors.append(no_match_error)

    error_count = 0

    for e in inv_errors:
        # Only count as warning/error if CRITICAL, ERROR and WARNING
        if e.severity > 20:
            error_count = error_count + 1

    log_errors(inv_errors, error_count, quiet, verbose,
               os.path.dirname(output))
    click.echo(' %(error_count)d errors or warnings detected.' % locals())
    click.echo('Finished.')
    sys.exit(0)
예제 #5
0
def attrib(location, output, template, mapping, mapping_file, inventory, vartext, quiet, verbose):
    """
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.

LOCATION: Path to an .ABOUT file, a directory containing .ABOUT files or a .zip archive containing .ABOUT files.

OUTPUT: Path to output file to write the attribution to.
    """
    print_version()
    click.echo('Generating attribution...')

    # accept zipped ABOUT files as input
    if location.lower().endswith('.zip'):
        location = extract_zip(location)

    inv_errors, abouts = model.collect_inventory(location, use_mapping=mapping, mapping_file=mapping_file)
    no_match_errors = attrib_generate_and_save(
        abouts=abouts, output_location=output,
        use_mapping=mapping, mapping_file=mapping_file, template_loc=template,
        inventory_location=inventory, vartext=vartext)

    if not no_match_errors:
        # Check for template error
        with open(output, 'r') as output_file:
            first_line = output_file.readline()
            if first_line.startswith('Template'):
                click.echo(first_line)
                sys.exit(errno.ENOEXEC)

    for no_match_error in no_match_errors:
        inv_errors.append(no_match_error)

    error_count = 0

    for e in inv_errors:
        # Only count as warning/error if CRITICAL, ERROR and WARNING
        if e.severity > 20:
            error_count = error_count + 1

    log_errors(inv_errors, error_count, quiet, verbose, os.path.dirname(output))
    click.echo(' %(error_count)d errors or warnings detected.' % locals())
    click.echo('Finished.')
    sys.exit(0)
예제 #6
0
def attrib(location, output, template, mapping, inventory, quiet, show_all):
    """
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.

LOCATION: Path to an .ABOUT file, a directory containing .ABOUT files or a .zip archive containing .ABOUT files.

OUTPUT: Path to output file to write the attribution to.
    """
    print_version()
    click.echo('Generating attribution...')

    # accept zipped ABOUT files as input
    if location.lower().endswith('.zip'):
        location = extract_zip(location)

    inv_errors, abouts = model.collect_inventory(location, use_mapping=mapping)
    no_match_errors = attrib_generate_and_save(abouts=abouts,
                                               output_location=output,
                                               use_mapping=mapping,
                                               template_loc=template,
                                               inventory_location=inventory)

    if not no_match_errors:
        # Check for template error
        with open(output, 'r') as output_file:
            first_line = output_file.readline()
            if first_line.startswith('Template'):
                click.echo(first_line)
                sys.exit(errno.ENOEXEC)

    for no_match_error in no_match_errors:
        inv_errors.append(no_match_error)

    finalized_errors = ignore_about_resource_path_not_exist_error(inv_errors)

    log_errors(finalized_errors, quiet, show_all, os.path.dirname(output))
    click.echo('Finished.')
    sys.exit(0)
예제 #7
0
def inventory(location, output, mapping, mapping_file, mapping_output, filter,
              quiet, format, verbose):  # NOQA
    """
Collect a JSON or CSV inventory of components from .ABOUT files.

LOCATION: Path to an .ABOUT file or a directory with .ABOUT files.

OUTPUT: Path to the JSON or CSV inventory file to create.
    """
    print_version()

    if not exists(os.path.dirname(output)):
        # FIXME: there is likely a better way to return an error
        click.echo('ERROR: <OUTPUT> path does not exists.')
        # FIXME: return error code?
        return

    click.echo(
        'Collecting inventory from: %(location)s and writing output to: %(output)s'
        % locals())

    # FIXME: do we really want to continue support zip as an input?
    if location.lower().endswith('.zip'):
        # accept zipped ABOUT files as input
        location = extract_zip(location)

    errors, abouts = model.collect_inventory(location,
                                             use_mapping=mapping,
                                             mapping_file=mapping_file)

    updated_abouts = []
    if filter:
        filter_dict = {}
        # Parse the filter and save to the filter dictionary with a list of value
        for element in filter:
            key = element.partition('=')[0]
            value = element.partition('=')[2]
            if key in filter_dict:
                filter_dict[key].append(value)
            else:
                value_list = [value]
                filter_dict[key] = value_list
        updated_abouts = inventory_filter(abouts, filter_dict)
    else:
        updated_abouts = abouts

    # Do not write the output if one of the ABOUT files has duplicated key names
    dup_error_msg = u'Duplicated key name(s)'
    halt_output = False
    for err in errors:
        if dup_error_msg in err.message:
            halt_output = True
            break

    if not halt_output:
        write_errors = model.write_output(updated_abouts, output, format,
                                          mapping_output)
        for err in write_errors:
            errors.append(err)
    else:
        msg = u'Duplicated key names are not supported.\n' + \
                        'Please correct and re-run.'
        print(msg)

    error_count = 0

    for e in errors:
        # Only count as warning/error if CRITICAL, ERROR and WARNING
        if e.severity > 20:
            error_count = error_count + 1

    log_errors(errors, error_count, quiet, verbose, os.path.dirname(output))
    click.echo(' %(error_count)d errors or warnings detected.' % locals())
    sys.exit(0)
예제 #8
0
def inventory(location, output, mapping, mapping_file, mapping_output, filter, quiet, format, verbose):  # NOQA
    """
Collect a JSON or CSV inventory of components from .ABOUT files.

LOCATION: Path to an .ABOUT file or a directory with .ABOUT files.

OUTPUT: Path to the JSON or CSV inventory file to create.
    """
    print_version()

    if not exists(os.path.dirname(output)):
        # FIXME: there is likely a better way to return an error
        click.echo('ERROR: <OUTPUT> path does not exists.')
        # FIXME: return error code?
        return

    click.echo('Collecting inventory from: %(location)s and writing output to: %(output)s' % locals())

    # FIXME: do we really want to continue support zip as an input?
    if location.lower().endswith('.zip'):
        # accept zipped ABOUT files as input
        location = extract_zip(location)

    errors, abouts = model.collect_inventory(location, use_mapping=mapping, mapping_file=mapping_file)

    updated_abouts = []
    if filter:
        filter_dict = {}
        # Parse the filter and save to the filter dictionary with a list of value
        for element in filter:
            key = element.partition('=')[0]
            value = element.partition('=')[2]
            if key in filter_dict:
                filter_dict[key].append(value)
            else:
                value_list = [value]
                filter_dict[key] = value_list
        updated_abouts = inventory_filter(abouts, filter_dict)
    else:
        updated_abouts = abouts

    # Do not write the output if one of the ABOUT files has duplicated key names
    dup_error_msg = u'Duplicated key name(s)'
    halt_output = False
    for err in errors:
        if dup_error_msg in err.message:
            halt_output = True
            break

    if not halt_output:
        write_errors = model.write_output(updated_abouts, output, format, mapping_output)
        for err in write_errors:
            errors.append(err)
    else:
        msg = u'Duplicated key names are not supported.\n' + \
                        'Please correct and re-run.'
        print(msg)

    error_count = 0

    for e in errors:
        # Only count as warning/error if CRITICAL, ERROR and WARNING
        if e.severity > 20:
            error_count = error_count + 1

    log_errors(errors, error_count, quiet, verbose, os.path.dirname(output))
    click.echo(' %(error_count)d errors or warnings detected.' % locals())
    sys.exit(0)