"""Offline Classic ASP FileSystemObject verification lab (IIS ASP absent)."""
import json
import ntpath
import os
from pathlib import Path
import shutil
import tempfile
class MockFileSystemObject:
"""Offline model of Scripting.FileSystemObject backed by a sandboxed directory."""
def __init__(self, sandbox_root: Path) -> None:
self.root = sandbox_root
self.drives = ["C:", "D:"]
@property
def Drives(self) -> list[str]:
return list(self.drives)
def BuildPath(self, base: str, name: str) -> str:
return ntpath.join(base, name)
def GetDriveName(self, path: str) -> str:
drive, _ = ntpath.splitdrive(path)
return drive
def GetParentFolderName(self, path: str) -> str:
trimmed = path.rstrip("\\/")
parent = ntpath.dirname(trimmed)
return parent
def GetFileName(self, path: str) -> str:
trimmed = path.rstrip("\\/")
return ntpath.basename(trimmed)
def GetExtensionName(self, path: str) -> str:
_, ext = ntpath.splitext(self.GetFileName(path))
return ext.lstrip(".")
def GetBaseName(self, path: str) -> str:
base, _ = ntpath.splitext(self.GetFileName(path))
return base
def GetAbsolutePathName(self, rel_path: str) -> str:
return str((self.root / rel_path).resolve())
def GetTempName(self, seq: int = 1) -> str:
return f"rad{seq:05X}.tmp"
def GetSpecialFolder(self, folder_spec: int) -> str:
mapping = {
0: r"C:\Windows",
1: r"C:\Windows\System32",
2: str(self.root / "temp"),
}
return mapping[folder_spec]
def DriveExists(self, drive_spec: str) -> bool:
norm = drive_spec.strip().rstrip("\\").upper()
if len(norm) == 1:
norm += ":"
return norm in self.drives
def FileExists(self, p: Path) -> bool:
return p.is_file()
def FolderExists(self, p: Path) -> bool:
return p.is_dir()
def CreateFolder(self, p: Path) -> Path:
p.mkdir(parents=False, exist_ok=False)
return p
def CreateTextFile(self, p: Path, overwrite: bool = True):
if p.exists() and not overwrite:
raise FileExistsError(str(p))
return p.open("w", encoding="utf-8", newline="\r\n")
def OpenTextFile(self, p: Path, iomode: int = 1, create: bool = False):
mode_map = {1: "r", 2: "w", 8: "a"}
if iomode == 1 and not p.exists():
if create:
p.touch()
else:
raise FileNotFoundError(str(p))
return p.open(mode_map[iomode], encoding="utf-8", newline="")
def CopyFile(self, src: Path, dst: Path) -> None:
shutil.copy2(src, dst)
def MoveFile(self, src: Path, dst: Path) -> None:
shutil.move(str(src), str(dst))
def DeleteFile(self, p: Path) -> None:
p.unlink()
def CopyFolder(self, src: Path, dst: Path) -> None:
shutil.copytree(src, dst)
def MoveFolder(self, src: Path, dst: Path) -> None:
shutil.move(str(src), str(dst))
def DeleteFolder(self, p: Path) -> None:
shutil.rmtree(p)
def main() -> None:
cases = []
with tempfile.TemporaryDirectory(prefix="asp_fso_") as tmp:
root = Path(tmp)
fso = MockFileSystemObject(root)
# Case 1: CreateTextFile + OpenTextFile + FileExists + DeleteFile
test_file = root / "test.txt"
with fso.CreateTextFile(test_file, overwrite=True) as fh:
fh.write("Hello World!\n")
exists_after_create = fso.FileExists(test_file)
with fso.OpenTextFile(test_file, iomode=8) as fh:
fh.write("Second Line\n")
with fso.OpenTextFile(test_file, iomode=1) as fh:
lines = fh.read().splitlines()
fso.DeleteFile(test_file)
exists_after_delete = fso.FileExists(test_file)
expected1 = {
"exists_after_create": True,
"lines": ["Hello World!", "Second Line"],
"exists_after_delete": False,
}
actual1 = {
"exists_after_create": exists_after_create,
"lines": lines,
"exists_after_delete": exists_after_delete,
}
cases.append({
"name": "fso_create_open_and_delete_text_file",
"summary": "Verify CreateTextFile, OpenTextFile (append/read), FileExists, and DeleteFile in temp dir.",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: Folder & File copy/move/delete
src_dir = fso.CreateFolder(root / "src")
note = src_dir / "note.txt"
with fso.CreateTextFile(note, overwrite=True) as fh:
fh.write("ASP FSO")
copied_note = src_dir / "note_copy.txt"
fso.CopyFile(note, copied_note)
moved_note = src_dir / "note_moved.txt"
fso.MoveFile(copied_note, moved_note)
bak_dir = root / "bak"
fso.CopyFolder(src_dir, bak_dir)
archived_dir = root / "archived"
fso.MoveFolder(bak_dir, archived_dir)
archived_files = sorted(p.name for p in archived_dir.iterdir())
bak_exists_after_move = fso.FolderExists(bak_dir)
fso.DeleteFolder(archived_dir)
archived_exists_after_del = fso.FolderExists(archived_dir)
expected2 = {
"src_exists": True,
"archived_files": ["note.txt", "note_moved.txt"],
"bak_exists_after_move": False,
"archived_exists_after_del": False,
}
actual2 = {
"src_exists": fso.FolderExists(src_dir),
"archived_files": archived_files,
"bak_exists_after_move": bak_exists_after_move,
"archived_exists_after_del": archived_exists_after_del,
}
cases.append({
"name": "fso_folder_copy_move_delete",
"summary": "Verify CreateFolder, FolderExists, CopyFile, MoveFile, CopyFolder, MoveFolder, DeleteFolder.",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: Path inspection helpers
sample_path = fso.BuildPath(r"C:\inetpub\wwwroot\demo", "report.asp")
expected3 = {
"built": r"C:\inetpub\wwwroot\demo\report.asp",
"drive_name": "C:",
"parent": r"C:\inetpub\wwwroot\demo",
"file_name": "report.asp",
"extension": "asp",
"base_name": "report",
"temp_name": "rad0002A.tmp",
}
actual3 = {
"built": sample_path,
"drive_name": fso.GetDriveName(sample_path),
"parent": fso.GetParentFolderName(sample_path),
"file_name": fso.GetFileName(sample_path),
"extension": fso.GetExtensionName(sample_path),
"base_name": fso.GetBaseName(sample_path),
"temp_name": fso.GetTempName(42),
}
cases.append({
"name": "fso_path_inspection_methods",
"summary": "Verify BuildPath, GetDriveName, GetParentFolderName, GetFileName, GetExtensionName, GetBaseName, GetTempName.",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: Drives collection, DriveExists, GetSpecialFolder, GetAbsolutePathName
abs_note = fso.GetAbsolutePathName("src/note.txt")
expected4 = {
"drives": ["C:", "D:"],
"c_exists": True,
"z_exists": False,
"special_windows": r"C:\Windows",
"special_system": r"C:\Windows\System32",
"abs_ends_with_note": True,
}
actual4 = {
"drives": fso.Drives,
"c_exists": fso.DriveExists("c"),
"z_exists": fso.DriveExists("Z:"),
"special_windows": fso.GetSpecialFolder(0),
"special_system": fso.GetSpecialFolder(1),
"abs_ends_with_note": os.path.normpath(abs_note).endswith(os.path.join("src", "note.txt")),
}
cases.append({
"name": "fso_drives_and_special_folders",
"summary": "Verify Drives collection, DriveExists, GetSpecialFolder, and GetAbsolutePathName.",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
# Case 5: CreateTextFile overwrite=False guard
guard_file = root / "locked.txt"
with fso.CreateTextFile(guard_file, overwrite=True) as fh:
fh.write("v1")
overwrite_blocked = False
try:
fso.CreateTextFile(guard_file, overwrite=False)
except FileExistsError:
overwrite_blocked = True
with fso.CreateTextFile(guard_file, overwrite=True) as fh:
fh.write("v2")
final_text = guard_file.read_text(encoding="utf-8")
expected5 = {"overwrite_blocked": True, "after_force_overwrite": "v2"}
actual5 = {"overwrite_blocked": overwrite_blocked, "after_force_overwrite": final_text}
cases.append({
"name": "fso_overwrite_protection_guard",
"summary": "Verify CreateTextFile raises when overwrite=False on existing file and replaces when True.",
"expected": expected5,
"actual": actual5,
"passed": actual5 == expected5,
})
print(json.dumps({
"version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/asp-ref-filesystem-lab/explore-ref-filesystem.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-filesystem.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1443.zip