예제 #1
0
def update_module(path, name, type, url):
    print 'update ' + path + "---" + type + '---' + url
    if type == 'git':
        util.git_update(path, url)
    else:
        if os.path.exists(path):
            pass
        else:
            util.safe_mkdir(path)
            download_file = util.download_path(name)
            if not os.path.exists(download_file):
                util.download_http_package(url, download_file)
            if type == 'tar.gz':
                util.extract_tar_gz(download_file, path)
            elif type == 'zip':
                util.extract_zip(download_file, os.path.normpath(path + '/../'))
예제 #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).'