def log_response(request, id): b = Benchmark.objects.get(pk=id) path = b.logfile ov = b.optionvalue.all() hw = b.hardware.all() ev = ExtraValue.objects.filter(benchmark=b) log = "" if not path or not os.path.exists(path): #read log from git repository containing logs try: from beat.tools.logsave import __init_code__, get_log repo = __init_code__() log = get_log(repo, path) except Exception as e: log = "Error: log file not found %s"%e else: try: #read log from file with open(path, 'rb') as file: for line in file: log+=line except IOError: log = "IOError while reading from log. Please retry or report a bug." return render_to_response('log_response.html', {'ev':ev,'log':log,'b': b, 'ov':ov, 'hardware':hw,}, context_instance=RequestContext(request))
def write_to_log(self, lines, filename): """Saves the log contained in lines as a file specified by filename """ # # # # # # # # # # # # seperate the header header_started = False i = 0 done = False #iterate 'till we've seen the whole log or when the header ends while i < len(lines) and not done: if header_started: #we're reading the header, check for its end if not lines[i].startswith("END OF HEADER"): pass # a line of the header is seen else: done = True #stop #we're not at the header yet, keep reading 'till we find the start elif lines[i].startswith("BEGIN OF HEADER"): header_started = True #point to the next line i += 1 #split the header off lines = lines[i:] header= lines[:i] f = os.path.join(LOGS_PATH, filename) fh = os.path.join(LOGS_PATH, filename + ".header") if self.use_dulwich: from beat.tools.logsave import create_log, __init_code__, GitFileError try: repo = __init_code__() create_log(repo, lines, filename) create_log(repo, header, filename + ".header") except GitFileError as gfe: self.print_message(V_QUIET, "Failed to write log to git repository: %s", gfe.error) else: with open(f, 'wb') as file: for x in lines: file.write(x) with open(fh, 'wb') as file: for x in lines: file.write(x) return f