Python Library Unittest
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

module_status.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import os
  5. import json
  6. import fstools
  7. import re
  8. import subprocess
  9. class termcolors:
  10. HEADER = '\033[95m'
  11. OKBLUE = '\033[94m'
  12. OKGREEN = '\033[92m'
  13. WARNING = '\033[93m'
  14. FAIL = '\033[91m'
  15. ENDC = '\033[0m'
  16. BOLD = '\033[1m'
  17. UNDERLINE = '\033[4m'
  18. STATUS_RELEASED = 'RELEASED'
  19. STATUS_AVAILABLE = 'AVAILABLE'
  20. STATUS_IN_WORK = 'IN_WORK'
  21. STATUS_EXISTS = 'EXISTS'
  22. STATUS_OLD = 'OLD'
  23. STATUS_MISSING = 'MISSING'
  24. #
  25. STATUS_CLEAN = 'CLEAN'
  26. STATUS_CHANGED = 'CHANGED'
  27. #
  28. STATUS_UNKNOWN = 'UNKNOWN'
  29. STATUS_LENGTH = 13
  30. STR_STATUS = {
  31. STATUS_RELEASED: termcolors.OKGREEN + (STATUS_LENGTH - len(STATUS_RELEASED)) * ' ' + STATUS_RELEASED + termcolors.ENDC,
  32. STATUS_AVAILABLE: termcolors.OKGREEN + (STATUS_LENGTH - len(STATUS_AVAILABLE)) * ' ' + STATUS_AVAILABLE + termcolors.ENDC,
  33. STATUS_IN_WORK: termcolors.WARNING + (STATUS_LENGTH - len(STATUS_IN_WORK)) * ' ' + STATUS_IN_WORK + termcolors.ENDC,
  34. STATUS_OLD: termcolors.WARNING + (STATUS_LENGTH - len(STATUS_OLD)) * ' ' + STATUS_OLD + termcolors.ENDC,
  35. STATUS_EXISTS: termcolors.WARNING + (STATUS_LENGTH - len(STATUS_EXISTS)) * ' ' + STATUS_EXISTS + termcolors.ENDC,
  36. STATUS_MISSING: termcolors.FAIL + (STATUS_LENGTH - len(STATUS_MISSING)) * ' ' + STATUS_MISSING + termcolors.ENDC,
  37. #
  38. STATUS_CLEAN: termcolors.OKGREEN + (STATUS_LENGTH - len(STATUS_CLEAN)) * ' ' + STATUS_CLEAN + termcolors.ENDC,
  39. STATUS_CHANGED: termcolors.WARNING + (STATUS_LENGTH - len(STATUS_CHANGED)) * ' ' + STATUS_CHANGED + termcolors.ENDC,
  40. #
  41. STATUS_UNKNOWN: termcolors.FAIL + (STATUS_LENGTH - len(STATUS_UNKNOWN)) * ' ' + STATUS_UNKNOWN + termcolors.ENDC,
  42. }
  43. def module_status_head():
  44. rv = termcolors.BOLD + termcolors.UNDERLINE + 'Status of the unittests for pylibs:\n' + termcolors.ENDC
  45. LINE_FORMAT = '%25s%' + str(STATUS_LENGTH) + 's%' + str(STATUS_LENGTH) + 's%' + str(STATUS_LENGTH) + 's%' + str(STATUS_LENGTH) + 's%' + str(STATUS_LENGTH) + 's%' + str(STATUS_LENGTH) + 's\n'
  46. rv += termcolors.BOLD + termcolors.HEADER + LINE_FORMAT % (
  47. 'Library',
  48. 'UT-Status',
  49. 'DOC-Status',
  50. 'Versions',
  51. 'UT-Coverage',
  52. 'SPEC-Status',
  53. 'GIT-Status',
  54. )
  55. rv += (25 + 6 * STATUS_LENGTH) * '-' + '\n' + termcolors.ENDC
  56. return rv
  57. def module_status_line(module_folder):
  58. rv = '%25s%s%s%s%s%s%s\n' % (
  59. os.path.basename(module_folder) + ':',
  60. STR_STATUS.get(module_unittest_status(module_folder), STATUS_UNKNOWN),
  61. STR_STATUS.get(module_doc_status(module_folder), STATUS_UNKNOWN),
  62. module_unittest_versions(module_folder),
  63. module_unittest_coverage(module_folder),
  64. STR_STATUS.get(module_spec_status(module_folder), STATUS_UNKNOWN),
  65. STR_STATUS.get(module_git_status(module_folder), STATUS_UNKNOWN),
  66. )
  67. return rv
  68. def module_unittest_status(module_folder):
  69. try:
  70. with open(os.path.join(module_folder, 'pylibs', os.path.basename(module_folder), '_testresults_', 'unittest.json'), 'r') as fh:
  71. ut_lib = json.loads(fh.read())
  72. except IOError:
  73. return STATUS_MISSING
  74. else:
  75. try:
  76. with open(os.path.join(module_folder, 'unittest', 'testresults', 'unittest.json'), 'r') as fh:
  77. ut_ut = json.loads(fh.read())
  78. except IOError:
  79. return STATUS_UNKNOWN
  80. else:
  81. if ut_ut['testobject_information'] != ut_lib['testobject_information'] or ut_ut['unittest_information'] != ut_lib['unittest_information']:
  82. return STATUS_OLD
  83. else:
  84. ut_status = ut_lib.get('testobject_information', {}).get('State', 'unknown')
  85. if 'released' in ut_status.lower():
  86. return STATUS_RELEASED
  87. elif 'work' in ut_status.lower():
  88. return STATUS_IN_WORK
  89. else:
  90. return STATUS_UNKNOWN
  91. def module_unittest_versions(module_folder):
  92. try:
  93. with open(os.path.join(module_folder, 'unittest', 'testresults', 'unittest.json'), 'r') as fh:
  94. ut = json.loads(fh.read())
  95. except IOError:
  96. return STR_STATUS[STATUS_UNKNOWN]
  97. else:
  98. interpreters = ut.get('testobject_information', '').get('Supported Interpreters')
  99. interpreters = interpreters.split(',')
  100. for i in range(len(interpreters)):
  101. interpreters[i] = interpreters[i].strip()
  102. interpreters[i] = interpreters[i][6:]
  103. rv = ', '.join(interpreters)
  104. return (STATUS_LENGTH - len(rv)) * ' ' + rv
  105. def module_unittest_coverage(module_folder):
  106. try:
  107. with open(os.path.join(module_folder, 'unittest', 'testresults', 'unittest.json'), 'r') as fh:
  108. ut = json.loads(fh.read())
  109. except IOError:
  110. return STR_STATUS[STATUS_UNKNOWN]
  111. else:
  112. lcov = ut.get('coverage_information', [{}])[0].get('line_coverage')
  113. bcov = ut.get('coverage_information', [{}])[0].get('branch_coverage')
  114. if lcov is None or bcov is None:
  115. return STR_STATUS[STATUS_UNKNOWN]
  116. elif lcov > 90:
  117. rv = termcolors.OKGREEN + '%3d%% (%3d%%)' % (lcov, bcov) + termcolors.ENDC
  118. else:
  119. rv = termcolors.WARNING + '%3d%% (%3d%%)' % (lcov, bcov) + termcolors.ENDC
  120. return (STATUS_LENGTH - 11) * ' ' + rv
  121. def module_git_status(module_folder):
  122. p = subprocess.Popen("git -C %s status" % module_folder, stdout=subprocess.PIPE, shell=True)
  123. output = p.communicate()[0]
  124. p_status = p.wait()
  125. if p_status == 0:
  126. if b"nichts zu committen" in output and b"um lokale Commits zu publizieren" not in output:
  127. return STATUS_CLEAN
  128. else:
  129. return STATUS_CHANGED
  130. else:
  131. return STATUS_UNKNOWN
  132. def module_doc_status(module_folder):
  133. if os.path.exists(os.path.join(module_folder, 'pylibs', os.path.basename(module_folder), '_docs_', 'index.html')):
  134. return STATUS_AVAILABLE
  135. else:
  136. if os.path.exists(os.path.join(module_folder, 'docs', 'index.rst')):
  137. return STATUS_IN_WORK
  138. else:
  139. return STATUS_MISSING
  140. def module_spec_status(module_folder):
  141. if os.path.exists(os.path.join(module_folder, 'requirements', 'specification.reqif')):
  142. try:
  143. with open(os.path.join(module_folder, 'unittest', 'testresults', 'unittest.json'), 'r') as fh:
  144. ut = json.loads(fh.read())
  145. if len(ut['lost_souls']['item_list']) > 0 or len(ut['lost_souls']['testcase_list']) > 0:
  146. return STATUS_IN_WORK
  147. else:
  148. return STATUS_CLEAN
  149. except IOError:
  150. return STATUS_EXISTS
  151. else:
  152. return STATUS_MISSING