diff --git a/__pycache__/ai_mgmt.cpython-312.pyc b/__pycache__/ai_mgmt.cpython-312.pyc index 2cd571f..6f222b0 100644 Binary files a/__pycache__/ai_mgmt.cpython-312.pyc and b/__pycache__/ai_mgmt.cpython-312.pyc differ diff --git a/ai_mgmt.py b/ai_mgmt.py index cae6ba9..11ad000 100644 --- a/ai_mgmt.py +++ b/ai_mgmt.py @@ -1,32 +1,36 @@ import socket +from pathlib import Path from smb.SMBConnection import SMBConnection SERVER = "192.168.45.4" SHARE = "AGILENT" -USERNAME = "" # leave empty for guest/anonymous +USERNAME = "" PASSWORD = "" CLIENT_NAME = socket.gethostname() -SERVER_NAME = SERVER # use IP for UNC path in TREE_CONNECT +SERVER_NAME = SERVER + +DATA_DIR = Path(__file__).parent / "data" -def list_csv_files(server, share, username="", password=""): - """Search an SMB1 share recursively and return all .csv file paths.""" +def _connect(): conn = SMBConnection( - username, password, + USERNAME, PASSWORD, CLIENT_NAME, SERVER_NAME, use_ntlm_v2=True, - is_direct_tcp=True # use port 445 directly + is_direct_tcp=True ) + if not conn.connect(SERVER, 445): + raise ConnectionError("Failed to connect to SMB share.") + return conn - if not conn.connect(server, 445): - print("Failed to connect to the share.") - return [] +def list_csv_files(server=SERVER, share=SHARE, username=USERNAME, password=PASSWORD): + """Return a list of all .csv paths found on the share (recursive).""" + conn = _connect() csv_files = [] def walk(path): - entries = conn.listPath(share, path) - for entry in entries: + for entry in conn.listPath(share, path): if entry.filename in (".", ".."): continue full_path = f"{path}/{entry.filename}" @@ -43,13 +47,52 @@ def list_csv_files(server, share, username="", password=""): return csv_files -if __name__ == "__main__": - print(f"Searching \\\\{SERVER}\\{SHARE} for CSV files...\n") - files = list_csv_files(SERVER, SHARE, USERNAME, PASSWORD) +def transfer_csv_files(): + """ + Copy all .csv files from the scope share to DATA_DIR, + then delete them from the scope. + Returns (copied, failed) counts. + """ + DATA_DIR.mkdir(exist_ok=True) + conn = _connect() + copied = 0 + failed = 0 - if files: - print(f"Found {len(files)} CSV file(s):") - for f in files: - print(f" {f}") - else: - print("No CSV files found.") + try: + csv_files = [] + + def walk(path): + for entry in conn.listPath(SHARE, path): + if entry.filename in (".", ".."): + continue + full_path = f"{path}/{entry.filename}" + if entry.isDirectory: + walk(full_path) + elif entry.filename.lower().endswith(".csv"): + csv_files.append(full_path) + + walk("/") + + for remote_path in csv_files: + filename = Path(remote_path).name + local_path = DATA_DIR / filename + try: + with open(local_path, "wb") as f: + conn.retrieveFile(SHARE, remote_path, f) + conn.deleteFiles(SHARE, remote_path) + print(f" TRANSFERRED: {filename}") + copied += 1 + except Exception as e: + print(f" FAILED: {filename} — {e}") + failed += 1 + + finally: + conn.close() + + return copied, failed + + +if __name__ == "__main__": + print(f"Transferring CSV files from \\\\{SERVER}\\{SHARE} to {DATA_DIR}...\n") + copied, failed = transfer_csv_files() + print(f"\nDone. {copied} transferred, {failed} failed.") diff --git a/mipi_test.py b/mipi_test.py index 5cd3f13..86998b4 100644 --- a/mipi_test.py +++ b/mipi_test.py @@ -265,10 +265,8 @@ def mgmt_worker(): print("[MGMT] PAUSING TEST — RUNNING MANAGEMENT TASKS...") resume_event.clear() try: - files = ai_mgmt.list_csv_files(ai_mgmt.SERVER, ai_mgmt.SHARE) - print(f"[MGMT] FOUND {len(files)} CSV FILE(S) ON SCOPE DRIVE.") - for f in files: - print(f" {f}") + copied, failed = ai_mgmt.transfer_csv_files() + print(f"[MGMT] TRANSFERRED {copied} FILE(S) TO DATA FOLDER. {failed} FAILED.") except Exception as e: print(f"[MGMT] ERROR: {e}") finally: