利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(config): publish first-boot config (#1491)
Rudy Mizrahi Celeklia803ad09b7f6
diff --git a/app/config/config.py b/app/config/config.pyindex 1fdbc46d..4538fdfa 100644--- a/app/config/config.py+++ b/app/config/config.py@@ -465,23 +465,69 @@ def _load_toml_config(config_path: str):raise++def _initialize_config_from_example(example_file):+ """Publish a complete first-boot config without replacing another writer."""+ fd, temp_path = tempfile.mkstemp(+ prefix=".config-init-", suffix=".toml.tmp",+ dir=os.path.dirname(os.path.abspath(config_file)),+ )+ try:+ with os.fdopen(fd, "wb") as destination:+ with open(example_file, "rb") as source:+ shutil.copyfileobj(source, destination)+ destination.flush()+ os.fsync(destination.fileno())+ try:+ if os.name == "nt":+ # Windows rename refuses an existing destination. POSIX rename+ # overwrites it, so use an exclusive hard-link publication there.+ os.rename(temp_path, config_file)+ else:+ os.link(temp_path, config_file)+ except FileExistsError:+ return False+ except OSError as exc:+ raise OSError(+ exc.errno,+ "cannot safely initialize config.toml; copy config.example.toml "+ "to config.toml before starting the application",+ ) from exc+ return True+ finally:+ try:+ os.unlink(temp_path)+ except FileNotFoundError:+ # A successful Windows rename consumed the temporary path.+ pass++def load_config():# Docker may create an empty directory when a missing config.toml is bind-mounted.# Only remove that empty placeholder; a nonempty directory may contain user data.if os.path.isdir(config_file):try:os.rmdir(config_file)+ except FileNotFoundError:+ # Another initializer already removed this empty bind-mount stub.+ passexcept OSError as exc:- raise IsADirectoryError(- f"{config_file} is a directory and cannot be used as the config file; "- "move or rename it, then restart"- ) from exc+ # Another initializer may already have replaced the empty Docker+ # stub with its complete regular file. Accept only that race, not+ # permission failures or a still-present/nonempty directory.+ if not (+ isinstance(exc, NotADirectoryError) and os.path.isfile(config_file)+ ):+ raise IsADirectoryError(+ f"{config_file} is a directory and cannot be used as the config file; "+ "move or rename it, then restart"+ ) from excif not os.path.isfile(config_file):example_file = f"{root_dir}/config.example.toml"if os.path.isfile(example_file):- shutil.copyfile(example_file, config_file)- logger.info("copy config.example.toml to config.toml")+ if _initialize_config_from_example(example_file):+ logger.info("copy config.example.toml to config.toml")logger.info(f"load config from file: {config_file}")diff --git a/test/services/test_config_initialization.py b/test/services/test_config_initialization.pynew file mode 100644index 00000000..ed366c5e--- /dev/null+++ b/test/services/test_config_initialization.py@@ -0,0 +1,155 @@+import errno+import os+from pathlib import Path+from tempfile import TemporaryDirectory+from unittest.mock import patch++import pytest++from app.config import config+++def test_late_initializer_preserves_config_created_after_missing_check():+ with TemporaryDirectory() as directory:+ target = Path(directory, 'config.toml')+ example = Path(directory, 'config.example.toml')+ example.write_text('[app]\napi_key = ""\n', encoding='utf-8')+ winner = '[app]\napi_key = "already-configured"\n'+ isfile = os.path.isfile+ created = False++ def create_winner_after_check(path):+ nonlocal created+ if str(path) == str(target) and not created:+ created = True+ target.write_text(winner, encoding='utf-8')+ return False+ return isfile(path)++ with (+ patch.object(config, 'root_dir', directory),+ patch.object(config, 'config_file', str(target)),+ patch.object(config.os.path, 'isfile', side_effect=create_winner_after_check),+ ):+ result = config.load_config()+ assert result['app']['api_key'] == 'already-configured'+ assert target.read_text() == winner+ assert not list(Path(directory).glob('.config-init-*'))+++def test_first_boot_publishes_only_complete_config():+ with TemporaryDirectory() as directory:+ target = Path(directory, 'config.toml')+ example = Path(directory, 'config.example.toml')+ payload = '[app]\napi_key = "complete"\n' + '# filler\n' * 1000+ example.write_text(payload, encoding='utf-8')+ link = os.link+ rename = os.rename++ def check_publication(source, destination):+ assert not target.exists()+ assert Path(source).read_text() == payload+ return (rename if os.name == 'nt' else link)(source, destination)++ with (+ patch.object(config, 'root_dir', directory),+ patch.object(config, 'config_file', str(target)),+ patch.object(config.os, 'rename' if os.name == 'nt' else 'link', side_effect=check_publication),+ ):+ assert config.load_config()['app']['api_key'] == 'complete'+ assert target.read_text() == payload+ assert not list(Path(directory).glob('.config-init-*'))++[email protected]('error', [OSError(errno.EPERM, 'unsupported publication'), OSError(errno.ENOSPC, 'full disk')])+def test_initialization_failure_leaves_no_partial_config_or_temporary_file(error):+ with TemporaryDirectory() as directory:+ target = Path(directory, 'config.toml')+ example = Path(directory, 'config.example.toml')+ example.write_text('[app]\napi_key = ""\n', encoding='utf-8')+ with (+ patch.object(config, 'root_dir', directory),+ patch.object(config, 'config_file', str(target)),+ patch.object(config.os, 'rename' if os.name == 'nt' else 'link', side_effect=error),+ ):+ with pytest.raises(OSError, match='copy config.example.toml'):+ config.load_config()+ assert not target.exists()+ assert not list(Path(directory).glob('.config-init-*'))+++def test_windows_publication_uses_rename_without_hardlinks():+ with TemporaryDirectory() as directory:+ target = str(Path(directory, 'config.toml'))+ example = Path(directory, 'config.example.toml')+ example.write_text('[app]\napi_key = ""\n', encoding='utf-8')+ rename = os.rename+ with (+ patch.object(config, 'config_file', target),+ patch.object(config.os, 'name', 'nt'),+ patch.object(config.os, 'rename', side_effect=rename) as renamed,+ patch.object(config.os, 'link') as linked,+ ):+ assert config._initialize_config_from_example(str(example))+ renamed.assert_called_once()+ linked.assert_not_called()+ assert Path(target).is_file()+ assert not list(Path(directory).glob('.config-init-*'))+++def test_windows_destination_collision_preserves_existing_config():+ with TemporaryDirectory() as directory:+ target = Path(directory, 'config.toml')+ target.write_text('[app]\napi_key = "keep"\n', encoding='utf-8')+ example = Path(directory, 'config.example.toml')+ example.write_text('[app]\napi_key = ""\n', encoding='utf-8')+ with (+ patch.object(config, 'config_file', str(target)),+ patch.object(config.os, 'name', 'nt'),+ patch.object(config.os, 'rename', side_effect=FileExistsError),+ ):+ assert not config._initialize_config_from_example(str(example))+ assert '"keep"' in target.read_text()+ assert not list(Path(directory).glob('.config-init-*'))+++def test_initializer_accepts_winner_published_after_docker_stub_check():+ with TemporaryDirectory() as directory:+ target = Path(directory, 'config.toml')+ target.mkdir()+ example = Path(directory, 'config.example.toml')+ example.write_text('[app]\napi_key = ""\n', encoding='utf-8')+ winner = '[app]\napi_key = "winning-config"\n'+ remove_directory = os.rmdir++ def publish_winner_before_stub_removal(path, *args, **kwargs):+ # Both initializers saw the empty Docker bind-mount stub. The first+ # removes it and publishes its complete regular config before the+ # second reaches rmdir, which now raises real NotADirectoryError.+ if str(path) == str(target):+ remove_directory(path)+ target.write_text(winner, encoding='utf-8')+ return remove_directory(path, *args, **kwargs)++ with (+ patch.object(config, 'root_dir', directory),+ patch.object(config, 'config_file', str(target)),+ patch.object(config.os, 'rmdir', side_effect=publish_winner_before_stub_removal),+ ):+ result = config.load_config()+ assert result['app']['api_key'] == 'winning-config'+ assert target.read_text() == winner+ assert not list(Path(directory).glob('.config-init-*'))+++def test_not_a_directory_error_does_not_hide_an_unusable_config_path():+ with TemporaryDirectory() as directory:+ target = Path(directory, 'config.toml')+ target.mkdir()+ with (+ patch.object(config, 'config_file', str(target)),+ patch.object(config.os, 'rmdir', side_effect=NotADirectoryError('not a regular winner')),+ ):+ with pytest.raises(IsADirectoryError):+ config.load_config()+ assert target.is_dir()