libreoffice_interface.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. import re
  3. import signal
  4. import subprocess
  5. import sys
  6. import time
  7. import traceback
  8. import psutil
  9. from format_convert import timeout_decorator
  10. from format_convert import get_memory_info
  11. from format_convert.judge_platform import get_platform
  12. import logging
  13. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  14. def monitor_libreoffice():
  15. try:
  16. # logging.info("=========================================")
  17. logging.info("into monitor_libreoffice")
  18. # logging.info("------------------------------MEM top 10")
  19. os.system("ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head")
  20. #
  21. # logging.info("--------------------------soffice process")
  22. # os.system("ps -ef | grep soffice")
  23. pids = psutil.pids()
  24. for pid in pids:
  25. try:
  26. process = psutil.Process(pid)
  27. # if process.username() == "appuser":
  28. if re.search("soffice|unrar", process.exe()):
  29. # if time.time() - process.create_time() >= 120:
  30. # logging.info("---------------------------killed soffice")
  31. # print("process", pid, process.exe())
  32. logging.info("process " + str(pid) + str(process.exe()))
  33. comm = "kill -9 " + str(pid)
  34. # subprocess.call(comm, shell=True)
  35. os.system(comm)
  36. # print("killed", pid)
  37. logging.info("killed " + str(pid))
  38. except TimeoutError:
  39. raise TimeoutError
  40. except:
  41. continue
  42. # logging.info("=========================================")
  43. except TimeoutError:
  44. raise TimeoutError
  45. # @timeout_decorator.timeout(120, timeout_exception=TimeoutError, use_signals=False)
  46. def office_convert(src_path, dest_path, target_format, retry_times=1):
  47. try:
  48. logging.info("into office_convert")
  49. print("src_path", src_path)
  50. uid1 = src_path.split(os.sep)[-1].split(".")[0]
  51. dest_file_path = dest_path + uid1 + "." + target_format
  52. src_format = src_path.split(".")[-1]
  53. # 重试转换
  54. for i in range(retry_times):
  55. # 调用Win下的libreoffice子进程
  56. if get_platform() == "Windows":
  57. soffice = 'C:\\Program Files\\LibreOfficeDev 5\\program\\soffice.exe'
  58. comm_list = [soffice, '--headless', '--convert-to', target_format, src_path,
  59. '--outdir', dest_path+os.sep]
  60. try:
  61. p = subprocess.call(comm_list, timeout=30*(i+2))
  62. except:
  63. continue
  64. # 调用Linux下的libreoffice子进程
  65. else:
  66. # 先杀libreoffice进程
  67. monitor_libreoffice()
  68. # 再调用转换
  69. libreoffice_dir = 'soffice'
  70. comm_list = [libreoffice_dir, '--headless', '--convert-to', target_format, src_path,
  71. '--outdir', dest_path+os.sep]
  72. comm = ''
  73. for c in comm_list:
  74. comm += c + ' '
  75. # logging.info("office_convert command" + comm)
  76. try:
  77. # p = subprocess.call(comm_list, timeout=30*(i+2))
  78. os.system(comm)
  79. except TimeoutError:
  80. return [-5]
  81. except Exception as e:
  82. print(src_format + ' to ' + target_format + ' Failed! Retry...', i, 'times')
  83. print(traceback.print_exc())
  84. continue
  85. # 执行失败,重试
  86. if not os.path.exists(dest_file_path):
  87. print(src_format + ' to ' + target_format + ' Failed! Retry...', i, 'times')
  88. continue
  89. # 执行成功,跳出循环
  90. else:
  91. break
  92. # 重试后还未成功
  93. if not os.path.exists(dest_file_path):
  94. # print(src_format + ' to ' + target_format + ' failed!')
  95. logging.info(src_format + ' to ' + target_format + " failed!")
  96. return [-3]
  97. logging.info("out office_convert")
  98. return dest_file_path
  99. except TimeoutError:
  100. return [-5]