PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/random/modules/_auxiliary.py

https://gitlab.com/cocorbin/development
Python | 134 lines | 58 code | 52 blank | 24 comment | 7 complexity | 3a3b17254ad0659e1f619c2782563288 MD5 | raw file
  1. ''' Auxiliary functions for development test suite.
  2. '''
  3. __all__ = ['distributeInput', 'finish', 'cleanup', \
  4. 'startLogging']
  5. # standard library
  6. import logging
  7. import socket
  8. import shutil
  9. import glob
  10. import os
  11. # subproject library
  12. import grmpy.tests.random.modules.clsMail
  13. ''' Logging.
  14. '''
  15. def startLogging():
  16. ''' Start logging of performance.
  17. '''
  18. logging.captureWarnings(True)
  19. logger = logging.getLogger('DEV-TEST')
  20. formatter = logging.Formatter(' %(asctime)s %(message)s \n', datefmt = '%I:%M:%S %p')
  21. handler = logging.FileHandler('logging.test.txt', mode = 'w')
  22. handler.setFormatter(formatter)
  23. logger.setLevel(logging.INFO)
  24. logger.addHandler(handler)
  25. ''' Auxiliary functions.
  26. '''
  27. def distributeInput(parser):
  28. ''' Check input for estimation script.
  29. '''
  30. # Parse arguments.
  31. args = parser.parse_args()
  32. # Distribute arguments.
  33. hours = args.hours
  34. # Assertions.
  35. assert (isinstance(hours, float))
  36. assert (hours > 0.0)
  37. # Finishing.
  38. return hours
  39. def finish(dict_, HOURS):
  40. ''' Finishing up a run of the testing battery.
  41. '''
  42. # Antibugging.
  43. assert (isinstance(dict_, dict))
  44. # Auxiliary objects.
  45. hostname = socket.gethostname()
  46. # Finish logging.
  47. with open('logging.test.txt', 'a') as file_:
  48. file_.write(' Summary \n\n')
  49. str_ = ' Test {0:<10} Success {1:<10} Failures {2:<10}\n'
  50. for label in sorted(dict_.keys()):
  51. success = dict_[label]['success']
  52. failure = dict_[label]['failure']
  53. file_.write(str_.format(label, success, failure))
  54. file_.write('\n')
  55. # Send notification.
  56. subject = ' grmToolbox: Completed Testing Battery '
  57. message = ' A ' + str(HOURS) +' hour run of the testing battery on @' + hostname + ' is completed.'
  58. mailObj = modules.clsMail.mailCls()
  59. mailObj.setAttr('subject', subject)
  60. mailObj.setAttr('message', message)
  61. mailObj.setAttr('attachment', 'logging.test.txt')
  62. mailObj.lock()
  63. mailObj.send()
  64. def cleanup():
  65. ''' Cleanup after test battery.
  66. '''
  67. files = []
  68. files = files + glob.glob('*.grm.*')
  69. files = files + glob.glob('.grm.*')
  70. files = files + glob.glob('*.ini')
  71. files = files + glob.glob('*.pkl')
  72. files = files + glob.glob('*.txt')
  73. files = files + glob.glob('*.dat')
  74. for file_ in files:
  75. try:
  76. os.remove(file_)
  77. except OSError:
  78. pass
  79. try:
  80. shutil.rmtree(file_)
  81. except OSError:
  82. pass