def makeTestList(request): """call the testListGenerator, return the response as excel file for download""" team, release = getTeamAndReleaseObjectsFromSession(request.session) testList = testListGenerator.createTestList(team, release) fileName = '%s_%s_%s_TestList.xls' % (date.today().strftime('%Y%m%d'), team.team_name, release.release_name) # File will be saved as django response: # http://stackoverflow.com/questions/1886744/use-pyexcelerator-to-generate-dynamic-excel-file-with-django-ensure-unique-tempo/1886836#1886836 # http://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document response = HttpResponse(mimetype="application/vnd.ms-excel") response['Content-Disposition'] = 'attachment; filename="%s"' % fileName testList.save(response) return response
def createBatchfileFromTestList(request): """ Create a batch file from the test list showing how the tests can be run from the command line. If a test does not have a test_script specified, fortyTwo does not know about it, and it cannot be added to this file. Assume python runs the tests. Add \r\n as the end line since that is what Windows/Notepad like. See 60293: Create a Mechanism to output the command line for running tests from fortyTwo. """ end = "\r\n" team, release = getTeamAndReleaseObjectsFromSession(request.session) testList = Planned_Exec.active.filter(team=team, release=release) \ .exclude(test_script__isnull=True) \ .exclude(test_script__exact='') \ .order_by('protocol__name', 'case__test__number', 'case__number', 'plm__pg_model') \ .select_related('commandlineargs','country') fileName = '%s_%s_%s_BatchFile.txt' % (date.today().strftime('%Y%m%d'), team.team_name, release.release_name) response = HttpResponse(mimetype="text/plain") response['Content-Disposition'] = 'attachment; filename="%s"' % fileName try: # Write Header information response.write("REM - Batch file output from fortyTwo generated at %s" % datetime.now()) response.write(end) response.write("REM - WARNING: The output in this file is not validated for fortyTwo's intended use."+end) response.write("REM "+end) for test in testList: try: response.write('python '+test.test_script+' '+test.commandlineargs.printCommandLine()+end) except CommandLineArgs.DoesNotExist: pass # tests without command line args do not need to be added to the batch file except: response.write("REM - An unknown error occurred during the creation of this file. Contact a fortyTwo administrator."+end) raise finally: response.close() return response
def createZipFileFromTestList(request): """ Creates and returns a zip file of all log files included in the current test list. The current test list refers to the logs that are present in the xls created by clicking on the export test list button on the project dashboard. """ team, release = getTeamAndReleaseObjectsFromSession(request.session) testList = Planned_Exec.active.filter(team=team, release=release) # Create a zip archive fileName = '%s_%s_%s_TestLogs.zip' % (date.today().strftime('%Y%m%d'), team.team_name, release.release_name) temp = cStringIO.StringIO() zf = ZipFile(temp, 'w', compression=ZIP_DEFLATED) success = True try: for plExec in testList: # See if a log file exists for this plExec log = False if plExec.latesttestlog_set.all().count() >= 1: log = plExec.latesttestlog_set.all()[0].testlog # Write the log if log: logger.info("Adding to archive: %s" % os.path.basename(log.log_file)) zf.write(os.sep.join([settings.LOG_PARSE_BASE_PATH, log.log_file]), os.path.basename(log.log_file)) except Exception as e: logger.info(e) err = e success = False finally: # Done writing the log file, close (save) it zf.close() if success: temp.seek(0) # rewind it response = HttpResponse(temp.read(), mimetype="application/x-zip-compressed") response['Content-Disposition'] = 'attachment; filename="%s"' % fileName response['Content-Length'] = temp.tell() return response else: return HttpResponse('There was an error creating the log file archive: %s' % err, status=500)