feat(core): rewrite installer with modular config, dynamic drive resolver, and clean orchestration
This commit is contained in:
@@ -1,63 +1,65 @@
|
||||
"""
|
||||
Directory setup and software file copying module.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
from typing import Optional
|
||||
|
||||
def get_non_c_drive() -> str:
|
||||
from module.config import InstallerConfig, detect_target_drive
|
||||
|
||||
logger = logging.getLogger("acc_installer")
|
||||
|
||||
|
||||
def setup_directories_and_copy(config: Optional[InstallerConfig] = None) -> Path:
|
||||
"""
|
||||
Find the first available non-C drive on the target system.
|
||||
Create directories on the target drive and copy software packages from source.
|
||||
"""
|
||||
possible_drives = [f"{letter}:\\" for letter in "DEFGH"]
|
||||
for drive in possible_drives:
|
||||
if os.path.exists(drive):
|
||||
return drive
|
||||
return r"C:\\"
|
||||
if config is None:
|
||||
config = InstallerConfig()
|
||||
|
||||
def setup_directories_and_copy(source_dir: str = r"C:\SoftwareNP",
|
||||
target_drive: str = None) -> str:
|
||||
"""
|
||||
Create directories on a non-C drive and copy files from source to NovinSoft.
|
||||
"""
|
||||
if not target_drive:
|
||||
base_drive = get_non_c_drive()
|
||||
else:
|
||||
base_drive = target_drive
|
||||
print(f"[+] Selected target drive: {config.target_drive}")
|
||||
print(f"[+] Target base directory: {config.base_dir}")
|
||||
|
||||
# Base target directory set to NovinPardazOne
|
||||
base_target_dir = os.path.join(base_drive, "NovinPardazOne")
|
||||
acc_dir = os.path.join(base_target_dir, "NovinAcc")
|
||||
soft_dir = os.path.join(base_target_dir, "NovinSoft")
|
||||
# Ensure source directory exists
|
||||
source_dir = Path(config.source_dir)
|
||||
if not source_dir.exists():
|
||||
raise FileNotFoundError(f"Source directory does not exist: {source_dir}")
|
||||
|
||||
print(f"[+] Selected target drive: {base_drive}")
|
||||
print(f"[+] Creating directory structure at {base_target_dir} ...")
|
||||
# Create destination directories
|
||||
config.ensure_directories()
|
||||
print(f" [+] Target directories ready at {config.soft_dir}")
|
||||
|
||||
# Create NovinAcc and NovinSoft directories
|
||||
os.makedirs(acc_dir, exist_ok=True)
|
||||
os.makedirs(soft_dir, exist_ok=True)
|
||||
print(" [+] NovinAcc and NovinSoft directories created successfully.")
|
||||
print(f"[+] Copying files from {source_dir} to {config.soft_dir} ...")
|
||||
|
||||
if config.dry_run:
|
||||
items = list(source_dir.iterdir())
|
||||
print(f" [DRY-RUN] Would copy {len(items)} items from {source_dir} to {config.soft_dir}")
|
||||
return config.soft_dir
|
||||
|
||||
copied_count = 0
|
||||
for item in source_dir.iterdir():
|
||||
dest = config.soft_dir / item.name
|
||||
if item.is_dir():
|
||||
shutil.copytree(item, dest, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy2(item, dest)
|
||||
copied_count += 1
|
||||
|
||||
print(f" [+] {copied_count} file(s)/folder(s) copied successfully to {config.soft_dir}.")
|
||||
return config.soft_dir
|
||||
|
||||
# Copy contents of source directory to NovinSoft
|
||||
if os.path.exists(source_dir):
|
||||
print(f"[+] Copying files from {source_dir} to {soft_dir} ...")
|
||||
|
||||
for item in os.listdir(source_dir):
|
||||
s = os.path.join(source_dir, item)
|
||||
d = os.path.join(soft_dir, item)
|
||||
|
||||
if os.path.isdir(s):
|
||||
shutil.copytree(s, d, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy2(s, d)
|
||||
|
||||
print(f" [+] All files copied successfully to {soft_dir}.")
|
||||
return soft_dir
|
||||
else:
|
||||
raise FileNotFoundError(f"[-] Source directory not found at {source_dir}!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.system("chcp 65001 > nul")
|
||||
print("[+] Testing directory creation and software copy module ...")
|
||||
|
||||
import sys
|
||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||
print("[*] Running directory setup and copier module standalone ...")
|
||||
try:
|
||||
target_path = setup_directories_and_copy(target_drive=r"D:\\")
|
||||
print(f"\n[+] Operation completed successfully. Software path: {target_path}")
|
||||
cfg = InstallerConfig(target_drive=detect_target_drive())
|
||||
setup_directories_and_copy(cfg)
|
||||
print("[+] Directory copier module completed successfully.")
|
||||
except Exception as e:
|
||||
print(f"\n[-] Error: {e}")
|
||||
print(f"[-] Error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user