Repository navigation
refactor: build paths with pathlib instead of os.path - #153
Conversation
|
This one is dedicated to you @GhostofGoes |
| cmd_src = str(Path(self.root_dir) / self.exp_name / cmd_file) | ||
| cmd_dst = str(PurePath("/tmp/miniccc/files") / self.exp_name / cmd_file) |
There was a problem hiding this comment.
Why not make these Path objects? And then convert to strings in the places they need to be strings? Right now they're becoming strings, then being converted to Paths in a bunch of places.
There was a problem hiding this comment.
Done. cmd_src and cmd_dst stay Path/PurePath, and str() only happens at the minimega call.
| dst=os.path.join( | ||
| self.base_dir, os.path.basename(client["client_log_path"]) | ||
| dst=str( | ||
| Path(self.base_dir) |
There was a problem hiding this comment.
can self.base_dir be made into a Path?
There was a problem hiding this comment.
Done. self.base_dir is a Path in ComponentBase, so the wrappers are gone.
| e_dir = Path(self.exp_dir) / "opcexport" | ||
| if not e_dir.exists(): | ||
| e_dir.mkdir() | ||
| opc_variables_file = str(e_dir / "opc_variables.json") |
There was a problem hiding this comment.
Done. e_dir and opc_variables_file are Paths, and exp_dir is now a Path too.
| script = config_snort["script"] | ||
| executor = config_snort["executor"] | ||
|
|
||
| logger.info(f"copying {os.path.basename(script)} to {hostname}") |
There was a problem hiding this comment.
the conversion once, store in a variable, instead of doing 4 times
There was a problem hiding this comment.
Done. script_name is computed once and reused.
| script = scripts["trafficServer"] | ||
|
|
||
| logger.info(f"copying {os.path.basename(script)} to {hostname}") | ||
| logger.info(f"copying {PurePath(script).name} to {hostname}") |
There was a problem hiding this comment.
Done. script_name is a variable here too.
| self.metadata: Box | None = self.extract_metadata() | ||
|
|
||
| self.root_dir: str = os.path.join(PHENIX_DIR, "images") | ||
| self.root_dir: str = str(Path(PHENIX_DIR) / "images") |
There was a problem hiding this comment.
Make this a Path object
There was a problem hiding this comment.
Done. root_dir, files_dir and base_dir are all Paths.
| self.root_dir: str = os.path.join(PHENIX_DIR, "images") | ||
| self.root_dir: str = str(Path(PHENIX_DIR) / "images") | ||
| self.files_dir: str = os.getenv( | ||
| "PHENIX_FILES_DIR", os.path.join(self.root_dir, self.exp_name, "files") |
There was a problem hiding this comment.
make all of these path objects yeah?
There was a problem hiding this comment.
exp_dir is a Path now as well. I also dropped the leftover Path(self.base_dir) wraps in cc and ssh.
1df233e to
b887c56
Compare
b887c56 to
468a5f7
Compare
|
@GhostofGoes Thanks, all addressed in 468a5f7:
Still one commit, still independent of the others. All five open PRs merged together pass |
| utils.mm_recv(mm, hostname, f"/var/log/snort/{log}", str(log_path)) | ||
|
|
||
| if log == "snort.stats" and os.path.exists(f"{self.base_dir}/{log}"): | ||
| if log == "snort.stats" and log_path.exists(): |
There was a problem hiding this comment.
Use 'is_file()' for file existence checks
Mechanical os.path -> pathlib conversion for files that need no other change: the otsim config writer, the logger file sink, the SCORCH base class and the caldera, hoststats, iperf, kafka listener, opcexport, snort, trafficgen and vmstats components, and the wind_turbine app. The SCORCH base class now keeps root_dir, files_dir and base_dir as Path objects, so components join onto them directly and convert to str only at the minimega and file-transfer boundaries. The two components that built paths by string concatenation on base_dir (cc recv and ssh SFTP downloads) now join with pathlib instead. No behavior change. common/utils.py and the files whose path handling also gains validation are converted in the hardening PRs, next to that validation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016KAfcDUSerQCCWwBM9BxAo
468a5f7 to
2d49208
Compare
|
@adamsrnmsu @nblair2 @glattercj to make sure internal apps are updated to use pathlib after this one is merged |
|
It looks like this is using the syntactic surgar |
Description
A mechanical
os.path→pathlibconversion, limited to files that need no other change: the otsim config writer, the logger file sink, the SCORCH base class, the caldera, hoststats, iperf, kafka listener, opcexport, snort, trafficgen and vmstats components, and the wind_turbine app.The SCORCH base class now keeps
root_dir,files_dirandbase_dirasPathobjects. Components join onto them directly and convert tostronly where minimega or a file-transfer helper needs one. The two components that built paths by string concatenation onbase_dir(ccrecv andsshSFTP downloads) now join with pathlib. Those lines are written identically to #154's versions, so the two PRs still merge cleanly.No behavior change.
common/utils.py, SunSpec loading and the files whose path handling also gains validation are converted in #154 instead, next to the validation, becausesafe_join()returns aPath. That keeps the PRs from overlapping.Related Issues/PRs
One of four independent PRs: #152, #154, #155. Each is a single commit on
mainand they can merge in any order.Type of Change
refactor)Checklist
Testing
make checkandmake testinsrc/pythonon Python 3.12: 697 passed, the same asmain. I also merged all five open PRs (#152–#156) together:make checkis clean and 744 tests pass.Additional Notes
The vendored
sunspec/models/smdx/manifest.py, which ruff excludes, is deliberately left alone.🤖 Generated with Claude Code
https://claude.ai/code/session_016KAfcDUSerQCCWwBM9BxAo