utilities.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. '''
  2. Created on 22 nov. 2016
  3. @author: olivier.massot
  4. '''
  5. import os
  6. import re
  7. import file
  8. # regex for ignoring lines in file comparison
  9. CMP_IGNORE_LINES = ["<dataroot xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" generated=\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\"/?>"]
  10. def compare_dirs(source_dir, reference_dir):
  11. print("** Compare {} to {} **".format(source_dir, reference_dir))
  12. err_code = 0
  13. list_source_dir = file.flist(source_dir, recursive=True, listdirs=False, listfiles=True, complete_paths=True)
  14. list_reference_dir = file.flist(reference_dir, recursive=True, listdirs=False, listfiles=True, complete_paths=True)
  15. if len(list_source_dir) != len(list_reference_dir):
  16. print(">> number of dirs / files does not match")
  17. print("Diff:")
  18. lst_source_names = [f.replace(source_dir, "") for f in list_source_dir]
  19. lst_ref_names = [f.replace(reference_dir, "") for f in list_reference_dir]
  20. for name in list(set(lst_source_names) - set(lst_ref_names)):
  21. print("+ " + name)
  22. err_code += 1
  23. for name in list(set(lst_ref_names) - set(lst_source_names)):
  24. print("- " + name)
  25. err_code += 1
  26. for source_path in list_source_dir:
  27. ref_path = source_path.replace(source_dir, reference_dir)
  28. identical = compare_files(source_path, ref_path)
  29. if not identical:
  30. err_code += 1
  31. return err_code
  32. def compare_files(path1, path2):
  33. """ return True if files are identicals """
  34. linecount = 0
  35. with open(path1, "r") as file1:
  36. with open(path2, "r") as file2:
  37. for line in file1:
  38. linecount += 1
  39. if line != file2.readline():
  40. ignore = False
  41. try:
  42. for ignore_regex in CMP_IGNORE_LINES:
  43. if re.match(ignore_regex, line) != None:
  44. ignore = True
  45. except:
  46. pass
  47. if not ignore:
  48. print("> {} : source and ref differ at line {}".format(path1, linecount))
  49. return False
  50. def clean_sources(sources_path):
  51. print("Clean the sources")
  52. # remove the source code of test module and macros
  53. for file_path in ("modules\\test_methods.bas",
  54. "macros\\test_export.bas",
  55. "macros\\test_import.bas"):
  56. sources_path = os.path.abspath(sources_path)
  57. try:
  58. os.remove(file.fjoin(sources_path, file_path))
  59. except FileNotFoundError:
  60. pass
  61. def verify_log(log_path):
  62. error_count = 0
  63. critical_found = False
  64. with open(os.path.abspath(log_path), "r") as log_file:
  65. for line in log_file:
  66. if "ERROR" in line:
  67. error_count += 1
  68. if "CRITICAL" in line:
  69. critical_found = True
  70. print("CRITICAL ERROR found in the log file ({})".format(log_path))
  71. break
  72. if error_count > 0:
  73. print("Warning: ERRORS found in the log file ({})".format(log_path))
  74. return 0 if not critical_found else 1