import os, sys, datetime, subprocess, shlex, shutil, requests, time, json, oss2 import platform if platform.system() == 'Windows': sys.path.append('e:\\libs\\') else: sys.path.append('/data/deploy/make3d/make2/libs/') import config, libs, libs_db def down_xmps_from_oss(psid): def need_down(): filename = f'xmps/{psid}/{psid}.rcbox' if not config.oss_bucket.object_exists(filename): print(f'未找到{filename}, 请检查是否已经上传{psid}号影棚坐标文件') time.sleep(5) return False #获取到oss上文件rcbox的最后修改时间 oss_file_last_modified = float(config.oss_bucket.get_object_meta(filename).last_modified) #本地文件的路径 local_file = os.path.join(config_path, 'xmps', psid, f'{psid}.rcbox') #判断本地文件是否存在 rcbox文件 if not os.path.exists(local_file): #不存在本地文件,从云端进行下载 print(f'未找到{local_file}, 需要下载影棚坐标文件') os.makedirs(os.path.join(config_path, 'xmps', psid), exist_ok=True) config.oss_bucket.get_object_to_file(filename, local_file) return True #文件已在本地存在,则判断本地时间先后问题 local_file_last_modified = os.path.getmtime(local_file) if oss_file_last_modified > local_file_last_modified: print(f'本地{local_file}文件已过期, 需要下载影棚坐标文件') config.oss_bucket.get_object_to_file(filename, local_file) return True if not need_down(): return print('正在下载影棚坐标文件') dest_path = os.path.join(config_path, 'xmps', psid) if os.path.exists(os.path.join(dest_path)): shutil.rmtree(os.path.join(dest_path), ignore_errors=True) os.makedirs(os.path.join(dest_path, 'mesh'), exist_ok=True) os.makedirs(os.path.join(dest_path, 'texture'), exist_ok=True) prefix = f'xmps/{psid}/mesh/' filelist = oss2.ObjectIteratorV2(config.oss_bucket, prefix=prefix) for file in filelist: if file.key.endswith('.xmp'): filename = file.key.split('/')[-1] print('正在下载:', file.key) config.oss_bucket.get_object_to_file(file.key, os.path.join(dest_path, 'mesh', filename)) prefix = f'xmps/{psid}/texture/' filelist = oss2.ObjectIteratorV2(config.oss_bucket, prefix=prefix) for file in filelist: if file.key.endswith('.xmp'): filename = file.key.split('/')[-1] print('正在下载:', file.key) config.oss_bucket.get_object_to_file(file.key, os.path.join(dest_path, 'texture', filename)) #下载rcbox文件 print("下载rcbox文件") filename_rcbox = f'xmps/{psid}/{psid}.rcbox' local_file_rcbox = os.path.join(config_path, 'xmps', psid, f'{psid}.rcbox') config.oss_bucket.get_object_to_file(filename_rcbox, local_file_rcbox) print('下载完成') #判断远程文件和本地文件的时间问题 def local_and_remote_file_time(psid,pid): #远程文件的路径 config_path = os.path.join(config.sharedir, 'libs') xmps_mesh_path = os.path.join(config_path, 'xmps', psid) remotefile = os.path.join(xmps_mesh_path, f'{psid}.rcbox') #本地文件的路劲 input_path = os.path.join(config.workdir, pid) ImagesGeometry = os.path.join(input_path) localfile = os.path.join(ImagesGeometry, f'{pid}.rcbox') print(f'比对本地文件和远程文件的时间先后--remotefile: {remotefile}, localfile: {localfile}') if os.path.exists(localfile): remotefile_last_modified = os.path.getmtime(remotefile) localfile_last_modified = os.path.getmtime(localfile) print(f'本地文件的时间{localfile_last_modified}, 远程文件的时间{remotefile_last_modified}') #比较两个文件的时间 if float(remotefile_last_modified) < float(localfile_last_modified): print(f'本地{localfile}文件已是最新,') return "local_file_best_new" else: print(f'远程{remotefile}文件最新') return "remote_file_best_new" print(f'本地{localfile}文件不存在') return "local_file_not_exist" if __name__ == '__main__': start = datetime.datetime.now() if len(sys.argv) == 2: pids = sys.argv[1] else: print('usage: python downxmps.py [pids]') exit(1) for pid in pids.split(','): #本地的路径 input_path = os.path.join(config.workdir, pid) ImagesGeometry = os.path.join(input_path, "photo1") ImagesTexture = os.path.join(input_path, "photo2") #共享盘上的路径 psid = libs.getPSid(pid) config_path = os.path.join(config.sharedir, 'libs') xmps_mesh_path = os.path.join(config_path, 'xmps', psid, 'mesh') xmps_texture_path = os.path.join(config_path, 'xmps', psid, 'texture') # 从oss下载影棚坐标文件 down_xmps_from_oss(psid) # 根据坐标文件时间戳判断是否需要更新 if local_and_remote_file_time(psid,pid) == "local_file_best_new": print(f'本地文件已是最新, 不需要下载云端影棚坐标文件') continue # remotefile = os.path.join(xmps_mesh_path, '11_1.xmp') # localfile = os.path.join(ImagesGeometry, '11_1.xmp') # print(f'remotefile: {remotefile}, localfile: {localfile}') # if os.path.exists(localfile): # remotefile_last_modified = os.path.getmtime(remotefile) # localfile_last_modified = os.path.getmtime(localfile) # if remotefile_last_modified <= localfile_last_modified: # print(f'本地{localfile}文件已是最新, 不需要更新') # continue print(f'正在更新{pid}号影棚坐标文件') os.system('xcopy /y /q ' + xmps_mesh_path + '\*.* ' + ImagesGeometry) os.system('xcopy /y /q ' + xmps_texture_path + '\*.* ' + ImagesTexture)