def deleteCollection(self, collection): """Delete all reports and logs for a particular collection.""" self.logreplock.acquire() try: for reportType in [liblog.RAW_REPORT, liblog.SUMMARY_REPORT]: reports = self.getLogReports(collection, reportType) for report in reports: # stop running job if report is being (re)generated. if report.completeState != COMPLETE: self.stopRunningJob(self.jobName(report)) # delete data files if any. (html_file, valid_file) = liblog.get_report_filenames(self.entConfig, reportType, report.reportName, collection) self.RemoveReportFiles(html_file, valid_file) self.reportCount[reportType] -= len(reports) logging.info('Delete total %d reports of type %s for collection %s.' % ( len(reports), reportType, collection)) listfile = liblog.get_report_list_filename(self.entConfig, reportType, collection) (err, out) = E.run_fileutil_command(self.entConfig, 'rm -f %s' % listfile) if err: logging.error('Cannot remove list file %s.' % listfile) report_collection_dir = liblog.get_report_collection_dir(self.entConfig, collection) (err, out) = E.run_fileutil_command(self.entConfig, 'rmdir %s' % report_collection_dir) if err: logging.error('Cannot delete unused directory %s' % \ report_collection_dir) finally: self.logreplock.release()
def setLogReports(self, reportType, collection, reports): """Set the file content for list of reports of given reportType on given collection.""" try: listfile = liblog.get_report_list_filename(self.entConfig, reportType, collection) gfile.GFile(listfile, 'w').write( string.join(map(ReportToString, reports), '\n')) except IOError: logging.error('Cannot write new LogReport') return false return true
def setReportCompleteState(self, reportType, collection, reportName, completeState, takeOldRecord=false): """This should be called by a worker thread to set complete state of report generation. If takeOldRecord is true, we restore the old creationDate and old isFinal for the report entry.""" self.logreplock.acquire() try: reports = self.getLogReports(collection, reportType) found = false for i in range(len(reports)): if reports[i].reportName == reportName: if reports[i].completeState == COMPLETE_REGENERATE: if takeOldRecord: keptIdx = 0 else: keptIdx = 1 reports[i].isFinal = string.split(reports[i].isFinal, '|')[keptIdx] reports[i].creationDate = string.split(reports[i].creationDate, '|')[keptIdx] reports[i].completeState = completeState found = true break if found: try: self.setLogReports(reportType, collection, reports) except IOError: logging.error('Fail to write report list.') return false else: logging.error('Cannot find the report % in %s' % ( reportName, liblog.get_report_list_filename(self.entConfig, reportType, collection))) return false finally: self.logreplock.release() return true
def getLogReports(self, collection, reportType): """Return a list of reports of given reportType on given collection.""" listFile = liblog.get_report_list_filename(self.entConfig, reportType, collection) reports = [] try: lines = gfile.GFile(listFile, 'r').readlines() for line in lines: if line[-1] == '\n': line = line[:-1] (reportName, collection, creationDate, isFinal, reportType, reportDate, completeState, withResults, topCount, diagnosticTerms) = string.split(line, '\t', 9) reports.append(LogReport(urllib.unquote(reportName), collection, creationDate, isFinal, reportType, reportDate, completeState, withResults, topCount, diagnosticTerms)) except IOError: return [] except ValueError: return [] return reports