Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion deploy/OneDragon-Launcher.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ a = Analysis(
['..\\src\\zzz_od\\win_exe\\launcher.py'],
pathex=[],
binaries=[],
datas=[],
datas=[
('../config/project.yml', 'resources/config'),
('../config/repository.yml', 'resources/config'),
],
hiddenimports=['_cffi_backend'],
hookspath=[],
hooksconfig={},
Expand Down
2 changes: 1 addition & 1 deletion docs/develop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ uv run pyinstaller --noconfirm --clean "OneDragon-Installer.spec"

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

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

```shell
uv run pyinstaller --noconfirm --clean "OneDragon-Launcher.spec"
Expand Down
15 changes: 14 additions & 1 deletion src/one_dragon/base/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def __init__(
sub_dir: list[str] | None = None,
sample: bool = False, copy_from_sample: bool = False,
read_sample_only: bool = False,
is_mock: bool = False
is_mock: bool = False,
prefer_bundled_config: bool = False,
):
self.instance_idx: int | None = instance_idx
"""传入时 该配置为一个的脚本实例独有的配置"""
Expand All @@ -32,6 +33,9 @@ def __init__(
self.is_mock: bool = is_mock
"""mock情况下 不读取文件 也不会实际保存 用于测试"""

self._prefer_bundled_config: bool = prefer_bundled_config
"""是否优先读取 PyInstaller 包内的配置"""

self._sample: bool = sample
"""是否有sample文件"""

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

if self._prefer_bundled_config:
resource_path = os_utils.get_resource_path(
*sub_dir,
f'{self.module_name}.yml',
prefer_bundled=True,
)
if resource_path != yml_path and os.path.exists(resource_path):
return resource_path, yml_path, resource_path

# 指定文件存在时 直接使用
if os.path.exists(yml_path):
return yml_path, yml_path, None
Expand Down
13 changes: 9 additions & 4 deletions src/one_dragon/base/operation/one_dragon_env_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@

class OneDragonEnvContext:

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

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

@cached_property
def project_config(self):
return ProjectConfig()
def project_config(self) -> ProjectConfig:
return ProjectConfig(
prefer_bundled_config=self._prefer_bundled_config,
)

@cached_property
def env_config(self):
return EnvConfig(self.repo_config)

@cached_property
def repo_config(self) -> RepoConfig:
return RepoConfig()
return RepoConfig(
prefer_bundled_config=self._prefer_bundled_config,
)

@cached_property
def download_service(self):
Expand Down
2 changes: 1 addition & 1 deletion src/one_dragon/devtools/python_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def run_python(app_path, no_windows: bool = True, args: list[str] | None = None,
print_message(f"OneDragon 启动器 {__version__}", "INFO")
cwd = verify_working_directory()
from one_dragon.base.operation.one_dragon_env_context import OneDragonEnvContext
ctx = OneDragonEnvContext()
ctx = OneDragonEnvContext(prefer_bundled_config=True)
configure_environment(ctx, cwd)
fetch_latest_code(ctx)
sync_dependencies(ctx)
Expand Down
8 changes: 6 additions & 2 deletions src/one_dragon/envs/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

class ProjectConfig(YamlConfig):

def __init__(self):
YamlConfig.__init__(self, module_name='project')
def __init__(self, prefer_bundled_config: bool = False) -> None:
YamlConfig.__init__(
self,
module_name='project',
prefer_bundled_config=prefer_bundled_config,
)

self.project_name = self.get('project_name')
self.python_version = self.get('python_version')
Expand Down
8 changes: 6 additions & 2 deletions src/one_dragon/envs/repo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ class RepoConfig(YamlConfig):
AUTO_REPOSITORY_VALUE = 'auto'
_SOURCE_EXCLUDED_KEYS = {'repositories', 'regions'}

def __init__(self) -> None:
YamlConfig.__init__(self, module_name='repository')
def __init__(self, prefer_bundled_config: bool = False) -> None:
YamlConfig.__init__(
self,
module_name='repository',
prefer_bundled_config=prefer_bundled_config,
)
repository_config = self._get_repository_config()
primary_branch = repository_config.get('primary_branch', '')
if not isinstance(primary_branch, str) or not primary_branch.strip():
Expand Down
23 changes: 17 additions & 6 deletions src/one_dragon/utils/os_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,29 @@ def get_path_under_work_dir(*sub_paths: str) -> str:
return join_dir_path_with_mk(get_work_dir(), *sub_paths)


def get_resource_path(*sub_paths: str) -> str:
def get_resource_path(
*sub_paths: str,
prefer_bundled: bool = False,
) -> str:
"""获取资源文件路径。

优先查找工作目录下的路径,不存在时回退到 PyInstaller _MEIPASS。
默认优先查找工作目录下的路径,不存在时回退到 PyInstaller _MEIPASS。
``prefer_bundled`` 开启时交换两者的优先级。
"""
work_path = os.path.join(get_work_dir(), *sub_paths)
runtime_dir = getattr(sys, '_MEIPASS', None)
bundled_path = (
os.path.join(runtime_dir, 'resources', *sub_paths)
if runtime_dir is not None
else None
)

if prefer_bundled and bundled_path is not None and os.path.exists(bundled_path):
return bundled_path
if os.path.exists(work_path):
return work_path
if hasattr(sys, '_MEIPASS'):
mei_path = os.path.join(sys._MEIPASS, 'resources', *sub_paths)
if os.path.exists(mei_path):
return mei_path
if bundled_path is not None and os.path.exists(bundled_path):
return bundled_path
return work_path


Expand Down
Loading