#!/usr/bin/env python3 """ 测试temperature_table.py脚本,设置正确的环境变量 """ import os import json import subprocess import sys from datetime import datetime def load_influx_config(): """从default.json加载InfluxDB配置""" config_path = os.path.join(os.path.dirname(__file__), "default.json") try: with open(config_path, 'r', encoding='utf-8') as f: config = json.load(f) return config.get('influx', {}) except Exception as e: print(f"加载配置文件失败: {e}") return {} def test_temperature_table(): """测试temperature_table.py脚本""" print("测试 temperature_table.py 脚本") print("=" * 50) # 加载InfluxDB配置 influx_config = load_influx_config() if not influx_config: print("无法加载InfluxDB配置") return print("InfluxDB配置:") print(f" URL: {influx_config.get('url', 'N/A')}") print(f" ORG: {influx_config.get('org', 'N/A')}") print(f" BUCKET: {influx_config.get('bucket', 'N/A')}") print(f" MEASUREMENT: {influx_config.get('measurement', 'N/A')}") print() # 设置环境变量 env = os.environ.copy() env.update({ 'INFLUX_URL': influx_config.get('url', ''), 'INFLUX_ORG': influx_config.get('org', ''), 'INFLUX_TOKEN': influx_config.get('token', ''), 'INFLUX_BUCKET': influx_config.get('bucket', 'PCM'), 'INFLUX_MEASUREMENT': influx_config.get('measurement', 'PCM_Measurement'), 'EXPERIMENT_START': '2025-11-27T11:30:00Z', 'EXPERIMENT_END': '2025-11-27T12:00:00Z', 'TABLE_LOG_LEVEL': 'DEBUG', 'TABLE_LOG_FILE': 'temperature_test.log' }) print("运行 temperature_table.py 脚本...") print(f" 实验时间: {env['EXPERIMENT_START']} → {env['EXPERIMENT_END']}") print() # 运行脚本 script_path = os.path.join(os.path.dirname(__file__), "temperature_table.py") try: result = subprocess.run( [sys.executable, script_path], env=env, capture_output=True, text=True, timeout=30 ) print("脚本输出:") if result.stdout: print("STDOUT:") print(result.stdout) if result.stderr: print("STDERR:") print(result.stderr) print(f"返回码: {result.returncode}") # 检查日志文件 log_file = "temperature_test.log" if os.path.exists(log_file): print(f"\n日志文件 ({log_file}) 最后10行:") with open(log_file, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines[-10:]: print(f" {line.strip()}") except subprocess.TimeoutExpired: print("脚本运行超时") except Exception as e: print(f"运行脚本失败: {e}") def main(): test_temperature_table() if __name__ == "__main__": main()