utilities.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. '''
  2. Created on 22 nov. 2016
  3. @author: olivier.massot
  4. '''
  5. import os
  6. import file
  7. import re
  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. linecount = 0
  28. with open(source_path, "r") as source_file:
  29. ref_path = source_path.replace(source_dir, reference_dir)
  30. with open(ref_path, "r") as ref_file:
  31. for line in source_file:
  32. linecount += 1
  33. if line != ref_file.readline():
  34. ignore = False
  35. try:
  36. for ignore_regex in CMP_IGNORE_LINES:
  37. if re.match(ignore_regex, line) != None:
  38. ignore = True
  39. except:
  40. pass
  41. if not ignore:
  42. print("> {} : source and ref differ at line {}".format(source_path, linecount))
  43. err_code += 1
  44. continue
  45. return err_code
  46. def clean_sources(sources_path):
  47. print("Clean the sources")
  48. # remove the source code of test module and macros
  49. for file_path in ("modules\\test_methods.bas",
  50. "macros\\test_export.bas",
  51. "macros\\test_import.bas"):
  52. sources_path = os.path.abspath(sources_path)
  53. try:
  54. os.remove( file.fjoin(sources_path, file_path) )
  55. except FileNotFoundError:
  56. pass
  57. def verify_log(log_path):
  58. error_count = 0
  59. critical_found = False
  60. with open(os.path.abspath(log_path), "r") as log_file:
  61. for line in log_file:
  62. if "ERROR" in line:
  63. error_count += 1
  64. if "CRITICAL" in line:
  65. critical_found = True
  66. print("CRITICAL ERROR found in the log file ({})".format(log_path))
  67. break
  68. if error_count > 0:
  69. print("Warning: ERRORS found in the log file ({})".format(log_path))
  70. return 0 if not critical_found else 1