Beispiel #1
0
def main(parser, options, args):
    overwrite = options.overwrite
    verbosity = options.verbosity
    mapping_config = options.mapping
    template_location = options.template_location
    verification_location = options.verification_location

    if options.version:
        print('ABOUT tool {0}\n{1}'.format(__version__, __copyright__))
        sys.exit(0)

    if verbosity == 1:
        handler.setLevel(logging.ERROR)
    elif verbosity >= 2:
        handler.setLevel(logging.WARNING)

    if mapping_config:
        if not exists('MAPPING.CONFIG'):
            print("The file 'MAPPING.CONFIG' does not exist.")
            sys.exit(errno.EINVAL)

    if template_location:
        template_location = abspath(expanduser(template_location))
        if not exists(template_location):
            print('The defined template location does not exist.')
            parser.print_help()
            sys.exit(errno.EINVAL)

    if verification_location:
        verification_location = abspath(expanduser(verification_location))
        if not verification_location.endswith('.csv'):
            print('The verification output must ends with ".csv".')
            parser.print_help()
            sys.exit(errno.EINVAL)
        if not exists(dirname(verification_location)):
            print('The verification output directory does not exist.')
            parser.print_help()
            sys.exit(errno.EINVAL)

    if not len(args) >= 2 or not len(args) < 4:
        print('The number of arguments is incorrect.\n')
        parser.print_help()
        sys.exit(errno.EEXIST)

    input_path = args[0]
    output_path = args[1]
    if len(args) == 3:
        component_subset_path = args[2]
    else:
        component_subset_path = ""

    # TODO: need more path normalization (normpath, expanduser)
    input_path = expanduser(normpath(input_path))
    output_path = expanduser(normpath(output_path))

    # Add the following to solve the
    # UnicodeEncodeError: 'ascii' codec can't encode character
    # FIXME: these two lines do not make sense
    reload(sys)
    sys.setdefaultencoding('utf-8')  # @UndefinedVariable

    if not exists(input_path):
        print('Input path does not exist.')
        parser.print_help()
        sys.exit(errno.EEXIST)

    if isdir(output_path):
        print('Output must be a HTML file.')
        parser.print_help()
        sys.exit(errno.EISDIR)

    # We only support HTML currently
    if not output_path.endswith('.html'):
        print('Output must be a HTML file.')
        parser.print_help()
        sys.exit(errno.EINVAL)

    if exists(output_path) and not overwrite:
        print('Output file already exists. Select a different file name '
              'or use the --overwrite option.')
        parser.print_help()
        sys.exit(errno.EEXIST)

    if component_subset_path and not exists(component_subset_path):
        print('Component Subset path does not exist.')
        parser.print_help()
        sys.exit(errno.EEXIST)

    if not exists(output_path) or (exists(output_path) and overwrite):
        collector = Collector(input_path)
        outlist = None
        if not component_subset_path:
            sublist = None
        else:
            input_list = []
            with open(component_subset_path, "rU") as f:
                input_dict = csv.DictReader(f)
                for row in input_dict:
                    input_list.append(row)
            updated_list = convert_dict_key_to_lower_case(input_list)
            if mapping_config:
                mapping_list = genabout.GenAbout().get_mapping_list()
                updated_list = genabout.GenAbout().convert_input_list(updated_list, mapping_list)
            if not check_about_file_existence_and_format(updated_list):
                print('The required key "about_file" was not found.')
                print('Please use the "--mapping" option to map the input '
                      'keys and verify the mapping information are correct.')
                print('OR, correct the header keys from the component list.')
                parser.print_help()
                sys.exit(errno.EISDIR)
            sublist = component_subset_to_sublist(updated_list)
            outlist = update_path_to_about(sublist)

        attrib_str = collector.generate_attribution(template_path=template_location, limit_to=outlist, verification=verification_location)
        errors = collector.get_genattrib_errors()

        if attrib_str:
            try:
                with open(output_path, "w") as f:
                    f.write(attrib_str)
            except Exception as e:
                print("Problem occurs. Attribution was not generated.")
                print(e)

        # Remove the previous log file if exist
        log_path = join(dirname(output_path), LOG_FILENAME)
        if exists(log_path):
            os.remove(log_path)

        file_handler = logging.FileHandler(log_path)
        file_logger.addHandler(file_handler)
        for error_msg in errors:
            logger.error(error_msg)
            file_logger.error(error_msg)
        if errors:
            print("%d errors detected." % len(errors))

    else:
        # we should never reach this
        assert False, "Unsupported option(s)."
