certflow.utils.path_utils module¶
路径工具模块 - 提供路径常量和辅助函数
本模块用于跨环境(开发环境 / PyInstaller / Nuitka 打包环境)的路径管理。
- 使用场景:
开发环境:从项目根目录获取 config/、scripts/、src/ 等路径
打包环境:从 EXE 所在目录获取 config/ 路径
- 环境检测优先级:
环境变量 CERTFLOW_ROOT(最高优先级)
sys.frozen / sys._MEIPASS2(打包环境自动检测)
开发环境:向上查找 pyproject.toml 或 config/config.yaml
示例
>>> from certflow.utils.path_utils import PROJECT_ROOT, CONFIG_PATH
>>> print(PROJECT_ROOT)
/path/to/project
>>> print(CONFIG_PATH)
/path/to/project/config/config.yaml
- certflow.utils.path_utils.PROJECT_ROOT: Final[Path] = PosixPath('/workspace')¶
项目根目录路径(优先使用环境变量 CERTFLOW_ROOT)
- certflow.utils.path_utils.CONFIG_DIR: Path = PosixPath('/workspace/config')¶
配置文件目录路径(开发环境:项目根目录/config,打包环境:EXE 同级或上级 config)
- certflow.utils.path_utils.SCRIPTS_DIR: Path = PosixPath('/workspace/scripts')¶
scripts/ 目录路径(仅在开发环境下有效,打包环境返回空 Path)
- certflow.utils.path_utils.BUILDER_DIR: Path = PosixPath('/workspace/scripts/builder')¶
scripts/builder/ 目录路径(仅在开发环境下有效,打包环境返回空 Path)
- certflow.utils.path_utils.CONFIG_PATH: Path = PosixPath('/workspace/config/config.yaml')¶
主配置文件完整路径(config/config.yaml)
- certflow.utils.path_utils.BUILD_CONFIG_PATH: Path = PosixPath('.')¶
构建配置文件完整路径(仅在开发环境下有效,打包环境返回空 Path)
- certflow.utils.path_utils.is_frozen()[源代码]¶
检测当前程序是否在打包环境中运行。
支持 PyInstaller (sys.frozen) 和 Nuitka (sys._MEIPASS2)。
- 返回:
True 表示在打包环境中,False 表示在开发环境中。
- 返回类型:
示例
>>> if is_frozen(): ... print("Running as packaged executable") ... else: ... print("Running in development environment")
- certflow.utils.path_utils.get_exe_dir()[源代码]¶
获取可执行文件所在目录。
仅在打包环境下有效,开发环境下返回当前工作目录。
- 返回:
EXE 所在目录路径(打包环境)或当前工作目录(开发环境)。
- 返回类型:
Path
示例
>>> exe_dir = get_exe_dir() >>> print(exe_dir) /path/to/dist
- certflow.utils.path_utils.get_project_root()[源代码]¶
获取项目根目录。
- 按以下优先级检测:
环境变量 CERTFLOW_ROOT
打包环境:EXE 所在目录
开发环境:从当前文件向上查找 pyproject.toml 或 config/config.yaml
- 返回:
项目根目录路径。
- 返回类型:
Path
示例
>>> root = get_project_root() >>> print(root) /path/to/project
- certflow.utils.path_utils.get_config_dir()[源代码]¶
获取配置文件目录。
- 打包环境:
优先返回 EXE 同级 config 目录(推荐结构)
回退到 EXE 上级 config 目录(兼容旧结构)
- 开发环境:
从项目根目录返回 config/ 目录
- 返回:
config 目录路径。
- 返回类型:
Path
示例
>>> config_dir = get_config_dir() >>> print(config_dir) /path/to/project/config
- certflow.utils.path_utils.get_config_path(filename='config.yaml')[源代码]¶
获取配置文件的完整路径。
- 参数:
filename (str) -- 配置文件名,默认为 config.yaml。
- 返回:
配置文件的完整路径。
- 返回类型:
Path
示例
>>> config_path = get_config_path() >>> print(config_path) /path/to/project/config/config.yaml >>> custom_path = get_config_path("custom.yaml") >>> print(custom_path) /path/to/project/config/custom.yaml
- certflow.utils.path_utils.get_build_config_path()[源代码]¶
获取构建配置文件路径。
仅在开发环境下有效,打包环境返回空 Path。
搜索顺序:
scripts/builder/build_config.yaml(优先)
项目根目录/build_config.yaml
- 返回:
构建配置文件路径,未找到时返回空 Path。
- 返回类型:
Path
示例
>>> build_config = get_build_config_path() >>> if build_config: ... print(f"Found build config: {build_config}") ... else: ... print("Build config not found")
- certflow.utils.path_utils.get_scripts_dir()[源代码]¶
获取 scripts/ 目录路径。
仅在开发环境下有效,打包环境返回空 Path。
- 返回:
scripts 目录路径,打包环境返回空 Path。
- 返回类型:
Path
示例
>>> scripts_dir = get_scripts_dir() >>> if scripts_dir: ... print(f"Scripts directory: {scripts_dir}") ... else: ... print("Running in packaged environment, scripts not available")
- certflow.utils.path_utils.get_builder_dir()[源代码]¶
获取 scripts/builder/ 目录路径。
仅在开发环境下有效,打包环境返回空 Path。
- 返回:
builder 目录路径,打包环境返回空 Path。
- 返回类型:
Path
示例
>>> builder_dir = get_builder_dir() >>> if builder_dir: ... print(f"Builder directory: {builder_dir}") ... else: ... print("Running in packaged environment, builder not available")
- certflow.utils.path_utils.setup_path(project_root=None)[源代码]¶
将 src/ 目录添加到 sys.path。
仅在开发环境下执行,打包环境自动跳过(源码已打包进 EXE)。
- 参数:
project_root (Path | None) -- 项目根目录,为 None 时自动使用 PROJECT_ROOT。 仅在需要手动指定根目录时传入。
- 返回:
None
- 返回类型:
None
示例
>>> # 自动检测项目根目录并添加 src/ 到 sys.path >>> setup_path() >>> >>> # 手动指定项目根目录 >>> setup_path(Path("/custom/project/root")) >>> >>> # 在打包环境中,此函数无任何效果 >>> if not is_frozen(): ... setup_path()