スタンドアロン/開発実行での output ディレクトリ解決を一元化#21
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 135c05b934
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return executable_path.parent | ||
|
|
||
| # Development mode: src/leaf_contour_efd/utils/paths.py -> repo root | ||
| return Path(__file__).resolve().parents[3] |
There was a problem hiding this comment.
Resolve output path to a user-writable location
This fallback makes output/ live next to the installed binaries/source tree (sys.executable parent in frozen mode, __file__.parents[3] otherwise), which is often read-only (e.g., /Applications, Program Files, or system site-packages). Because get_output_dir() immediately runs mkdir(...) and is invoked while widgets are constructed, the app can fail at startup with PermissionError before any user action in those install contexts.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
@codex thank you. How can we avoid this issue? Please fix.
There was a problem hiding this comment.
Summary
- Updated output path resolution to always have a user-writable fallback via
appdirs.user_data_dir("leaf_contour_efd"), preventing startup-timePermissionErrorwhen installed in read-only locations. src/leaf_contour_efd/utils/paths.pyL8-L16 - Added a writability probe for development mode so repo-root output is only used when it is actually writable; otherwise it falls back to user data automatically. src/leaf_contour_efd/utils/paths.pyL18-L48
- Hardened
get_output_dir(...)with a defensive retry path: ifmkdir(...)fails in the primary base, it transparently recreates the same output path under the user-writable base. src/leaf_contour_efd/utils/paths.pyL51-L69 - Committed the fix on the current branch (
027c3f9) and created a follow-up PR message via themake_prtool summarizing the change set on top of the original PR.
Testing
- ✅
python3 -m compileall src/leaf_contour_efd/utils/paths.py
Motivation
This PR fixes an issue where the
outputdirectory for distributed applications (frozen/.app/.exe) was created relative to the current working directory (CWD) or directly under the user’s home directory at launch.The goal is to eliminate CWD-dependent behavior while preserving the convenience of the current behavior during development.
Description
A new utility module,
leaf_contour_efd.utils.paths, was added to centralize output path resolution. It providesget_output_base_dir()andget_output_dir(...).For frozen builds, the base path is resolved relative to
sys.executable. When launched from inside a macOS.appbundle, the parent directory of the.appis used as the base path.sys._MEIPASSis intentionally not used as a writable destination.The default save paths in each widget and save routine were updated from relative
output/...paths toget_output_dir(...)-based paths. Directory creation is now explicitly guaranteed at each call site withmkdir(parents=True, exist_ok=True).Main changed files:
src/leaf_contour_efd/utils/paths.py(new)src/leaf_contour_efd/widgets/binarize_image.pysrc/leaf_contour_efd/widgets/calculate_efd.pysrc/leaf_contour_efd/widgets/clear_viewer.pysrc/leaf_contour_efd/widgets/crop_rectangle.pysrc/leaf_contour_efd/widgets/extract_contour.pysrc/leaf_contour_efd/widgets/rotate_image.pyTesting
As a static verification step, I searched the codebase with
rgand confirmed that no direct relative paths such as./outputoroutput/remained.I also ran
compileallto byte-compile the Python modules and confirmed that all modules compiled successfully. The first attempt failed because of the environment’s.python-version, but recompilation with an available Python interpreter succeeded.The automated checks performed were limited to the above, and all of them passed. Runtime behavior and actual file creation should still be verified by running the application locally.
(Japanese)
Motivation
outputが起動時の CWD やホーム直下に作られてしまう問題を修正するため。cwd依存を排除することを目的とする。Description
leaf_contour_efd.utils.pathsを追加し、get_output_base_dir()とget_output_dir(...)を提供した。sys.executableを基準とし、macOS の.app内からの起動は.appの親フォルダを基準にする実装とし、sys._MEIPASSは書き込み先として使わない設計にした。output/...からget_output_dir(...)ベースへ置換し、呼び出し側でmkdir(parents=True, exist_ok=True)を保証するようにした。src/leaf_contour_efd/utils/paths.py(新規追加)、およびsrc/leaf_contour_efd/widgets/以下のbinarize_image.py,calculate_efd.py,clear_viewer.py,crop_rectangle.py,extract_contour.py,rotate_image.pyを修正して共通ヘルパを利用するようにした。Testing
rgによる検索で./output/output/等の直接相対パスが残っていないことを確認した(該当パターンは検出されなかった)。compileallを実行して全モジュールが問題なくコンパイルされることを確認した(最初は環境の.python-versionによる実行失敗が出たが、利用可能な Python を指定して再実行したところコンパイルは成功した)。Codex Task