utilities.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. return True
  51. def clean_sources(sources_path):
  52. print("Clean the sources")
  53. # remove the source code of test module and macros
  54. for file_path in ("modules\\test_methods.bas",
  55. "macros\\test_export.bas",
  56. "macros\\test_import.bas"):
  57. sources_path = os.path.abspath(sources_path)
  58. try:
  59. os.remove(file.fjoin(sources_path, file_path))
  60. except FileNotFoundError:
  61. pass
  62. def verify_log(log_path):
  63. error_count = 0
  64. critical_found = False
  65. with open(os.path.abspath(log_path), "r") as log_file:
  66. for line in log_file:
  67. if "ERROR" in line:
  68. error_count += 1
  69. if "CRITICAL" in line:
  70. critical_found = True
  71. print("CRITICAL ERROR found in the log file ({})".format(log_path))
  72. break
  73. if error_count > 0:
  74. print("Warning: ERRORS found in the log file ({})".format(log_path))
  75. return 0 if not critical_found else 1