"""Offline Python verification lab for PyCharm Chapter 03 (install)."""
import json
import tarfile
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path
VERSION = "CPython 3.12.14 offline PyCharm workflow verifier (desktop IDE not launched)"
def case_platform_installer_artifacts():
artifacts = {
"windows": "pycharm-2025.1.exe",
"macos": "pycharm-2025.1-aarch64.dmg",
"linux": "pycharm-2025.1.tar.gz",
}
expected = {
"windows_ext": ".exe",
"macos_ext": ".dmg",
"linux_ext": ".tar.gz",
}
actual = {
"windows_ext": Path(artifacts["windows"]).suffix,
"macos_ext": Path(artifacts["macos"]).suffix,
"linux_ext": "".join(Path(artifacts["linux"]).suffixes[-2:]),
}
return {
"name": "platform_installer_artifacts",
"summary": "校验 Windows (.exe)、macOS (.dmg) 与 Linux (.tar.gz) 三端安装包后缀规范",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_linux_tarball_launch_sequence():
with tempfile.TemporaryDirectory() as tmp:
tp = Path(tmp)
src_root = tp / "stage" / "pycharm-2025.1" / "bin"
src_root.mkdir(parents=True)
launcher = src_root / "pycharm.sh"
launcher.write_text("#!/bin/sh\necho 'PyCharm launcher ready'\n", encoding="utf-8")
archive = tp / "pycharm-2025.1.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
tf.add(tp / "stage" / "pycharm-2025.1", arcname="pycharm-2025.1")
out_dir = tp / "extracted"
out_dir.mkdir()
with tarfile.open(archive, "r:gz") as tf:
tf.extractall(out_dir, filter="data")
bin_dir = out_dir / "pycharm-2025.1" / "bin"
sh_path = bin_dir / "pycharm.sh"
commands = ["tar -xzf pycharm-*.tar.gz", "cd pycharm-*/bin", "./pycharm.sh"]
expected = {
"bin_exists": True,
"launcher_exists": True,
"shebang": "#!/bin/sh",
"command_count": 3,
}
actual = {
"bin_exists": bin_dir.is_dir(),
"launcher_exists": sh_path.is_file(),
"shebang": sh_path.read_text(encoding="utf-8").splitlines()[0],
"command_count": len(commands),
}
return {
"name": "linux_tarball_launch_sequence",
"summary": "在临时目录构造并解压模拟 pycharm-*.tar.gz,验证 bin/pycharm.sh 启动路径与三步命令序列",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_initial_theme_and_plugin_config():
laf_xml = """<application>
<component name="LafManager" autodetect="false">
<laf themeId="Darcula" />
</component>
</application>"""
root = ET.fromstring(laf_xml)
laf = root.find("./component[@name='LafManager']/laf")
supported_themes = ["Dark", "Light", "Light with Light Header", "High Contrast", "Darcula"]
recommended_plugins = [".env files", ".ignore", "CSV Editor", "IdeaVim"]
expected = {
"configured_theme": "Darcula",
"theme_valid": True,
"plugin_count": 4,
}
actual = {
"configured_theme": laf.attrib.get("themeId") if laf is not None else "",
"theme_valid": (laf.attrib.get("themeId") if laf is not None else "") in supported_themes,
"plugin_count": len(recommended_plugins),
}
return {
"name": "initial_theme_and_plugin_config",
"summary": "解析首次启动外观主题配置 XML(Darcula/Dark/Light)并核对常用插件清单",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_venv_interpreter_detection():
interpreters = [
{"platform": "Windows", "rel_path": ".venv/Scripts/python.exe", "kind": "Virtualenv"},
{"platform": "macOS/Linux", "rel_path": ".venv/bin/python", "kind": "Virtualenv"},
]
expected = {
"windows_bin": ".venv/Scripts/python.exe",
"posix_bin": ".venv/bin/python",
"all_virtualenv": True,
}
actual = {
"windows_bin": interpreters[0]["rel_path"],
"posix_bin": interpreters[1]["rel_path"],
"all_virtualenv": all(i["kind"] == "Virtualenv" for i in interpreters),
}
return {
"name": "venv_interpreter_detection",
"summary": "校验不同操作系统下 Virtualenv 解释器标准相对路径约定",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_platform_installer_artifacts(),
case_linux_tarball_launch_sequence(),
case_initial_theme_and_plugin_config(),
case_venv_interpreter_detection(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。
入口文件:projects/pycharm-install-lab/explore-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联17章) · 含当前章节或实例 · mbskycom202612a2240.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2247.zip