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).'
def main(parser, options, args): """ Main commnand line entry point. """ verbosity = options.verbosity action = options.action copy_files_path = options.copy_files license_text_path = options.license_text_location mapping_config = options.mapping extract_license = options.extract_license action_num = 0 api_url = '' api_key = '' gen_license = False dje_license_dict = {} if options.version: print(__full_info__) sys.exit(0) if verbosity == 1: handler.setLevel(logging.ERROR) elif verbosity >= 2: handler.setLevel(logging.WARNING) valid_actions = 0, 1, 2, 3 if action: if action in valid_actions: action_num = action else: print('ERROR: Invalid action: must be one of: 0, 1, 2 or 3') sys.exit(errno.EINVAL) if copy_files_path: # code to handle tilde character copy_files_path = os.path.abspath(expanduser(copy_files_path)) if not path_exists(copy_files_path): print("ERROR: The COPY_FILES path does not exist.") sys.exit(errno.EINVAL) if license_text_path: # code to handle tilde character license_text_path = os.path.abspath(expanduser(license_text_path)) if not path_exists(license_text_path): print("ERROR: LICENSE_TEXT_LOCATION path does not exist.") sys.exit(errno.EINVAL) mapping_config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'MAPPING.CONFIG') if mapping_config and not path_exists(mapping_config_path): print("ERROR: The file 'MAPPING.CONFIG' does not exist.") sys.exit(errno.EINVAL) if extract_license: api_url = extract_license[0].partition('--api_url=')[2] api_key = extract_license[1].partition('--api_key=')[2] gen_license = True if not len(args) == 2: print('ERROR: <input_path> and <output_path> are required.') print() parser.print_help() sys.exit(errno.EEXIST) input_path, output_path = args output_path = abspath(output_path) if not output_path.endswith('/'): output_path += '/' if not exists(input_path): print('ERROR: <input_path> file does not exist.') print() parser.print_help() sys.exit(errno.EEXIST) if not exists(output_path): print('ERROR: <output_path> directory does not exist.') print() parser.print_help() sys.exit(errno.EEXIST) if not isdir(output_path): print('ERROR: <output_path> must be a directory, not a file.') print() parser.print_help() sys.exit(errno.EISDIR) if not input_path.endswith('.csv'): print("ERROR: <input_path> file must be a CSV file ends with '.csv'") print() parser.print_help() sys.exit(errno.EINVAL) gen = GenAbout() dup_keys = gen.get_duplicated_keys(input_path) if dup_keys: print('ERROR: The <input_path> CSV file contains duplicated column keys. ' 'Duplicated column keys are not allowed.') print(dup_keys) print() print('Please fix the input file and re-run the tool.') sys.exit(errno.EINVAL) # Remove the previous log file if exist log_path = join(output_path, LOG_FILENAME) if exists(log_path): os.remove(log_path) file_handler = logging.FileHandler(log_path) file_logger.addHandler(file_handler) input_data = load_data_from_csv(input_path) input_data = filter_dicts_of_empty_values(input_data) user_keys = [] if mapping_config: mappings = get_mappings(location=None) input_data = apply_mappings(input_data, mappings) user_keys = mappings.keys() gen.validate(input_data) ignored_fields_list = get_unknown_fields(input_data, user_keys) if ignored_fields_list: input_list = gen.get_only_supported_fields(input_data, ignored_fields_list) else: input_list = input_data if copy_files_path: if not isdir(copy_files_path): print("WARNING: The COPY_FILES path must be a directory.") print("'--copy_files' option ignored.") else: licenses_in_project = True license_list = gen.verify_files_existence(input_list, copy_files_path, licenses_in_project) if not license_list: print("WARNING: No file found. '--copy_files' is ignored.") else: gen.copy_files(output_path, license_list) if license_text_path: if not isdir(license_text_path): print("WARNING: The LICENSE_TEXT_LOCATION path must be a directory.") print("'--license_text_location' option ignored.") else: licenses_in_project = False license_list = gen.verify_files_existence(input_list, license_text_path, licenses_in_project) if not license_list: print("WARNING: No file found. '--copy_files' option ignored.") else: gen.copy_files(output_path, license_list) if extract_license: if not api_url or not api_key: print("Missing argument for --extract_license") sys.exit(errno.EINVAL) for line in input_list: try: if line['dje_license_key']: break except Exception as e: print(repr(e)) print("ERROR: The <input_path> CSV does not contain the required column 'dje_license_key' ") sys.exit(errno.EINVAL) if gen_license: # Strip the ' and " for api_url, and api_key from input api_url = api_url.strip("'").strip("\"") api_key = api_key.strip("'").strip("\"") dje_license_dict = gen.pre_process_and_dje_license_dict(input_list, api_url, api_key) dje_license_list = gen.get_dje_license_list(output_path, input_list, gen_license, dje_license_dict) components_list = gen.pre_generation(output_path, input_list, action_num) formatted_output = gen.format_output(components_list) gen.write_output(formatted_output) if dje_license_list: license_list_context = gen.process_dje_licenses(dje_license_list, dje_license_dict, output_path) gen.write_licenses(license_list_context) print('Completed.') gen.warnings_errors_summary() print('Warnings: %s' % len(gen.warnings)) print('Errors: %s' % len(gen.errors))