utilities.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. '''
  2. Created on 22 nov. 2016
  3. @author: olivier.massot
  4. '''
  5. import os
  6. import file
  7. def compare_dirs(source_dir, reference_dir):
  8. print("** Compare {} to {} **".format(source_dir, reference_dir))
  9. err_code = 0
  10. list_source_dir = file.flist(source_dir, recursive=True, listdirs=False, listfiles=True, complete_paths=True)
  11. list_reference_dir = file.flist(reference_dir, recursive=True, listdirs=False, listfiles=True, complete_paths=True)
  12. if len(list_source_dir) != len(list_reference_dir):
  13. print(">> number of dirs / files does not match")
  14. print("Diff:")
  15. lst_source_names = [f.replace(source_dir, "") for f in list_source_dir]
  16. lst_ref_names = [f.replace(reference_dir, "") for f in list_reference_dir]
  17. for name in list( set(lst_source_names) - set(lst_ref_names) ):
  18. print("+ "+name)
  19. err_code += 1
  20. for name in list( set(lst_ref_names) - set(lst_source_names) ):
  21. print("- "+name)
  22. err_code += 1
  23. for source_path in list_source_dir:
  24. linecount = 0
  25. with open(source_path, "rb") as source_file:
  26. ref_path = source_path.replace(source_dir, reference_dir)
  27. with open(ref_path, "rb") as ref_file:
  28. for line in source_file:
  29. linecount += 1
  30. if line != ref_file.readline():
  31. print("> {} : source and ref differ at line {}".format(source_path, linecount))
  32. err_code += 1
  33. continue
  34. return err_code
  35. def clean_sources(sources_path):
  36. print("Clean the sources")
  37. # remove the source code of test module and macros
  38. for file_path in ("modules\\test_methods.bas",
  39. "macros\\test_export.bas",
  40. "macros\\test_import.bas"):
  41. sources_path = os.path.abspath(sources_path)
  42. try:
  43. os.remove( file.fjoin(sources_path, file_path) )
  44. except FileNotFoundError:
  45. pass
  46. def verify_log(log_path):
  47. error_count = 0
  48. critical_found = False
  49. with open(os.path.abspath(log_path), "r") as log_file:
  50. for line in log_file:
  51. if "ERROR" in line:
  52. error_count += 1
  53. if "CRITICAL" in line:
  54. critical_found = True
  55. print("CRITICAL ERROR found in the log file ({})".format(log_path))
  56. break
  57. if error_count > 0:
  58. print("Warning: ERRORS found in the log file ({})".format(log_path))
  59. return 0 if not critical_found else 1