Skip to content
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

Fix prov files not printing with links in prov index.html #937

Merged
merged 5 commits into from
Feb 14, 2025

Conversation

tomvothecoder
Copy link
Collaborator

@tomvothecoder tomvothecoder commented Feb 13, 2025

Description

  • Closes [Bug]: index.html under /prov missing provenance files #936
  • Consolidate logger messages for prov paths
    • Related files: e3sm_diags/e3sm_diags_driver.py, e3sm_diags/run.py
    • Before:
      2025-02-13 13:01:47,207 [INFO]: run.py(_log_diagnostic_run_info:157) >> 
      ================================================================================
      E3SM Diagnostics Run
      --------------------
      Timestamp: 2025-02-13 13:01:47
      Version Info: branch main with commit 4e3571144ea0ed1d74c97d97aedd33172a1069b9
      Log Filepath: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files-main/prov/e3sm_diags_run.log
      ================================================================================
      
      2025-02-13 13:01:47,210 [INFO]: e3sm_diags_driver.py(save_provenance:159) >> Created provenance index HTML file at: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files-main/prov/index.html
      2025-02-13 13:01:50,673 [INFO]: e3sm_diags_driver.py(_save_env_yml:58) >> Saved environment yml file to: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files-main/prov/environment.yml
      2025-02-13 13:01:50,674 [INFO]: e3sm_diags_driver.py(_save_parameter_files:69) >> Saved command used to: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files-main/prov/cmd_used.txt
      2025-02-13 13:01:50,677 [INFO]: e3sm_diags_driver.py(_save_parameter_files:99) >> Saved cfg file to: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files-main/prov/run.cfg
      2025-02-13 13:01:50,679 [INFO]: e3sm_diags_driver.py(_save_python_script:133) >> Saved Python script to: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files-main/prov/ipykernel_launcher.py
    • After
     2025-02-13 12:55:27,447 [INFO]: e3sm_diags_driver.py(_log_diagnostic_run_info:509) >> 
     ================================================================================
     E3SM Diagnostics Run
     --------------------
     Timestamp: 2025-02-13 12:55:27
     Version Info: branch bug/936-missing-prov-files with commit 865a7fa6341d33a798ab0225fced4a2eeef7e37c
     Log Path: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files/prov/e3sm_diags_run.log
     Parameter Files Path: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files/prov/run.cfg
     Python Script Path: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files/prov/ipykernel_launcher.py
     Environment YML Path: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files/prov/environment.yml
     Provenance Index HTML Path: /lcrc/group/e3sm/public_html/cdat-migration-fy24/936-missing-prov-files/prov/index.html
     ================================================================================
  • Suppress error for git command when the package is a version build
    • Related files: e3sm_diags/run.py
    • Before:
      fatal: not a git repository (or any parent up to mount point /lcrc)
      Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
          2025-02-13 13:16:37,970 [INFO]: run.py(_log_diagnostic_run_info:157) >> 
      ================================================================================
      E3SM Diagnostics Run
      --------------------
    • After:
       2025-02-13 13:16:37,970 [INFO]: run.py(_log_diagnostic_run_info:157) >> 
       ================================================================================
       E3SM Diagnostics Run
       --------------------

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

If applicable:

  • New and existing unit tests pass with my changes (locally and CI/CD build)
  • I have added tests that prove my fix is effective or that my feature works
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have noted that this is a breaking change for a major release (fix or feature that would cause existing functionality to not work as expected)

- Update `save_provenance()` to generate `index.html` last
- Update order of `save_pronenance()` child functions for readability
@tomvothecoder tomvothecoder self-assigned this Feb 13, 2025
@tomvothecoder tomvothecoder added the bug Bug fix (will increment patch version) label Feb 13, 2025
Copy link
Collaborator Author

@tomvothecoder tomvothecoder left a comment

Choose a reason for hiding this comment

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

Hey @chengzhuzhang, this PR is ready for review.

The description contains all of the useful information about the changes. Thanks!

Comment on lines 75 to 86
prov_dir = os.path.join(results_dir, "prov")

paths: ProvPaths = {
"log_path": os.path.join(prov_dir, LOG_FILENAME),
"parameter_files_path": None,
"python_script_path": None,
"env_yml_path": None,
"index_html_path": None,
}

paths["parameter_files_path"] = _save_parameter_files(prov_dir, parser)
paths["python_script_path"] = _save_python_script(prov_dir, parser)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Prov paths are now captured here to consolidate them under _log_diagnostic_run_info().

f.write(output.decode("utf-8"))
logger.info("Saved environment yml file to: {}".format(fnm))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Remove unnecessary logger call.

f.write(cmd_used)
logger.info("Saved command used to: {}".format(fnm))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Remove unnecessary logger call.

Comment on lines +153 to +170
filepath = args.parameters
elif hasattr(args, "other_parameters") and args.other_parameters:
filepath = args.other_parameters[0]

