Skip to content

standardize file copy to output #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

drbergman
Copy link
Collaborator

@drbergman drbergman commented Apr 29, 2025

Standardize how all files are copied to output. This serves several purposes:

  • ensures that the config file is copied
    • previously handled in user-defined main.cpp
    • now done in core as we had been doing with rules, ic cells, and ic substrates
  • optional default_basename argument in copy_file_to_output ensures standard file naming in output
    • ensures downstream tools can find the appropriate files, e.g. pcdl
    • does result in duplicate files in the output if the user-supplied name differs from the default base name, but this is minimal compared to the simulation data

Note: If a user retains the copy command in their main.cpp, this will just mean it is copied twice which will not cause errors.

@drbergman drbergman requested a review from Copilot April 29, 2025 18:16
Copilot

This comment was marked as resolved.

@drbergman drbergman force-pushed the copy-config-in-core branch from 3a2795f to 0c7b54b Compare April 29, 2025 18:27
Copy link
Collaborator Author

@drbergman drbergman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reviewed all the updated main.cpp's and they have all been properly updated. The episodes project has a unique structure, however, and is unchanged. Again, all this should mean is repeated copying for that project, but nothing excessive.

@drbergman drbergman changed the title copy config to output now part of core standardize file copy to output Apr 30, 2025
@drbergman drbergman requested a review from Copilot April 30, 2025 13:03
Copilot

This comment was marked as resolved.

- default names:
  - config: PhysiCell_settings.xml
  - rules: cell_rules.csv
  - ic cells: cells.csv
  - ic substrates: substrates.csv or substrates.mat
@drbergman drbergman force-pushed the copy-config-in-core branch from d853fa0 to ae0c2b7 Compare April 30, 2025 13:52
@drbergman drbergman self-assigned this May 22, 2025
@drbergman drbergman requested a review from Copilot May 22, 2025 13:49
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Standardize how configuration, microenvironment, geometry, and rule files are copied to the output directory by centralizing operations in copy_file_to_output with an optional default_basename parameter.

  • Removed redundant manual cp commands from all sample project main.cpp files.
  • Updated core utilities to accept a default_basename and invoke a second copy when needed.
  • Applied copy_file_to_output uniformly in modules for settings, microenvironment, cell data, and rules.

Reviewed Changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
sample_projects/worm/main.cpp Removed manual config copy
sample_projects/virus_macrophage/main.cpp Removed manual config copy
sample_projects/template/main.cpp Removed manual config copy
sample_projects/rules_sample/main.cpp Removed manual config copy
sample_projects/pred_prey_farmer/main.cpp Removed manual config copy
sample_projects/physimess/main.cpp Removed manual config copy
sample_projects/mechano/main.cpp Removed manual config copy
sample_projects/interactions/main.cpp Removed manual config copy
sample_projects/immune_function/main.cpp Removed manual config copy
sample_projects/heterogeneity/main.cpp Removed manual config copy
sample_projects/custom_division/main.cpp Removed manual config copy
sample_projects/cancer_immune/main-cancer_immune_3D.cpp Removed manual config copy
sample_projects/cancer_biorobots/main.cpp Removed manual config copy
sample_projects/biorobots/main.cpp Removed manual config copy
sample_projects/asymmetric_division/main.cpp Removed manual config copy
modules/PhysiCell_settings.cpp Added default_basename arguments to copy settings XML
modules/PhysiCell_geometry.cpp Added default_basename for cell CSV copy
core/PhysiCell_utilities.h Extended copy_file_to_output signature with default
core/PhysiCell_utilities.cpp Implemented double-copy logic with default_basename
core/PhysiCell_rules.cpp Applied default_basename for rule file copy
Comments suppressed due to low confidence (2)

core/PhysiCell_utilities.cpp:383

  • Using sprintf and system() with unvalidated filenames can lead to shell injection and portability issues; consider replacing this with std::filesystem::copy or another safe API.
sprintf(copy_command, "cp %s %s", filename.c_str(), output_filename.c_str());

core/PhysiCell_utilities.h:114

  • Add unit tests for copy_file_to_output covering both the single-copy and double-copy (with non-empty default_basename) paths to verify correct behavior.
void copy_file_to_output(const std::string &filename, const std::string &default_basename = "");


// copy the file to the output folder with the default basename
std::string default_output_filename = PhysiCell_settings.folder + "/" + default_basename;
sprintf(copy_command, "cp %s %s", filename.c_str(), default_output_filename.c_str());
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, this second sprintf + system() call risks command injection and may fail on non-Unix platforms; use a filesystem-native copy routine instead.

Copilot uses AI. Check for mistakes.

// copy the file to the output folder with the default basename
std::string default_output_filename = PhysiCell_settings.folder + "/" + default_basename;
sprintf(copy_command, "cp %s %s", filename.c_str(), default_output_filename.c_str());
(void)system(copy_command); // make it explicit that we are ignoring the return value
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of system() is ignored here, so failures go unnoticed; consider capturing and handling the result or throwing an exception on error.

Suggested change
(void)system(copy_command); // make it explicit that we are ignoring the return value
int ret_code = system(copy_command);
if (ret_code != 0) {
std::cerr << "Error: Failed to execute command: " << copy_command << std::endl;
}

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant