multirunner.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. """Testing framework module for running tests in Python unittest fashion
  3. Copyright (C) 2014 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS GIS
  6. for details.
  7. :authors: Vaclav Petras
  8. """
  9. from __future__ import print_function
  10. import sys
  11. import os
  12. import argparse
  13. import itertools
  14. import subprocess
  15. def main():
  16. parser = argparse.ArgumentParser(
  17. description='Run tests with new')
  18. parser.add_argument('--location', '-l', required=True, action='append',
  19. dest='locations', metavar='LOCATION',
  20. help='Directories with reports')
  21. parser.add_argument('--location-type', '-t', action='append',
  22. dest='location_types',
  23. default=[], metavar='TYPE',
  24. help='Add repeated values to a list',
  25. )
  26. parser.add_argument('--grassbin', required=True,
  27. help='Use file timestamp instead of date in test summary')
  28. # TODO: rename since every src can be used?
  29. parser.add_argument('--grasssrc', required=True,
  30. help='GRASS GIS source code (to take tests from)')
  31. parser.add_argument('--grassdata', required=True,
  32. help='GRASS GIS data base (GISDBASE)')
  33. parser.add_argument('--create-main-report',
  34. help='Create also main report for all tests',
  35. action="store_true", default=False, dest='main_report')
  36. args = parser.parse_args()
  37. gisdb = args.grassdata
  38. locations = args.locations
  39. locations_types = args.location_types
  40. # TODO: if locations empty or just one we can suppose the same all the time
  41. if len(locations) != len(locations_types):
  42. print("ERROR: Number of locations and their tags must be the same", file=sys.stderr)
  43. return 1
  44. main_report = args.main_report
  45. grasssrc = args.grasssrc # TODO: can be guessed from dist
  46. # TODO: create directory according to date and revision and create reports there
  47. # some predefined variables, name of the GRASS launch script + location/mapset
  48. #grass7bin = 'C:\Program Files (x86)\GRASS GIS 7.2.svn\grass77svn.bat'
  49. grass7bin = args.grassbin # TODO: can be used if pressent
  50. ########### SOFTWARE
  51. # query GRASS 7 itself for its GISBASE
  52. # we assume that GRASS GIS' start script is available and in the PATH
  53. # the shell=True is here because of MS Windows? (code taken from wiki)
  54. startcmd = grass7bin + ' --config path'
  55. p = subprocess.Popen(startcmd, shell=True,
  56. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  57. out, err = p.communicate()
  58. if p.returncode != 0:
  59. print("ERROR: Cannot find GRASS GIS 7 start script (%s):\n%s" % (startcmd, err), file=sys.stderr)
  60. return 1
  61. gisbase = out.strip('\n')
  62. # set GISBASE environment variable
  63. os.environ['GISBASE'] = gisbase
  64. # define GRASS Python environment
  65. grass_python_dir = os.path.join(gisbase, "etc", "python")
  66. sys.path.append(grass_python_dir)
  67. ########### DATA
  68. # define GRASS DATABASE
  69. # Set GISDBASE environment variable
  70. os.environ['GISDBASE'] = gisdb
  71. # import GRASS Python package for initialization
  72. import grass.script.setup as gsetup
  73. # launch session
  74. # we need some location and mapset here
  75. # TODO: can init work without it or is there some demo location in dist?
  76. location = locations[0].split(':')[0]
  77. mapset = 'PERMANENT'
  78. gsetup.init(gisbase, gisdb, location, mapset)
  79. reports = []
  80. for location, location_type in itertools.izip(locations, locations_types):
  81. # here it is quite a good place to parallelize
  82. # including also type to make it unique and preserve it for sure
  83. report = 'report_for_' + location + '_' + location_type
  84. absreport = os.path.abspath(report)
  85. p = subprocess.Popen([sys.executable, '-tt',
  86. '-m', 'grass.gunittest.main',
  87. '--grassdata', gisdb, '--location', location,
  88. '--location-type', location_type,
  89. '--output', absreport],
  90. cwd=grasssrc)
  91. returncode = p.wait()
  92. reports.append(report)
  93. if main_report:
  94. # TODO: solve the path to source code (work now only for grass source code)
  95. arguments = [sys.executable, grasssrc + '/lib/python/guittest/' + 'multireport.py', '--timestapms']
  96. arguments.extend(reports)
  97. p = subprocess.Popen(arguments)
  98. returncode = p.wait()
  99. if returncode != 0:
  100. print("ERROR: Creation of main report failed.", file=sys.stderr)
  101. return 1
  102. return 0
  103. if __name__ == '__main__':
  104. sys.exit(main())