pbs_cov 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Copyright (C) 1994-2018 Altair Engineering, Inc.
  4. # For more information, contact Altair at www.altair.com.
  5. #
  6. # This file is part of the PBS Professional ("PBS Pro") software.
  7. #
  8. # Open Source License Information:
  9. #
  10. # PBS Pro is free software. You can redistribute it and/or modify it under the
  11. # terms of the GNU Affero General Public License as published by the Free
  12. # Software Foundation, either version 3 of the License, or (at your option) any
  13. # later version.
  14. #
  15. # PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
  16. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. # FOR A PARTICULAR PURPOSE.
  18. # See the GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. # Commercial License Information:
  24. #
  25. # For a copy of the commercial license terms and conditions,
  26. # go to: (http://www.pbspro.com/UserArea/agreement.html)
  27. # or contact the Altair Legal Department.
  28. #
  29. # Altair’s dual-license business model allows companies, individuals, and
  30. # organizations to create proprietary derivative works of PBS Pro and
  31. # distribute them - whether embedded or bundled with other software -
  32. # under a commercial license agreement.
  33. #
  34. # Use of Altair’s trademarks, including but not limited to "PBS™",
  35. # "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
  36. # trademark licensing policies.
  37. import os
  38. import sys
  39. import getopt
  40. import logging
  41. import logging.config
  42. import errno
  43. import ptl
  44. from ptl.utils.pbs_cliutils import CliUtils
  45. from ptl.utils.pbs_covutils import LcovUtils
  46. from ptl.lib.pbs_testlib import PtlConfig
  47. # trap SIGINT and SIGPIPE
  48. def trap_exceptions(etype, value, tb):
  49. sys.excepthook = sys.__excepthook__
  50. if issubclass(etype, IOError) and value.errno == errno.EPIPE:
  51. pass
  52. else:
  53. sys.__excepthook__(etype, value, tb)
  54. sys.excepthook = trap_exceptions
  55. def usage():
  56. msg = []
  57. msg += ['Usage: ' + os.path.basename(sys.argv[0]) + ' [OPTION]\n\n']
  58. msg += [' code coverage tools\n\n']
  59. msg += ['-c: capture coverage\n']
  60. msg += ['-d <path>: path to directory that contains coverage data\n']
  61. msg += ['-i: initialize coverage\n']
  62. msg += ['-o <path>: path to output directory\n']
  63. msg += ['-m <f1,f2>: merge comma-separated coverage files\n']
  64. msg += ['-r <path>: path to file to remove coverage patterns from\n']
  65. msg += ['-z: reset coverage counters\n']
  66. msg += ['--exclude=<p1,p2>: comma-separated pattern of files to exclude\n']
  67. msg += ['--summarize: summarize coverage analysis\n']
  68. msg += ['--html: Generate HTML from coverage analysis\n']
  69. msg += ['--no-source: don\'t include PBS source in coverage analysis']
  70. msg += [' (Must be used with --html)\n']
  71. msg += ['--baseurl=<url>: use <url> as baseurl in html report']
  72. msg += [' (Must be used with --html)\n']
  73. msg += [' Default source will be in coverage analysis\n']
  74. msg += ['--version: print version number and exit\n']
  75. print "".join(msg)
  76. if __name__ == '__main__':
  77. if len(sys.argv) < 2:
  78. usage()
  79. sys.exit(1)
  80. data_dir = None
  81. capture = None
  82. initialize = None
  83. merge = None
  84. reset = None
  85. remove = None
  86. out = None
  87. html_nosrc = False
  88. html = False
  89. html_baseurl = None
  90. exclude = ['"*work/gSOAP/*"', '"*/pbs/doc/*"', 'lex.yy.c',
  91. 'pbs_ifl_wrap.c', 'usr/include/*', 'unsupported/*']
  92. summarize = None
  93. lvl = logging.INFO
  94. logconf = None
  95. lopts = ["version", "exclude=", "summarize", 'no-source', 'html']
  96. lopts += ['baseurl=']
  97. try:
  98. opts, args = getopt.getopt(sys.argv[1:], "ciszd:mo:l:rh", lopts)
  99. except:
  100. usage()
  101. sys.exit(1)
  102. for o, val in opts:
  103. if o == '-d':
  104. data_dir = CliUtils.expand_abs_path(val)
  105. elif o == '-c':
  106. capture = True
  107. elif o == '-o':
  108. out = CliUtils.expand_abs_path(val)
  109. elif o == '-i':
  110. initialize = True
  111. elif o == '-l':
  112. lvl = CliUtils.get_logging_level(val)
  113. elif o == '-m':
  114. merge = val
  115. elif o == '-l':
  116. lvl = CliUtils().get_logging_level(val)
  117. elif o == '-r':
  118. remove = CliUtils.expand_abs_path(val)
  119. elif o == '-z':
  120. reset = True
  121. elif o == '-h':
  122. usage()
  123. sys.exit(0)
  124. elif o == '--exclude':
  125. exclude = val.split(',')
  126. elif o == '--log-conf':
  127. logconf = val
  128. elif o in ('-s', '--summarize'):
  129. summarize = True
  130. elif o == '--html':
  131. html = True
  132. elif o in '--no-source':
  133. html_nosrc = False
  134. elif o in '--baseurl':
  135. html_baseurl = val
  136. elif o == '--version':
  137. print ptl.__version__
  138. sys.exit(0)
  139. else:
  140. sys.stderr.write("Unrecognized option")
  141. usage()
  142. sys.exit(1)
  143. PtlConfig()
  144. if logconf:
  145. logging.config.fileConfig(logconf)
  146. else:
  147. logging.basicConfig(level=lvl)
  148. if html_nosrc and not html:
  149. logging.error('--no-source must be used with --html')
  150. sys.exit(1)
  151. if html_baseurl and not html:
  152. logging.error('--baseurl must be used with --html')
  153. sys.exit(1)
  154. cu = LcovUtils(cov_out=out, data_dir=data_dir, html_nosrc=html_nosrc,
  155. html_baseurl=html_baseurl)
  156. if reset:
  157. cu.zero_coverage()
  158. if initialize:
  159. cu.initialize_coverage()
  160. if capture:
  161. cu.capture_coverage()
  162. if merge is not None:
  163. for m in merge.split(','):
  164. cu.add_trace(m)
  165. cu.merge_coverage_traces(exclude=exclude)
  166. if html:
  167. cu.generate_html()
  168. if html_baseurl:
  169. cu.change_baseurl()
  170. if summarize:
  171. cu.summarize_coverage()