Files
iDRAC_Info/data/server_list/GUIDtxtT0Execl.py
2025-10-05 17:37:51 +09:00

117 lines
3.8 KiB
Python

from __future__ import annotations
import os
from pathlib import Path
from collections import OrderedDict
import pandas as pd
# ------------------------------------------------------------
# Cross-platform root resolver (Windows / Linux / macOS)
# ------------------------------------------------------------
def resolve_data_root() -> Path:
"""
Priority:
1) Env var IDRAC_DATA_DIR (absolute/relative OK)
2) nearest parent of this file that contains a 'data' folder
3) ./data under current working directory
"""
env = os.getenv("IDRAC_DATA_DIR")
if env:
return Path(env).expanduser().resolve()
here = Path(__file__).resolve()
for p in [here] + list(here.parents):
if (p / "data").is_dir():
return (p / "data").resolve()
return (Path.cwd() / "data").resolve()
DATA_ROOT = resolve_data_root()
# ------------------------------------------------------------
# Paths (can be overridden with env vars if needed)
# ------------------------------------------------------------
SERVER_LIST_DIR = Path(os.getenv("GUID_SERVER_LIST_DIR", DATA_ROOT / "server_list"))
SERVER_LIST_FILE = Path(os.getenv("GUID_LIST_FILE", SERVER_LIST_DIR / "guid_list.txt"))
GUID_TXT_DIR = Path(os.getenv("GUID_TXT_DIR", DATA_ROOT / "guid_file"))
OUTPUT_XLSX = Path(
os.getenv("GUID_OUTPUT_XLSX", DATA_ROOT / "idrac_info" / "XE9680_GUID.xlsx")
)
# Make sure output directory exists
OUTPUT_XLSX.parent.mkdir(parents=True, exist_ok=True)
# ------------------------------------------------------------
# Utilities
# ------------------------------------------------------------
def read_lines_any_encoding(path: Path) -> list[str]:
"""Read text file trying common encodings (utf-8/utf-8-sig/cp949/euc-kr/latin-1)."""
encodings = ["utf-8-sig", "utf-8", "cp949", "euc-kr", "latin-1"]
for enc in encodings:
try:
with path.open("r", encoding=enc, errors="strict") as f:
return f.read().splitlines()
except Exception:
continue
# last resort with replacement
with path.open("r", encoding="utf-8", errors="replace") as f:
return f.read().splitlines()
def parse_txt_with_st(file_path: Path) -> dict:
"""
Parse a GUID .txt file:
- First line becomes 'S/T'
- Remaining lines in 'Key: Value' form
Keeps insertion order.
"""
lines = read_lines_any_encoding(file_path)
if not lines:
return {}
data = OrderedDict()
data["S/T"] = lines[0].strip()
for raw in lines[1:]:
line = raw.strip()
if not line or ":" not in line:
continue
key, value = line.split(":", 1)
data[key.strip()] = value.strip()
return dict(data)
# ------------------------------------------------------------
# Load list of file basenames from guid_list.txt
# ------------------------------------------------------------
if not SERVER_LIST_FILE.is_file():
raise FileNotFoundError(f"guid_list.txt not found: {SERVER_LIST_FILE}")
file_names = [x.strip() for x in read_lines_any_encoding(SERVER_LIST_FILE) if x.strip()]
# ------------------------------------------------------------
# Collect rows
# ------------------------------------------------------------
rows: list[dict] = []
for name in file_names:
txt_path = GUID_TXT_DIR / f"{name}.txt"
if not txt_path.is_file():
print(f"[WARN] 파일을 찾을 수 없습니다: {txt_path.name}")
# still append at least S/T if you want a row placeholder
# rows.append({"S/T": name})
continue
rows.append(parse_txt_with_st(txt_path))
# Build DataFrame (union of keys across all rows)
df = pd.DataFrame(rows)
# Prepend No column (1..N)
df.insert(0, "No", range(1, len(df) + 1))
# Save to Excel
df.to_excel(OUTPUT_XLSX, index=False)
print(f"엑셀 파일이 생성되었습니다: {OUTPUT_XLSX}")