diff options
| author | J08nY | 2021-12-10 14:07:57 +0100 |
|---|---|---|
| committer | J08nY | 2021-12-10 14:07:57 +0100 |
| commit | b75821cca821982f98af1342f1df255584e3bd7d (patch) | |
| tree | 5adb8b0c4ac645f7cec087ddf9f6c8d915730196 | |
| parent | 857f36252dfa8c3bcfaac7c3ec3ab79cdbd252ff (diff) | |
| download | sec-certs-b75821cca821982f98af1342f1df255584e3bd7d.tar.gz sec-certs-b75821cca821982f98af1342f1df255584e3bd7d.tar.zst sec-certs-b75821cca821982f98af1342f1df255584e3bd7d.zip | |
Unify logging uses.
| -rw-r--r-- | examples/fips_oop_demo.py | 25 | ||||
| -rwxr-xr-x | fips_cli.py | 32 | ||||
| -rw-r--r-- | sec_certs/dataset/cpe.py | 2 | ||||
| -rw-r--r-- | sec_certs/dataset/fips.py | 13 | ||||
| -rw-r--r-- | sec_certs/dataset/fips_algorithm.py | 5 | ||||
| -rw-r--r-- | sec_certs/helpers.py | 2 | ||||
| -rw-r--r-- | sec_certs/sample/fips.py | 8 |
7 files changed, 43 insertions, 44 deletions
diff --git a/examples/fips_oop_demo.py b/examples/fips_oop_demo.py index 0ae3a444..3c30c86e 100644 --- a/examples/fips_oop_demo.py +++ b/examples/fips_oop_demo.py @@ -7,6 +7,9 @@ from sec_certs.dataset.fips_algorithm import FIPSAlgorithmDataset from sec_certs.config.configuration import config +logger = logging.getLogger(__name__) + + @click.command() @click.option('--config-file', help='Path to config file') @click.option('--json-file', help='Path to dataset json file') @@ -32,36 +35,36 @@ def main(config_file, json_file, no_download_algs, redo_web_scan, redo_keyword_s # Load metadata for certificates from CSV and HTML sources dset.get_certs_from_web(redo=redo_web_scan) - logging.info(f'Finished parsing. Have dataset with {len(dset)} certificates.') - logging.info(f'Dataset saved to {dset.root_dir}/fips_full_dataset.json') + logger.info(f'Finished parsing. Have dataset with {len(dset)} certificates.') + logger.info(f'Dataset saved to {dset.root_dir}/fips_full_dataset.json') - logging.info("Converting pdfs") + logger.info("Converting pdfs") dset.convert_all_pdfs() - logging.info("Extracting keywords now.") + logger.info("Extracting keywords now.") dset.pdf_scan(redo=redo_keyword_scan) - logging.info(f'Finished extracting certificates for {len(dset.certs)} items.') + logger.info(f'Finished extracting certificates for {len(dset.certs)} items.') - logging.info("Searching for tables in pdfs") + logger.info("Searching for tables in pdfs") not_decoded_files = dset.extract_certs_from_tables(higher_precision_results) - logging.info(f"Done. Files not decoded: {not_decoded_files}") - logging.info("Parsing algorithms") + logger.info(f"Done. Files not decoded: {not_decoded_files}") + logger.info("Parsing algorithms") if not no_download_algs: aset = FIPSAlgorithmDataset({}, Path(dset.root_dir / 'web/algorithms'), 'algorithms', 'sample algs') aset.get_certs_from_web() - logging.info(f'Finished parsing. Have algorithm dataset with {len(aset)} algorithm numbers.') + logger.info(f'Finished parsing. Have algorithm dataset with {len(aset)} algorithm numbers.') dset.algorithms = aset - logging.info("finalizing results.") + logger.info("finalizing results.") dset.finalize_results() dset.plot_graphs(show=False) end = datetime.now() - logging.info(f'The computation took {(end - start)} seconds.') + logger.info(f'The computation took {(end - start)} seconds.') if __name__ == '__main__': diff --git a/fips_cli.py b/fips_cli.py index f5b9cbdd..e81e7645 100755 --- a/fips_cli.py +++ b/fips_cli.py @@ -122,9 +122,7 @@ def main( output = Path(output) if not inputpath and not output: - print( - "Error: You did not specify path to load the dataset from, nor did you specify where dataset can be stored." - ) + logger.error("You did not specify path to load the dataset from, nor did you specify where dataset can be stored.") sys.exit(1) if not silent: @@ -138,15 +136,15 @@ def main( try: config.load(Path(configpath)) except FileNotFoundError: - print("Error: Bad path to configuration file") + logger.error("Bad path to configuration file") sys.exit(1) except ValueError as e: - print(f"Error: Bad format of configuration file: {e}") + logger.error(f"Bad format of configuration file: {e}") else: - print(f"Using default configuration file at {DEFAULT_CONFIG_PATH}.") + logger.info(f"Using default configuration file at {DEFAULT_CONFIG_PATH}.") if "all" in actions and "new-run" in actions: - print("Error: Only one of 'new-run' and 'all' can be specified.") + logger.error("Only one of 'new-run' and 'all' can be specified.") sys.exit(1) r_actions = ( @@ -160,16 +158,12 @@ def main( actions = r_actions if "build" in actions and "update" in actions: - print( - "Error: 'build' and 'update' cannot be specified at once. Use 'build' to create dataset from scratch, 'update' to update existing dataset." - ) + logger.error("'build' and 'update' cannot be specified at once. Use 'build' to create dataset from scratch, 'update' to update existing dataset.") if "build" in actions: assert output if inputpath: - print( - "warning: Both 'build' and 'inputpath' specified. 'build' creates new dataset, 'inputpath' will be ignored." - ) + logger.warning("Both 'build' and 'inputpath' specified. 'build' creates new dataset, 'inputpath' will be ignored.") dset: FIPSDataset = FIPSDataset( certs={}, root_dir=output, @@ -183,17 +177,15 @@ def main( # only 'build' can work without inputpath else: if not inputpath: - print("Error: You must provide inputpath to previously generated dataset with 'build'") + logger.error("You must provide inputpath to previously generated dataset with 'build'") sys.exit(1) assert inputpath dset: FIPSDataset = FIPSDataset.from_json(inputpath) - print(f'Have dataset with {len(dset)} certs and {len(dset.algorithms)} algorithms.') + logger.info(f'Have dataset with {len(dset)} certs and {len(dset.algorithms)} algorithms.') if output: - print( - "Warning: You provided both inputpath and outputpath, dataset will be copied to outputpath (without data)" - ) + logger.warning("You provided both inputpath and outputpath, dataset will be copied to outputpath (without data)") dset.root_dir = output dset.to_json(output) @@ -208,9 +200,7 @@ def main( if "table-search" in actions or "update" in actions: if not higher_precision_results: - print( - "Info: You are using table search without higher precision results. It is advised to use the switch in the next run." - ) + logger.info("You are using table search without higher precision results. It is advised to use the switch in the next run.") dset.extract_certs_from_tables(high_precision=higher_precision_results) if "analysis" in actions: diff --git a/sec_certs/dataset/cpe.py b/sec_certs/dataset/cpe.py index 0f09ef64..480b3152 100644 --- a/sec_certs/dataset/cpe.py +++ b/sec_certs/dataset/cpe.py @@ -56,7 +56,7 @@ class CPEDataset(ComplexSerializableType): """ Will build look-up dictionaries that are used for fast matching """ - logging.info('CPE dataset: building lookup dictionaries.') + logger.info('CPE dataset: building lookup dictionaries.') self.vendor_to_versions = {x.vendor: set() for x in self} self.vendor_version_to_cpe = dict() self.title_to_cpes = dict() diff --git a/sec_certs/dataset/fips.py b/sec_certs/dataset/fips.py index 12ed487c..12d4dfb5 100644 --- a/sec_certs/dataset/fips.py +++ b/sec_certs/dataset/fips.py @@ -16,6 +16,9 @@ from sec_certs.serialization.json import ComplexSerializableType, serialize from sec_certs.sample.fips import FIPSCertificate +logger = logging.getLogger(__name__) + + class FIPSDataset(Dataset, ComplexSerializableType): certs: Dict[str, FIPSCertificate] @@ -107,7 +110,7 @@ class FIPSDataset(Dataset, ComplexSerializableType): f"https://csrc.nist.gov/CSRC/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp{cert_id}.pdf" ) sp_paths.append(self.policies_dir / f"{cert_id}.pdf") - logging.info(f"downloading {len(sp_urls)} module pdf files") + logger.info(f"downloading {len(sp_urls)} module pdf files") cert_processing.process_parallel( FIPSCertificate.download_security_policy, list(zip(sp_urls, sp_paths)), config.n_threads, progress_bar_desc="Downloading PDF files" ) @@ -125,14 +128,14 @@ class FIPSDataset(Dataset, ComplexSerializableType): html_paths.append(self.web_dir / f"{cert_id}.html") new_files.append(cert_id) - logging.info(f"downloading {len(html_urls)} module html files") + logger.info(f"downloading {len(html_urls)} module html files") failed = cert_processing.process_parallel( FIPSCertificate.download_html_page, list(zip(html_urls, html_paths)), config.n_threads, progress_bar_desc="Downloading HTML files" ) failed = [c for c in failed if c] self.new_files += len(html_urls) - logging.info(f"Download failed for {len(failed)} files. Retrying...") + logger.info(f"Download failed for {len(failed)} files. Retrying...") cert_processing.process_parallel(FIPSCertificate.download_html_page, failed, config.n_threads, progress_bar_desc="Downloading HTML files again") return new_files @@ -260,7 +263,7 @@ class FIPSDataset(Dataset, ComplexSerializableType): if not no_download_algorithms: aset = FIPSAlgorithmDataset({}, Path(self.root_dir / 'web' / 'algorithms'), 'algorithms', 'sample algs') aset.get_certs_from_web() - logging.info(f'Finished parsing. Have algorithm dataset with {len(aset)} algorithm numbers.') + logger.info(f'Finished parsing. Have algorithm dataset with {len(aset)} algorithm numbers.') self.algorithms = aset @@ -507,7 +510,7 @@ class FIPSDataset(Dataset, ComplexSerializableType): dot.edge(key, conn) edges += 1 - logging.info(f"rendering for {connection_list}: {keys} keys and {edges} edges") + logger.info(f"rendering for {connection_list}: {keys} keys and {edges} edges") dot.render(self.root_dir / (str(output_file_name) + "_connections"), view=show) single_dot.render(self.root_dir / (str(output_file_name) + "_single"), view=show) diff --git a/sec_certs/dataset/fips_algorithm.py b/sec_certs/dataset/fips_algorithm.py index feb019d8..11f3d6c3 100644 --- a/sec_certs/dataset/fips_algorithm.py +++ b/sec_certs/dataset/fips_algorithm.py @@ -13,6 +13,9 @@ from sec_certs.sample.fips import FIPSCertificate from sec_certs.config.configuration import config +logger = logging.getLogger(__name__) + + class FIPSAlgorithmDataset(Dataset, ComplexSerializableType): def get_certs_from_web(self): self.root_dir.mkdir(exist_ok=True) @@ -36,7 +39,7 @@ class FIPSAlgorithmDataset(Dataset, ComplexSerializableType): # get the last page, always helpers.download_file(constants.FIPS_ALG_URL + num_pages['data-total-pages'], self.root_dir / f"page{int(num_pages['data-total-pages'])}.html") - logging.info(f"downloading {len(algs_urls)} algs html files") + logger.info(f"downloading {len(algs_urls)} algs html files") cert_processing.process_parallel(FIPSCertificate.download_html_page, list(zip(algs_urls, algs_paths)), config.n_threads) diff --git a/sec_certs/helpers.py b/sec_certs/helpers.py index d2b96786..08b53497 100644 --- a/sec_certs/helpers.py +++ b/sec_certs/helpers.py @@ -520,7 +520,7 @@ def plot_dataframe_graph(data: Dict, label: str, file_name: str, density: bool = if log: sorted_data = pd_data.value_counts(ascending=True) - logging.info(sorted_data.where(sorted_data > 1).dropna()) + logger.info(sorted_data.where(sorted_data > 1).dropna()) def is_in_dict(target_dict, path): diff --git a/sec_certs/sample/fips.py b/sec_certs/sample/fips.py index 54d0e2bc..3cd8b38e 100644 --- a/sec_certs/sample/fips.py +++ b/sec_certs/sample/fips.py @@ -333,7 +333,7 @@ class FIPSCertificate(Certificate, ComplexSerializableType): html_items_found['vendor'] = vendor_string if html_items_found['vendor'] == '': - logger.warning(f"WARNING: NO VENDOR FOUND {current_file}") + logger.warning(f"NO VENDOR FOUND {current_file}") @staticmethod def parse_lab(current_div: Tag, html_items_found: Dict, current_file: Path): @@ -344,10 +344,10 @@ class FIPSCertificate(Certificate, ComplexSerializableType): 'div', 'panel-body').children)[2].strip().split('\n')[1].strip() if html_items_found['lab'] == '': - logger.warning(f"WARNING: NO LAB FOUND {current_file}") + logger.warning(f"NO LAB FOUND {current_file}") if html_items_found['nvlap_code'] == '': - logger.warning(f"WARNING: NO NVLAP CODE FOUND {current_file}") + logger.warning(f"NO NVLAP CODE FOUND {current_file}") @staticmethod def parse_related_files(current_div: Tag, html_items_found: Dict): @@ -580,7 +580,7 @@ class FIPSCertificate(Certificate, ComplexSerializableType): MAX_ALLOWED_MATCH_LENGTH = 300 match_len = len(match) if match_len > MAX_ALLOWED_MATCH_LENGTH: - print('WARNING: Excessive match with length of {} detected for rule {}'.format( + logger.warning('Excessive match with length of {} detected for rule {}'.format( match_len, rule)) if match not in items_found[rule_str]: |
