Skip to content

Commit 23308a6

Browse files
committed
updates/refactoring related to PR#16
1 parent a990bb2 commit 23308a6

7 files changed

Lines changed: 45 additions & 530 deletions

File tree

docs/examples/ca_api_example_basic.ipynb

Lines changed: 21 additions & 453 deletions
Large diffs are not rendered by default.

src/codeaudit/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
# SPDX-FileCopyrightText: 2025-present Maikel Mardjan - https://nocomplexity.com/
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
4-
from importlib.metadata import version
5-
6-
__version__ = version("codeaudit")

src/codeaudit/api_interfaces.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
import platform
2020
from collections import Counter
2121
from pathlib import Path
22+
from importlib.metadata import version
2223

2324
import altair as alt
2425
import pandas as pd
2526

2627

27-
from codeaudit import __version__
2828
from codeaudit.checkmodules import (
2929
check_module_vulnerability,
3030
get_all_modules,
@@ -48,9 +48,9 @@
4848
)
4949

5050

51-
def version():
51+
def version_info():
5252
"""Returns the version of Python Code Audit"""
53-
ca_version = __version__
53+
ca_version = version("codeaudit")
5454
return {"name": "Python_Code_Audit", "version": ca_version}
5555

5656

@@ -113,7 +113,7 @@ def filescan(input_path, nosec=False):
113113
"""
114114
file_output = {}
115115
file_path = Path(input_path)
116-
ca_version_info = version()
116+
ca_version_info = version_info()
117117
now = datetime.datetime.now()
118118
timestamp_str = now.strftime("%Y-%m-%d %H:%M")
119119
output = ca_version_info | {"generated_on": timestamp_str}
@@ -403,7 +403,7 @@ def get_default_validations():
403403

404404
def _generation_info():
405405
"""Internal function to retrieve generation info for APIs output"""
406-
ca_version_info = version()
406+
ca_version_info = version_info()
407407
now = datetime.datetime.now()
408408
timestamp_str = now.strftime("%Y-%m-%d %H:%M")
409409
output = ca_version_info | {"generated_on": timestamp_str}

src/codeaudit/codeaudit.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
"""
1515

1616
import sys
17+
from importlib.metadata import version
1718

1819
import fire # for working CLI with this PoC-thing (The Google way)
1920

20-
from codeaudit import __version__
21-
2221
from codeaudit.reporting import (
2322
overview_report,
2423
report_implemented_tests,
2524
report_module_information,
2625
scan_report,
2726
)
2827

28+
29+
CA_VERSION = version("codeaudit")
30+
2931
codeaudit_ascii_art = r"""
3032
----------------------------------------------------
3133
_ __ _
@@ -37,7 +39,7 @@
3739

3840
def display_version():
3941
"""Prints the module version. Or use codeaudit [-v] [--v] [-version] or [--version]."""
40-
print(f"version: {__version__}")
42+
print(f"version: {CA_VERSION}")
4143

4244

4345
def display_help():

src/codeaudit/privacy_lint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import datetime
1717
import re
1818
from importlib.resources import files
19+
from importlib.metadata import version
1920
from pathlib import Path
2021

21-
# from codeaudit.api_interfaces import version
22-
from codeaudit import __version__
22+
2323
from codeaudit.filehelpfunctions import (
2424
collect_python_source_files,
2525
get_filename_from_path,
@@ -63,7 +63,7 @@ def data_egress_scan(input_path):
6363
"""
6464
file_output = {}
6565
file_path = Path(input_path)
66-
ca_version_info = {"name": "Python_Code_Audit", "version": __version__}
66+
ca_version_info = {"name": "Python_Code_Audit", "version": version("codeaudit")}
6767
now = datetime.datetime.now()
6868
timestamp_str = now.strftime("%Y-%m-%d %H:%M")
6969
output = ca_version_info | {"generated_on": timestamp_str}

src/codeaudit/pypi_package_scan.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
from urllib.error import HTTPError, URLError
2222
from urllib.request import Request, urlopen
2323

24-
from codeaudit import __version__
24+
from importlib.metadata import version
25+
26+
CA_VERSION = version("codeaudit")
2527

2628
NOCX_HEADERS = {
27-
"user-agent": f"Python Code Audit /{__version__} (https://github.com/nocomplexity/codeaudit)",
29+
"user-agent": f"Python Code Audit /{CA_VERSION} (https://github.com/nocomplexity/codeaudit)",
2830
"Accept": "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
2931
"Accept-Encoding": "gzip, deflate,br",
3032
"Connection": "keep-alive",

src/codeaudit/reporting.py

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from pathlib import Path
1919
import sys
2020

21+
from importlib.metadata import version
22+
2123
import pandas as pd
2224
import html
2325
import datetime
@@ -44,14 +46,16 @@
4446
get_imported_modules_by_file,
4547
)
4648
from codeaudit.htmlhelpfunctions import json_to_html, dict_list_to_html_table
47-
from codeaudit import __version__
49+
4850
from codeaudit.pypi_package_scan import get_pypi_download_info, get_package_source
4951
from codeaudit.privacy_lint import data_egress_scan, has_privacy_findings
5052
from codeaudit.suppression import filter_sast_results
5153
from codeaudit.api_interfaces import _collect_issue_lines
5254

