common.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """common definitions."""
  2. import os
  3. import shutil
  4. import re
  5. import json
  6. import torch
  7. def clear_gpu_memory():
  8. '''
  9. clear GPU memory
  10. '''
  11. torch.cuda.empty_cache()
  12. print("GPU memory cleared.")
  13. def check_shell(cmd):
  14. """shell successful."""
  15. res = os.system(cmd)
  16. assert res == 0
  17. def update_config_file(file_path, key, value):
  18. """update config file."""
  19. with open(file_path, 'r', encoding="utf-8") as fr:
  20. config = json.loads(fr.read())
  21. config[key] = value
  22. # 保存修改后的内容
  23. with open(file_path, 'w', encoding='utf-8') as fw:
  24. json.dump(config, fw, ensure_ascii=False, indent=4)
  25. def cli_count_folders_and_check_contents(file_path):
  26. """" count cli files."""
  27. if os.path.exists(file_path):
  28. for files in os.listdir(file_path):
  29. folder_count = os.path.getsize(os.path.join(file_path, files))
  30. assert folder_count > 0
  31. assert len(os.listdir(file_path)) > 5
  32. def sdk_count_folders_and_check_contents(file_path):
  33. """count folders."""
  34. if os.path.exists(file_path):
  35. file_count = os.path.getsize(file_path)
  36. assert file_count > 0
  37. else:
  38. exit(1)
  39. def delete_file(path):
  40. """delete file."""
  41. if not os.path.exists(path):
  42. if os.path.isfile(path):
  43. try:
  44. os.remove(path)
  45. print(f"File '{path}' deleted.")
  46. except TypeError as e:
  47. print(f"Error deleting file '{path}': {e}")
  48. elif os.path.isdir(path):
  49. try:
  50. shutil.rmtree(path)
  51. print(f"Directory '{path}' and its contents deleted.")
  52. except TypeError as e:
  53. print(f"Error deleting directory '{path}': {e}")
  54. def check_latex_table_exists(file_path):
  55. """check latex table exists."""
  56. pattern = r'\\begin\{tabular\}.*?\\end\{tabular\}'
  57. with open(file_path, 'r', encoding='utf-8') as file:
  58. content = file.read()
  59. matches = re.findall(pattern, content, re.DOTALL)
  60. return len(matches) > 0
  61. def check_html_table_exists(file_path):
  62. """check html table exists."""
  63. pattern = r'<table.*?>.*?</table>'
  64. with open(file_path, 'r', encoding='utf-8') as file:
  65. content = file.read()
  66. matches = re.findall(pattern, content, re.DOTALL)
  67. return len(matches) > 0
  68. def check_close_tables(file_path):
  69. """delete no tables."""
  70. latex_pattern = r'\\begin\{tabular\}.*?\\end\{tabular\}'
  71. html_pattern = r'<table.*?>.*?</table>'
  72. with open(file_path, 'r', encoding='utf-8') as file:
  73. content = file.read()
  74. latex_matches = re.findall(latex_pattern, content, re.DOTALL)
  75. html_matches = re.findall(html_pattern, content, re.DOTALL)
  76. if len(latex_matches) == 0 and len(html_matches) == 0:
  77. return True
  78. else:
  79. return False