debug_drive_access.pyโข6.02 kB
#!/usr/bin/env python3
"""
Debug script to troubleshoot Google Drive access and file detection
"""
import os
from dotenv import load_dotenv
from utils import get_drive_service, list_files_in_folder
load_dotenv()
def debug_drive_access():
print("๐ MCP Drive Access Diagnostics")
print("=" * 50)
# Check environment variables
print("1. Environment Variables:")
server_folder_id = os.getenv('MCP_SERVER_FOLDER_ID')
client_folder_id = os.getenv('MCP_CLIENT_FOLDER_ID')
key_path = os.getenv('GOOGLE_SERVICE_ACCOUNT_KEY_PATH')
print(f" ๐ Server Folder ID: {server_folder_id}")
print(f" ๐ Client Folder ID: {client_folder_id}")
print(f" ๐ Service Account Key: {key_path}")
if not server_folder_id:
print(" โ MCP_SERVER_FOLDER_ID is missing!")
return
if not key_path or not os.path.exists(key_path):
print(f" โ Service account key file not found: {key_path}")
return
print(" โ
Environment variables look good")
print()
# Test Google Drive connection
print("2. Google Drive Connection:")
try:
drive_service = get_drive_service()
print(" โ
Successfully connected to Google Drive API")
except Exception as e:
print(f" โ Failed to connect to Google Drive: {e}")
return
print()
# Test folder access
print("3. Folder Access Test:")
try:
# Test server folder access
print(f" Testing server folder access ({server_folder_id})...")
folder_info = drive_service.files().get(fileId=server_folder_id).execute()
print(f" โ
Server folder accessible: '{folder_info['name']}'")
# Test client folder access
print(f" Testing client folder access ({client_folder_id})...")
folder_info = drive_service.files().get(fileId=client_folder_id).execute()
print(f" โ
Client folder accessible: '{folder_info['name']}'")
except Exception as e:
print(f" โ Folder access failed: {e}")
print(" ๐ก Make sure the service account has access to both folders")
return
print()
# List files in server folder
print("4. Files in Server Folder:")
try:
files = list_files_in_folder(drive_service, server_folder_id)
if not files:
print(" ๐ญ No files found in server folder")
print(" ๐ก Try uploading a CSV or Excel file to the folder")
else:
print(f" ๐ Found {len(files)} file(s):")
for i, file_info in enumerate(files, 1):
file_name = file_info['name']
file_id = file_info['id']
file_size = file_info.get('size', 'Unknown')
created_time = file_info.get('createdTime', 'Unknown')
# Check if supported format
is_supported = file_name.lower().endswith(('.csv', '.xlsx', '.xls'))
support_icon = "โ
" if is_supported else "โ"
print(f" {i:2d}. {support_icon} {file_name}")
print(f" ๐ ID: {file_id}")
print(f" ๐ Size: {file_size} bytes")
print(f" ๐ Created: {created_time}")
print()
except Exception as e:
print(f" โ Failed to list files: {e}")
return
print()
# Test file processing capability
print("5. Processing Test:")
if files:
# Find first supported file
supported_file = None
for file_info in files:
if file_info['name'].lower().endswith(('.csv', '.xlsx', '.xls')):
supported_file = file_info
break
if supported_file:
print(f" ๐งช Testing with file: {supported_file['name']}")
print(f" ๐ File ID: {supported_file['id']}")
# Test if we can download the file
try:
from utils import download_file_from_drive
file_content = download_file_from_drive(supported_file['id'], drive_service)
print(f" โ
Successfully downloaded file ({len(file_content)} bytes)")
# Test if we can read the file
import pandas as pd
from io import BytesIO
filename = supported_file['name']
if filename.lower().endswith('.csv'):
df = pd.read_csv(BytesIO(file_content))
elif filename.lower().endswith(('.xlsx', '.xls')):
df = pd.read_excel(BytesIO(file_content))
print(f" โ
Successfully parsed file: {len(df)} rows ร {len(df.columns)} columns")
print(f" ๐ Columns: {list(df.columns)}")
except Exception as e:
print(f" โ Failed to process file: {e}")
else:
print(" โ ๏ธ No supported files found for testing")
else:
print(" โ ๏ธ No files available for testing")
print()
print("๐ฏ Diagnosis Complete!")
print("=" * 30)
if files:
supported_count = len([f for f in files if f['name'].lower().endswith(('.csv', '.xlsx', '.xls'))])
if supported_count > 0:
print(f"โ
Found {supported_count} supported file(s) ready for processing")
print("๐ก The auto-processor should be able to detect and process these files")
else:
print("โ ๏ธ Files found but none are in supported formats (CSV, Excel)")
print("๐ก Upload CSV or Excel files to test the auto-processor")
else:
print("๐ญ No files found in the monitored folder")
print("๐ก Upload a CSV or Excel file to your Google Drive folder and try again")
if __name__ == "__main__":
debug_drive_access()