Beispiel #2
0
def main(parser, options, args):
    overwrite = options.overwrite
    verbosity = options.verbosity
    mapping_config = options.mapping
    template_location = options.template_location
    verification_location = options.verification_location

    if options.version:
        print(__full_info__)
        sys.exit(0)

    if verbosity == 1:
        handler.setLevel(logging.ERROR)
    elif verbosity >= 2:
        handler.setLevel(logging.WARNING)

    if mapping_config:
        if not exists('MAPPING.CONFIG'):
            print("ERROR: The 'MAPPING.CONFIG' file does not exist.")
            sys.exit(errno.EINVAL)

    if template_location:
        template_location = abspath(expanduser(template_location))
        if not exists(template_location):
            print('ERROR: The TEMPLATE_LOCATION file does not exist.')
            parser.print_help()
            sys.exit(errno.EINVAL)

    if verification_location:
        verification_location = abspath(expanduser(verification_location))
        if not verification_location.endswith('.csv'):
            print('ERROR: The VERIFICATION_LOCATION file path must end with ".csv".')
            parser.print_help()
            sys.exit(errno.EINVAL)
        if not exists(dirname(verification_location)):
            print('ERROR: The VERIFICATION_LOCATION file parent directory does not exist.')
            parser.print_help()
            sys.exit(errno.EINVAL)

    if not len(args) >= 2 or not len(args) < 4:
        print('ERROR: The number of arguments is incorrect.')
        parser.print_help()
        sys.exit(errno.EEXIST)

    input_path = args[0]
    output_path = args[1]
    if len(args) == 3:
        component_subset_path = args[2]
    else:
        component_subset_path = ""

    # TODO: need more path normalization (normpath, expanduser)
    input_path = expanduser(normpath(input_path))
    output_path = expanduser(normpath(output_path))

    # Add the following to solve the
    # UnicodeEncodeError: 'ascii' codec can't encode character
    # FIXME: these two lines do not make sense
    reload(sys)
    sys.setdefaultencoding('utf-8')  # @UndefinedVariable

    if not exists(input_path):
        print('ERROR: <input_path> does not exist.')
        parser.print_help()
        sys.exit(errno.EEXIST)

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

    if isdir(output_path):
        print('ERROR: <output_path> cannot be a directory')
        parser.print_help()
        sys.exit(errno.EISDIR)

    """
    # We only support HTML currently
    if not output_path.endswith('.html'):
        print('ERROR: <output_path> must be an HTML file.')
        parser.print_help()
        sys.exit(errno.EINVAL)
    """

    if exists(output_path) and not overwrite:
        print('ERROR: A file at <output_path> already exists. Select a different file name or use the --overwrite option.')
        parser.print_help()
        sys.exit(errno.EEXIST)

    if component_subset_path and not exists(component_subset_path):
        print('ERROR: the <component_list> CSV file does not exist.')
        parser.print_help()
        sys.exit(errno.EEXIST)

    if not exists(output_path) or (exists(output_path) and overwrite):
        collector = Collector(input_path)
        outlist = None
        if not component_subset_path:
            sublist = None

        else:
            with open(component_subset_path, 'rU') as inp:
                reader = csv.DictReader(inp)
                abouts = [data for data in reader]

            abouts = lower_keys(abouts)

            if mapping_config:
                abouts = apply_mappings(abouts)

            if not has_about_file_keys(abouts):
                print('ERROR: The required column key "about_file" was not found in the <component_list> CSV file.')
                print('Please use the "--mapping" option to map the input keys and verify the mapping information are correct.')
                print('OR, correct the header keys from the component list.')
                parser.print_help()
                sys.exit(errno.EISDIR)

            abouts = normalize_about_file_paths(abouts)

            sublist = get_about_file_paths(abouts)
            outlist = as_about_paths(sublist)

        attrib_str = collector.generate_attribution(template_path=template_location, limit_to=outlist, verification=verification_location)
        errors = collector.get_genattrib_errors()

        if attrib_str:
            try:
                with open(output_path, 'w') as f:
                    f.write(attrib_str)
            except Exception as e:
                print('An error occurred. Attribution was not generated.')
                print(e)

        print('Completed.')
        # Remove the previous log file if exist
        log_path = join(dirname(output_path), LOG_FILENAME)
        if exists(log_path):
            os.remove(log_path)

        file_handler = logging.FileHandler(log_path)
        file_logger.addHandler(file_handler)
        for error_msg in errors:
            logger.error(error_msg)
            file_logger.error(error_msg)
        if errors:
            print('%d errors detected.' % len(errors))

    else:
        # we should never reach this
        assert False, 'Unsupported option(s).'