def zip_file(src, name): imz = InMemoryZip() abs_src = os.path.abspath(src) for dirname, subdirs, files in os.walk(src): for filename in files: absname = os.path.abspath(os.path.join(dirname, filename)) arcname = absname[len(abs_src) + 1:] print('zipping %s as %s' % (os.path.join(dirname, filename), arcname)) imz.append(absname, arcname) imz.writetofile(name) return imz
def GET(self): if not 'user' in session or session.user is None: f = register_form() return render.login(f) i = web.input() if not i.has_key("id"): return render.error("No project identifier given") db = init_web_db() sql = """ select min(crash_id) crash_id, concat('0x', substr(conv(program_counter, 10, 16), length(conv(program_counter, 10, 16))-2)) address, crash_signal, substr(disassembly, instr(disassembly, ' ')+1) dis, count(*) count from crashes c, projects p where p.project_id = c.project_id and crash_signal != 'UNKNOWN' and c.project_id = $id group by 2 order by 5 desc """ res = db.query(sql, vars={"id":i.id}) imz = InMemoryZip() i = 0 for row in res: i += 1 samples = get_sample_files(db, i, row.crash_id) folder = "bug%d" % i imz.append("%s/notes.txt" % folder, ", ".join(map(str, row.values())) + "\n") for sample in samples: try: imz.append("%s/%s" % (folder, os.path.split(sample)[1]), open(sample, "rb").read()) if sample.endswith(".diff"): with open(sample, "rb") as f: line = f.readline().strip("\r").strip("\n") pos = line.find(" was ") if pos > -1: original_file = line[pos+5:] imz.append("%s/original" % folder, open(original_file, "rb").read()) except: imz.append("%s/error.txt" % folder, "Error reading file: %s" % str(sys.exc_info()[1])) if i == 0: return render.error("There are no results for the specified project") # This is horrible file_handle, filename = mkstemp() imz.writetofile(filename) buf = open(filename, "rb").read() os.remove(filename) filename = sha1(buf).hexdigest() web.header("Content-type", "application/octet-stream") web.header("Content-disposition", "attachment; filename=%s.zip" % filename) return buf