CSV Export
Relevant source files
The following files were used as context for generating this wiki page:
The CSV Export feature in the Duplicate Code Detection Tool provides a spreadsheet-compatible output format for code similarity analysis. It allows developers and automated systems to save detailed duplication reports into .csv files, facilitating offline analysis, long-term tracking of technical debt, and integration with third-party data visualization tools.
This feature is primary controlled via the Command Line Interface (CLI) but is supported by the underlying processing logic that handles file paths, similarity percentages, and optional Line of Code (LoC) metrics.
Sources: README.md:99-105, duplicate_code_detection.py:171-176
Architecture and Data Flow
The CSV export logic is integrated directly into the run function of the detection engine. After the tool calculates similarities using the Gensim-based TF-IDF model, it iterates through the results and writes them to the specified file path if the --csv-output argument is provided.
The flow below illustrates how similarity data is transformed into a CSV file:
The export process evaluates two distinct schemas based on whether the --show-loc flag is active.
Sources: duplicate_code_detection.py:233-247, duplicate_code_detection.py:341-377
Configuration and Parameters
The CSV export is triggered by specific CLI arguments and library parameters. While the library's high-level DuplicateCodeDetector class primarily focuses on returning structured Python objects, the core run function handles the physical writing to the disk.
| Parameter | Type | Default | Description |
|---|---|---|---|
csv_output | str | "" | The file system path where the CSV report will be saved. |
show_loc | bool | False | When True, adds line count columns for both compared files to the CSV. |
ignore_threshold | int | 0 | Excludes similarities below this percentage from the CSV rows. |
project_root_dir | str | "" | Filters out the root path from file names in the CSV for readability. |
Sources: duplicate_code_detection.py:171-176, duplicate_code_detection.py:220-230, core.py:141-160
CSV Data Schemas
The output structure of the CSV varies depending on the inclusion of Lines of Code (LoC) data.
Standard Schema
If --show-loc is not used, the CSV contains three columns:
- File A: The source file being analyzed.
- File B: The target file found to be similar.
- Similarity: The calculated similarity percentage (0.0 to 100.0).
Extended Schema (with LoC)
When --show-loc is enabled, the tool adds context regarding file size:
- File A: Source file path.
- #LoC A: Line count of Source file.
- File B: Target file path.
- #LoC B: Line count of Target file.
- Similarity: Similarity percentage.
Sources: duplicate_code_detection.py:343-377
Implementation Details
The implementation utilizes the standard Python csv module. The tool ensures that file paths are normalized and relative to the project_root_dir if specified, making the exported reports portable across different environments.
# snippet from duplicate_code_detection.py:341-358
if csv_output:
with open(csv_output, "w") as csv_file:
writer = csv.writer(csv_file)
if show_loc:
writer.writerow(["File A", "#LoC A", "File B", "#LoC B", "Similarity"])
for first_file in code_similarity:
for second_file in code_similarity[first_file]:
if second_file != loc_label:
writer.writerow([
first_file,
get_loc_to_print(get_loc_count(os.path.join(project_root_dir, first_file))),
second_file,
get_loc_to_print(get_loc_count(os.path.join(project_root_dir, second_file))),
code_similarity[first_file][second_file][similarity_label],
])
Sources: duplicate_code_detection.py:341-358
Key Components
duplicate_code_detection.run: The main execution entry point where the CSV writing logic resides. Sources: duplicate_code_detection.py:180get_loc_count: A utility function used during CSV generation to retrieve line counts for the#LoCcolumns. Sources: duplicate_code_detection.py:102code_similarity: AnOrderedDictthat stores similarity results before they are serialized to CSV. Sources: duplicate_code_detection.py:255
Usage via CLI
To generate a CSV report from the command line, the --csv-output flag must be followed by a valid filename.
# Basic CSV export
duplicate-code-detection -d src/ --csv-output duplication_report.csv
# Export with Line of Code information and an ignore threshold
duplicate-code-detection -d src/ --csv-output detailed_report.csv --show-loc --ignore-threshold 20
Sources: README.md:99-105
Summary
The CSV Export feature provides a robust method for persisting code similarity data in a structured, tabular format. By supporting both basic and LoC-enriched schemas, it enables flexible reporting suitable for both human review in spreadsheet software and automated quality gate processing.