Skip to content

Commit 16a2bd0

Browse files
committed
fix: 启动器场景优先读内置的yaml
1 parent 84ed14c commit 16a2bd0

8 files changed

Lines changed: 58 additions & 18 deletions

File tree

deploy/OneDragon-Launcher.spec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ a = Analysis(
55
['..\\src\\zzz_od\\win_exe\\launcher.py'],
66
pathex=[],
77
binaries=[],
8-
datas=[],
8+
datas=[
9+
('../config/project.yml', 'resources/config'),
10+
('../config/repository.yml', 'resources/config'),
11+
],
912
hiddenimports=['_cffi_backend'],
1013
hookspath=[],
1114
hooksconfig={},

docs/develop/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ uv run pyinstaller --noconfirm --clean "OneDragon-Installer.spec"
8686

8787
### 3.2.启动器(原始)
8888

89-
使用spec打包,会自动生成种子文件
89+
使用 spec 打包。`project.yml``repository.yml` 会随启动器写入 `resources/config`;原始启动器创建环境上下文时显式开启包内配置优先,其他入口仍读取仓库配置。
9090

9191
```shell
9292
uv run pyinstaller --noconfirm --clean "OneDragon-Launcher.spec"

src/one_dragon/base/config/yaml_config.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def __init__(
1515
sub_dir: list[str] | None = None,
1616
sample: bool = False, copy_from_sample: bool = False,
1717
read_sample_only: bool = False,
18-
is_mock: bool = False
18+
is_mock: bool = False,
19+
prefer_bundled_config: bool = False,
1920
):
2021
self.instance_idx: int | None = instance_idx
2122
"""传入时 该配置为一个的脚本实例独有的配置"""
@@ -32,6 +33,9 @@ def __init__(
3233
self.is_mock: bool = is_mock
3334
"""mock情况下 不读取文件 也不会实际保存 用于测试"""
3435

36+
self._prefer_bundled_config: bool = prefer_bundled_config
37+
"""是否优先读取 PyInstaller 包内的配置"""
38+
3539
self._sample: bool = sample
3640
"""是否有sample文件"""
3741

@@ -68,6 +72,15 @@ def _get_yaml_file_paths(self) -> tuple[str | None, str | None, str | None]:
6872
if self._read_sample_only and os.path.exists(sample_yml_path):
6973
return sample_yml_path, sample_yml_path, None
7074

75+
if self._prefer_bundled_config:
76+
resource_path = os_utils.get_resource_path(
77+
*sub_dir,
78+
f'{self.module_name}.yml',
79+
prefer_bundled=True,
80+
)
81+
if resource_path != yml_path and os.path.exists(resource_path):
82+
return resource_path, yml_path, resource_path
83+
7184
# 指定文件存在时 直接使用
7285
if os.path.exists(yml_path):
7386
return yml_path, yml_path, None

src/one_dragon/base/operation/one_dragon_env_context.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,31 @@
1414

1515
class OneDragonEnvContext:
1616

17-
def __init__(self):
17+
def __init__(self, prefer_bundled_config: bool = False) -> None:
1818
"""
1919
存项目和环境信息的
2020
安装器可以使用这个减少引入依赖
2121
"""
2222
self.installer_dir: str | None = None
23+
self._prefer_bundled_config: bool = prefer_bundled_config
2324

2425
#------------------- 需要懒加载的都使用 @cached_property -------------------#
2526

2627
@cached_property
27-
def project_config(self):
28-
return ProjectConfig()
28+
def project_config(self) -> ProjectConfig:
29+
return ProjectConfig(
30+
prefer_bundled_config=self._prefer_bundled_config,
31+
)
2932

3033
@cached_property
3134
def env_config(self):
3235
return EnvConfig(self.repo_config)
3336

3437
@cached_property
3538
def repo_config(self) -> RepoConfig:
36-
return RepoConfig()
39+
return RepoConfig(
40+
prefer_bundled_config=self._prefer_bundled_config,
41+
)
3742

3843
@cached_property
3944
def download_service(self):

src/one_dragon/devtools/python_launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def run_python(app_path, no_windows: bool = True, args: list[str] | None = None,
331331
print_message(f"OneDragon 启动器 {__version__}", "INFO")
332332
cwd = verify_working_directory()
333333
from one_dragon.base.operation.one_dragon_env_context import OneDragonEnvContext
334-
ctx = OneDragonEnvContext()
334+
ctx = OneDragonEnvContext(prefer_bundled_config=True)
335335
configure_environment(ctx, cwd)
336336
fetch_latest_code(ctx)
337337
sync_dependencies(ctx)

src/one_dragon/envs/project_config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
class ProjectConfig(YamlConfig):
55

6-
def __init__(self):
7-
YamlConfig.__init__(self, module_name='project')
6+
def __init__(self, prefer_bundled_config: bool = False) -> None:
7+
YamlConfig.__init__(
8+
self,
9+
module_name='project',
10+
prefer_bundled_config=prefer_bundled_config,
11+
)
812

913
self.project_name = self.get('project_name')
1014
self.python_version = self.get('python_version')

src/one_dragon/envs/repo_config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ class RepoConfig(YamlConfig):
100100
AUTO_REPOSITORY_VALUE = 'auto'
101101
_SOURCE_EXCLUDED_KEYS = {'repositories', 'regions'}
102102

103-
def __init__(self) -> None:
104-
YamlConfig.__init__(self, module_name='repository')
103+
def __init__(self, prefer_bundled_config: bool = False) -> None:
104+
YamlConfig.__init__(
105+
self,
106+
module_name='repository',
107+
prefer_bundled_config=prefer_bundled_config,
108+
)
105109
repository_config = self._get_repository_config()
106110
primary_branch = repository_config.get('primary_branch', '')
107111
if not isinstance(primary_branch, str) or not primary_branch.strip():

src/one_dragon/utils/os_utils.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,29 @@ def get_path_under_work_dir(*sub_paths: str) -> str:
3434
return join_dir_path_with_mk(get_work_dir(), *sub_paths)
3535

3636

37-
def get_resource_path(*sub_paths: str) -> str:
37+
def get_resource_path(
38+
*sub_paths: str,
39+
prefer_bundled: bool = False,
40+
) -> str:
3841
"""获取资源文件路径。
3942
40-
优先查找工作目录下的路径,不存在时回退到 PyInstaller _MEIPASS。
43+
默认优先查找工作目录下的路径,不存在时回退到 PyInstaller _MEIPASS。
44+
``prefer_bundled`` 开启时交换两者的优先级。
4145
"""
4246
work_path = os.path.join(get_work_dir(), *sub_paths)
47+
runtime_dir = getattr(sys, '_MEIPASS', None)
48+
bundled_path = (
49+
os.path.join(runtime_dir, 'resources', *sub_paths)
50+
if runtime_dir is not None
51+
else None
52+
)
53+
54+
if prefer_bundled and bundled_path is not None and os.path.exists(bundled_path):
55+
return bundled_path
4356
if os.path.exists(work_path):
4457
return work_path
45-
if hasattr(sys, '_MEIPASS'):
46-
mei_path = os.path.join(sys._MEIPASS, 'resources', *sub_paths)
47-
if os.path.exists(mei_path):
48-
return mei_path
58+
if bundled_path is not None and os.path.exists(bundled_path):
59+
return bundled_path
4960
return work_path
5061

5162

0 commit comments

Comments
 (0)