common.py 2.5 KB

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