5355
from importlib.resources import files
5456

57+
CA_VERSION = version("codeaudit")
58+
5559
PYTHON_CODE_AUDIT_TEXT = '<a href="https://github.com/nocomplexity/codeaudit" target="_blank"><b>Python Code Audit</b></a>'
5660
DISCLAIMER_TEXT = (
5761
"<p><b>Disclaimer:</b> <i>This SAST tool "
@@ -769,7 +773,7 @@ def create_htmlfile(html_input, outputfile):
769773

770774
now = datetime.datetime.now()
771775
timestamp_str = now.strftime("%Y-%m-%d %H:%M")
772-
code_audit_version = __version__
776+
code_audit_version = CA_VERSION
773777

774778
output += (
775779
f"<p>This Python security report was created on: <b>{timestamp_str}</b> with "
@@ -806,64 +810,6 @@ def create_htmlfile(html_input, outputfile):
806810
print("=====================================================================\n")
807811

808812

809-
# def create_htmlfile(html_input,outputfile):
810-
# """ Creates a clean html file based on html input given """
811-
# # Read CSS from the file - So it is included in the reporting HTML file
812-
813-
# with open(SIMPLE_CSS_FILE, 'r') as css_file:
814-
# css_content = css_file.read()
815-
# # Start building the HTML
816-
# output = '<!DOCTYPE html><html lang="en-US"><head>'
817-
# output += '<meta charset="UTF-8"/>'
818-
# output += '<title>Python_Code_Audit_SecurityReport</title>'
819-
# # Inline CSS inside <style> block
820-
# output += f'<style>\n{css_content}\n</style>'
821-
# output += '<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>' # needed for altair plots
822-
# output += '<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>' # needed for altair plots
823-
# output += '<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>' # needed for altair plots
824-
# output += '</head><body>'
825-
# output += '<div class="container">'
826-
# output += html_input
827-
# now = datetime.datetime.now()
828-
# timestamp_str = now.strftime("%Y-%m-%d %H:%M")
829-
# code_audit_version = __version__
830-
# output += (
831-
# f"<p>This Python security report was created on: <b>{timestamp_str}</b> with "
832-
# + PYTHON_CODE_AUDIT_TEXT
833-
# + f" version <b>{code_audit_version}</b></p>"
834-
# )
835-
# output += '<hr>'
836-
# output += '<footer>'
837-
# output += (
838-
# '<div class="footer-links">'
839-
# 'Check the <a href="https://nocomplexity.com/documents/codeaudit/intro.html" '
840-
# 'target="_blank">documentation</a> for help on found issues.<br>'
841-
# 'Codeaudit is made with <span class="heart">&#10084;</span> by cyber security '
842-
# 'professionals who advocate for <a href="https://nocomplexity.com/simplify-security/" target="_blank">open simple security solutions</a>.<br>'
843-
# '<a href="https://nocomplexity.com/documents/codeaudit/CONTRIBUTE.html" target="_blank">Join the community</a> and contribute to make this tool better!'
844-
# "</div>"
845-
# )
846-
# output += "</footer>"
847-
# output += '</div>' #base container
848-
# output += '</body></html>'
849-
# # Now create the HTML output file
850-
# with open(outputfile, 'w') as f:
851-
# f.write(output)
852-
# current_directory = os.getcwd()
853-
# # Get the directory of the output file (if any)
854-
# directory_for_output = os.path.dirname(os.path.abspath(outputfile))
855-
# filename_only = os.path.basename(outputfile)
856-
# # Determine the effective directory to use in the file URL
857-
# if not directory_for_output or directory_for_output == current_directory:
858-
# file_url = f'file://{current_directory}/{filename_only}'
859-
# else:
860-
# file_url = f'file://{directory_for_output}/{filename_only}'
861-
# # Print the result
862-
# print("\n=====================================================================")
863-
# print(f'Code Audit report file created!\nPaste the line below directly into your browser bar:\n\t{file_url}\n')
864-
# print("=====================================================================\n")
865-
866-
867813
def extract_altair_html(plot_html):
868814
match = re.search(r"<body[^>]*>(.*?)</body>", plot_html, re.DOTALL | re.IGNORECASE)
869815
if match:
@@ -955,7 +901,7 @@ def report_implemented_tests(filename=DEFAULT_OUTPUT_FILE):
955901
number_of_test = len(df_checks)
956902

957903
output += df_checks_sorted.to_html(escape=False, index=False)
958-
code_audit_version = __version__
904+
code_audit_version = CA_VERSION
959905
output += "<br>"
960906
output += (
961907
f"<p>Number of implemented security validations:<b>{number_of_test}</b></p>"

0 commit comments

Comments
 (0)