if not os.path.isfile(filepath):
logger.warning("File does not exist: {}".format(filepath))
else:
with open(filepath, "r") as f:
contents = "".join(f.readlines())

# Remove any path, just keep the filename.
new_filepath = filepath.split("/")[-1]
new_filepath = os.path.join(results_dir, new_filepath)

with open(new_filepath, "w") as f:
f.write(contents)

return new_filepath
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Refactor this code to remove repeating code.

subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
cwd=os.path.dirname(__file__),
stderr=subprocess.DEVNULL,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suppress git command error if a versioned release is installed a conda environment instead.

subprocess.check_output(
["git", "rev-parse", "HEAD"],
cwd=os.path.dirname(__file__),
stderr=subprocess.DEVNULL,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suppress git command error if a versioned release is installed a conda environment instead.

Comment on lines -113 to -166
def _log_diagnostic_run_info(self, log_path: str):
"""Logs information about the diagnostic run.

This method is useful for tracking the provenance of the diagnostic run
and understanding the context of the diagnostic results.

It logs the following information:
- Timestamp of the run
- Version information (Git branch and commit hash or module version)

Parameters
----------
log_path : str
The path to the log file, which is stored in the `results_dir`
sub-directory called "prov".

Notes
-----
The version information is retrieved from the current Git branch and
commit hash. If the Git information is not available, it falls back
to the version defined in the `e3sm_diags` module.
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

try:
branch_name = (
subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
cwd=os.path.dirname(__file__),
)
.strip()
.decode("utf-8")
)
commit_hash = (
subprocess.check_output(
["git", "rev-parse", "HEAD"], cwd=os.path.dirname(__file__)
)
.strip()
.decode("utf-8")
)
version_info = f"branch {branch_name} with commit {commit_hash}"
except subprocess.CalledProcessError:
version_info = f"version {e3sm_diags.__version__}"

logger.info(
f"\n{'=' * 80}\n"
f"E3SM Diagnostics Run\n"
f"{'-' * 20}\n"
f"Timestamp: {timestamp}\n"
f"Version Info: {version_info}\n"
f"Log Filepath: {log_path}\n"
f"{'=' * 80}\n"
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved to e3sm_diags/e3sm_diags_driver.py

@tomvothecoder tomvothecoder changed the title Fix prov files not printing in prov index.html Fix prov files not printing with links in prov index.html Feb 13, 2025
@tomvothecoder
Copy link
Collaborator Author

Note, build is failing because of Codacy. I disabled the web hook for now. We can consider using it later.

@chengzhuzhang chengzhuzhang self-requested a review February 13, 2025 20:45
Copy link
Contributor

@chengzhuzhang chengzhuzhang left a comment

Choose a reason for hiding this comment

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

Thanks for a quick, fix! I tested with PR with one run and all the provenance files are captured.

@chengzhuzhang
Copy link
Contributor

Maybe a small improvement to make: for the cmd_used file, I think it

@tomvothecoder
Copy link
Collaborator Author

@chengzhuzhang I think you accidentally closed this.

Maybe a small improvement to make: for the cmd_used file, I think it

What should be improved here? I think I launched the script using VS Code interactive console so it looks different compared to the normal python run_script.py.

@tomvothecoder tomvothecoder reopened this Feb 13, 2025
@chengzhuzhang
Copy link
Contributor

@chengzhuzhang I think you accidentally closed this.

Maybe a small improvement to make: for the cmd_used file, I think it

What should be improved here? I think I launched the script using VS Code interactive console so it looks different compared to the normal python run_script.py.

in my test, I saw this: https://web.lcrc.anl.gov/public/e3sm/zhang40/cdat-migration-fy24/25-02-13-branch-viewer-fix937/prov/cmd_used.txt.
Would be nice to add python in the command line, but I figure it may not be necessary.

@tomvothecoder
Copy link
Collaborator Author

@chengzhuzhang I think you accidentally closed this.

Maybe a small improvement to make: for the cmd_used file, I think it

What should be improved here? I think I launched the script using VS Code interactive console so it looks different compared to the normal python run_script.py.

in my test, I saw this: web.lcrc.anl.gov/public/e3sm/zhang40/cdat-migration-fy24/25-02-13-branch-viewer-fix937/prov/cmd_used.txt. Would be nice to add python in the command line, but I figure it may not be necessary.

The _save_parameter_files() function aggregates the args used. I think it is agnostic to how e3sm_diags is executed (e.g., Python script, command line, etc.). I was thinking of prependingpython, but it wouldn't be correct if e3sm_diags is executed via command line.

We can think of printing this nicely in another issue if it is easy and desired.

Merging this PR now.

@tomvothecoder tomvothecoder merged commit dcd5719 into main Feb 14, 2025
12 of 13 checks passed
@tomvothecoder tomvothecoder deleted the bug/936-missing-prov-files branch February 14, 2025 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug fix (will increment patch version)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: index.html under /prov missing provenance files
2 participants