xosc-pjisuv.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import time
  4. import oss2
  5. import logging
  6. import json
  7. path1 = '/mnt/disk001/dcl_data_process/src/python2/pjisuv/'
  8. path2 = '/mnt/disk001/dcl_data_process/src/python3/pjisuv/'
  9. logging.basicConfig(filename=path1 + 'log/xosc-pjisuv.log', level=logging.INFO,
  10. format='%(asctime)s - %(levelname)s - %(message)s')
  11. key1 = 'pjisuv/'
  12. sleep_time = 60 # 每多少秒扫描一次
  13. error_bag_json = "/mnt/disk001/dcl_data_process/src/python2/pjisuv/xosc-errorBag.json"
  14. def parse_json_to_string_array(file_path):
  15. try:
  16. # 打开并读取JSON文件(Python 2中不支持encoding参数,需要使用codecs模块或处理文件读取后的编码)
  17. with open(file_path, 'r') as file:
  18. # 读取文件内容
  19. file_content = file.read()
  20. # 解析JSON内容(Python 2中json.loads用于解析字符串)
  21. data = json.loads(file_content.decode('utf-8')) # 假设文件是UTF-8编码,这里需要手动解码
  22. # 检查数据是否是一个列表,并且列表中的元素是否是字符串
  23. if isinstance(data, list):
  24. for item in data:
  25. if not isinstance(item, basestring): # Python 2中字符串类型包括str和unicode,用basestring检查
  26. raise ValueError("JSON数组中的元素不是字符串")
  27. return data
  28. else:
  29. return []
  30. except Exception as e:
  31. return []
  32. def list_to_json_file(data, file_path):
  33. """
  34. 将列表转换为JSON格式并写入指定的文件路径。
  35. 如果文件已存在,则覆盖它。
  36. 参数:
  37. data (list): 要转换为JSON的列表。
  38. file_path (str): 要写入JSON数据的文件路径。
  39. """
  40. # 将列表转换为JSON格式的字符串,并确保输出为UTF-8编码的字符串
  41. json_data = json.dumps(data, ensure_ascii=False, indent=4)
  42. json_data_utf8 = json_data.encode('utf-8') # 编码为UTF-8
  43. # 以写入模式打开文件,如果文件已存在则覆盖
  44. with open(file_path, 'w') as file:
  45. # 将UTF-8编码的JSON字符串写入文件
  46. file.write(json_data_utf8)
  47. def generate_xosc(parse_prefix, local_parse_dir, local_delete_list):
  48. try:
  49. os.chdir(path2)
  50. # todo simulation_hmi 实际生成的 orig,因为多功能车没有 hmi
  51. command2 = 'python3 simulation_hmi.py ' + local_parse_dir[:-1] # 生成orig
  52. logging.info("进入目录 %s 调用命令2: %s", path2, str(command2))
  53. os.system(command2)
  54. local_xosc_path2 = local_parse_dir + 'simulation_orig/xosc/openx0.xosc'
  55. bucket.put_object_from_file(parse_prefix + 'scenario_orig.xosc', local_xosc_path2)
  56. bucket.put_object_from_file(parse_prefix + 'scenario_hmi.xosc',
  57. local_xosc_path2) # todo 将orig也上传成hmi,因为多功能车暂时没有hmi
  58. logging.info("上传 scenario_orig.xosc 成功: %s" % str(parse_prefix + 'scenario_orig.xosc'))
  59. local_delete_list.append(local_xosc_path2)
  60. except Exception as e:
  61. error_bag_list = parse_json_to_string_array(error_bag_json)
  62. error_bag_list.append(parse_prefix)
  63. list_to_json_file(error_bag_list, error_bag_json)
  64. logging.exception(parse_prefix, "生成 xosc 报错,将其添加到错误bag列表。", str(e))
  65. # ------- 获取合并之后的bag包,解析出csv -------
  66. if __name__ == '__main__':
  67. # 1 创建阿里云对象
  68. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  69. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  70. bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
  71. while True:
  72. try:
  73. logging.info("开始新一轮扫描")
  74. prefix_list = []
  75. # 2 获取已经上传完成的所有目录并分组
  76. all_prefix_set = set()
  77. for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
  78. # 获取csv
  79. if 'data_parse' in str(obj1.key) and str(obj1.key).count('/') == 4:
  80. all_prefix_set.add('/'.join(str(obj1.key).split('/')[:-1]) + '/')
  81. for obj2_key in all_prefix_set:
  82. try:
  83. local_delete_list = []
  84. oss_delete_list = []
  85. xosc1_done = False
  86. xosc2_done = False
  87. csv1_done = False
  88. csv2_done = False
  89. for obj3 in oss2.ObjectIterator(bucket, prefix=obj2_key):
  90. if '/scenario_orig.xosc' in str(obj3.key):
  91. xosc1_done = True
  92. # if '/scenario_hmi.xosc' in str(obj3.key):
  93. # xosc2_done = True
  94. if '/pos_orig.csv' in str(obj3.key):
  95. csv1_done = True
  96. # if '/pos_hmi.csv' in str(obj3.key):
  97. # csv2_done = True
  98. if xosc1_done or not csv1_done:
  99. continue
  100. error_bag_list = parse_json_to_string_array(error_bag_json)
  101. if str(obj2_key) in error_bag_list:
  102. continue
  103. logging.info("需要生成xosc: %s" % obj2_key)
  104. local_dir_full = path1 + obj2_key
  105. if not os.path.exists(local_dir_full):
  106. os.makedirs(local_dir_full)
  107. # todo 下载单个csv生成两个视频?
  108. bucket.get_object_to_file(obj2_key + 'pos_orig.csv', local_dir_full + 'pos_orig.csv')
  109. # bucket.get_object_to_file(obj2_key+'pos_hmi.csv', local_dir_full+'pos_hmi.csv')
  110. # local_delete_list.append(local_dir_full + 'pos_orig.csv') # todo 不删除了 做测试用
  111. # local_delete_list.append(local_dir_full+'pos_hmi.csv')
  112. generate_xosc(obj2_key, local_dir_full, local_delete_list)
  113. # 删除本地临时文件
  114. if len(local_delete_list) > 0:
  115. for local_delete in local_delete_list:
  116. try:
  117. os.remove(local_delete)
  118. except Exception as e:
  119. logging.exception("删除本地临时文件报错: %s" % str(e))
  120. except Exception as e:
  121. logging.exception("局部异常处理: %s", str(e))
  122. time.sleep(sleep_time)
  123. except Exception as e:
  124. logging.exception("全局异常处理: %s", str(e))