def patch_fix(request, patch_id): patch = get_object_or_404(Patch, id=patch_id) if request.method == "POST": src = get_request_paramter(request, 'src', '') if len(src) == 0: return HttpResponse('FIX: report, ERROR: no source specified') sfile = patch.sourcefile() if not os.path.exists(sfile) or not os.path.isfile(sfile): return HttpResponse('FIX: report, ERROR: %s does not exists' % sfile) try: rtype = patch.type repo = patch.tag.repo srcfile = open(sfile, "w") try: src = srcfile.write(src) except: src = srcfile.write(unicode.encode(src, 'utf-8')) srcfile.close() diff = _get_diff_and_revert(repo.dirname(), patch.file) user = patch.username() email = patch.email() if re.search(r'{{[^}]*}}', rtype.ptitle): title = rtype.ptitle else: title = patch.title if re.search(r'{{[^}]*}}', rtype.pdesc): desc = rtype.pdesc else: desc = patch.desc formater = PatchFormater(repo.dirname(), patch.file, user, email, title, desc, diff) patch.content = formater.format_patch() patch.title = formater.format_title() patch.desc = formater.format_desc() patch.emails = formater.get_mail_list() if patch.diff != diff: patch.diff = diff patch.status = STATUS_NEW patch.build = 0 patch.save() return HttpResponse('FIX: patch %d, SUCCEED' % patch.id, True) except: return HttpResponse('FIX: patch, ERROR: write file error') else: context = RequestContext(request) sfile = patch.sourcefile() src = '' if os.path.exists(sfile) and os.path.isfile(sfile): srcfile = open(sfile, "r") src = srcfile.read() srcfile.close() try: tmpsrcfname = tempfile.mktemp() tmpsrcfile = open(tmpsrcfname, "w") tmpsrcfile.write(src) tmpsrcfile.close() tmpdiffname = tempfile.mktemp() tmpdiffile = open(tmpdiffname, "w") tmpdiffile.write(patch.diff) tmpdiffile.close() os.system('patch %s -i %s' % (tmpsrcfname, tmpdiffname)) srcfile = open(tmpsrcfname, "r") src = srcfile.read() srcfile.close() os.unlink(tmpsrcfname) os.unlink(tmpdiffname) except: pass context['patch'] = patch context['src'] = src return render_to_response("patch/patchfix.html", context)
def patch_new(request): if request.method == "POST": tagid = get_request_paramter(request, 'tag') typeid = get_request_paramter(request, 'type') rfile = get_request_paramter(request, 'file') rtags = GitTag.objects.filter(id = tagid) if len(rtags) == 0: logevent("NEW: patch , ERROR: tag id %s does not exists" % tagid) return HttpResponse('NEW: patch, ERROR: tag id %s does not exists' % tagid) rtypes = Type.objects.filter(id = typeid) if len(rtypes) == 0: logevent("NEW: patch , ERROR: type id %s does not exists" % typeid) return HttpResponse('NEW: patch, ERROR: type id %s does not exists' % typeid) rtype = rtypes[0] patch = Patch(tag = rtags[0], type = rtype, file = rfile, status = STATUS_NEW, diff = '') if not os.path.exists(patch.sourcefile()): logevent("NEW: patch , ERROR: type id %s does not exists" % typeid) return HttpResponse('NEW: patch, ERROR: type id %s does not exists' % typeid) patch.save() for dot in patch_engine_list(): test = dot(rtags[0].repo.dirname(), None, rtags[0].repo.builddir()) for i in range(test.tokens()): if test.get_type() != rtype.id: test.next_token() continue test.set_filename(rfile) if test.should_patch(): text = test.get_patch() patch.diff = text user = patch.username() email = patch.email() desc = test.get_patch_description() title = test.get_patch_title() if desc is None: desc = rtype.pdesc if title is None: title = rtype.ptitle formater = PatchFormater(rtags[0].repo.dirname(), rfile, user, email, title, desc, text) patch.content = formater.format_patch() patch.title = formater.format_title() patch.desc = formater.format_desc() patch.emails = formater.get_mail_list() patch.module = formater.get_module() patch.save() break rtags[0].total += 1 rtags[0].save() logevent("NEW: patch for %s, SUCCEED: new id %s" % (rfile, patch.id), True) return HttpResponse('NEW: patch for file, SUCCEED') else: repoid = int(get_request_paramter(request, 'repo', '1')) tagname = get_request_paramter(request, 'tag') context = RequestContext(request) context['form'] = PatchNewForm(repoid, tagname) return render_to_response("patch/patchnew.html", context)
def report_fix(request, report_id): report = get_object_or_404(Report, id=report_id) if request.method == "POST": src = get_request_paramter(request, 'src', '') if len(src) == 0: return HttpResponse('FIX: report, ERROR: no source specified') sfile = report.sourcefile() if not os.path.exists(sfile) or not os.path.isfile(sfile): return HttpResponse('FIX: report, ERROR: %s does not exists' % sfile) try: rtype = report.type repo = report.tag.repo srcfile = open(sfile, "w") try: src = srcfile.write(src) except: src = srcfile.write(unicode.encode(src, 'utf-8')) srcfile.close() diff = _get_diff_and_revert(repo.dirname(), report.file) user = report.username() email = report.email() title = rtype.ptitle desc = rtype.pdesc formater = PatchFormater(repo.dirname(), report.file, user, email, title, desc, diff) report.content = formater.format_patch() report.title = formater.format_title() report.desc = formater.format_desc() report.emails = formater.get_mail_list() report.module = formater.get_module() report.diff = diff report.build = 0 report.status = STATUS_PATCHED report.save() return HttpResponse('FIX: report %d, SUCCEED' % report.id, True) except: return HttpResponse('FIX: report, ERROR: write file error') else: context = RequestContext(request) sfile = report.sourcefile() src = '' if os.path.exists(sfile) and os.path.isfile(sfile): srcfile = open(sfile, "r") src = srcfile.read() srcfile.close() if report.diff != None and len(report.diff) != 0: try: tmpsrcfname = tempfile.mktemp() tmpsrcfile = open(tmpsrcfname, "w") tmpsrcfile.write(src) tmpsrcfile.close() tmpdiffname = tempfile.mktemp() tmpdiffile = open(tmpdiffname, "w") tmpdiffile.write(report.diff) tmpdiffile.close() os.system('patch %s -i %s' % (tmpsrcfname, tmpdiffname)) srcfile = open(tmpsrcfname, "r") src = srcfile.read() srcfile.close() os.unlink(tmpsrcfname) os.unlink(tmpdiffname) except: pass context['report'] = report context['src'] = src return render_to_response("report/reportfix.html", context)
def report_fix(request, report_id): report = get_object_or_404(Report, id=report_id) if request.method == "POST": src = get_request_paramter(request, 'src', '') if len(src) == 0: return HttpResponse('FIX: report, ERROR: no source specified') sfile = report.sourcefile() if not os.path.exists(sfile) or not os.path.isfile(sfile): return HttpResponse('FIX: report, ERROR: %s does not exists' % sfile) try: rtype = report.type repo = report.tag.repo srcfile = open(sfile, "w") try: src = srcfile.write(src) except: src = srcfile.write(unicode.encode(src, 'utf-8')) srcfile.close() diff = _get_diff_and_revert(repo.dirname(), report.file) user = report.username() email = report.email() title = rtype.ptitle desc = rtype.pdesc formater = PatchFormater(repo.dirname(), report.file, user, email, title, desc, diff) report.content = formater.format_patch() report.title = formater.format_title() report.desc = formater.format_desc() report.emails = formater.get_mail_list() report.module = formater.get_module() report.diff = diff report.build = 0 report.status = STATUS_PATCHED report.save() return HttpResponse('FIX: report %d, SUCCEED' % report.id, True) except: return HttpResponse('FIX: report, ERROR: write file error') else: context = RequestContext(request) sfile = report.sourcefile() src = '' if os.path.exists(sfile) and os.path.isfile(sfile): srcfile = open(sfile, "r") src = srcfile.read() srcfile.close() if report.diff != None and len(report.diff) != 0: try: tmpsrcfname = tempfile.mktemp() tmpsrcfile = open(tmpsrcfname, "w") tmpsrcfile.write(src) tmpsrcfile.close() tmpdiffname = tempfile.mktemp() tmpdiffile = open(tmpdiffname, "w") tmpdiffile.write(report.diff) tmpdiffile.close() os.system('patch %s -i %s' % (tmpsrcfname, tmpdiffname)) srcfile = open(tmpsrcfname, "r") src = srcfile.read() srcfile.close() os.unlink(tmpsrcfname) os.unlink(tmpdiffname) except: pass context['report'] = report context['src'] = src return render_to_response("report/reportfix.html", context)
def check_patch(repo, git, rtag, flists, commit): count = 0 scaninfo = [] logs = ScanLog(reponame = repo.name, tagname = rtag.name, starttime = strftime("%Y-%m-%d %H:%M:%S", localtime()), desc = 'Processing, please wait...') logs.save() logger = MyLogger() logger.logger.info('%d Files changed' % len(flists)) #logger.logger.info('=' * 40) #logger.logger.info('%s' % '\n'.join(flists)) #logger.logger.info('=' * 40) sche_weekend_enable = read_config('patch.schedule.weekend.enable', True) sche_weekend_limit = read_config('patch.schedule.weekend.limit', 600) sche_weekend_delta = read_config('patch.schedule.weekend.delta', 90) sche_obsolete_skip = read_config('patch.schedule.obsolete.skip', False) weekday = datetime.datetime.now().weekday() for dot in patch_engine_list(): scount = 0 test = dot(repo.dirname(), logger.logger, repo.builddir()) for i in range(test.tokens()): try: rtype = None try: rtype = Type.objects.filter(id = test.get_type())[0] except: test.next_token() continue if rtype.status == False: test.next_token() continue if (rtype.flags & TYPE_SCAN_NEXT_ONLY) != 0 and not git.is_linux_next(): test.next_token() continue if rtype.type == 0 and sche_weekend_enable is True and len(flists) > sche_weekend_limit and weekday < 5: # if we does not have a patch for this cleanup type in # sche_weekend_limit days, schedule scan only on weekend stime = datetime.datetime.now() - datetime.timedelta(days=sche_weekend_delta) if Patch.objects.filter(type = rtype, date__gte=stime).count() == 0: logger.info('Delay scan type %d to weekend' % test.get_type()) test.next_token() continue cmts = GitCommit.objects.filter(repo = repo, type = rtype) if len(cmts) == 0: cmt = GitCommit(repo = repo, type = rtype) cmt.save() else: cmt = cmts[0] if cmt.commit == commit: test.next_token() continue if repo.delta == False: oldcommit = cmt.commit if oldcommit != repo.commit: if git.is_linux_next(): oldcommit = git.get_stable() rflists = git.get_changelist(oldcommit, commit, None, True) else: rflists = flists else: rflists = flists if rtype.type == 0 and sche_weekend_enable is True and len(rflists) > sche_weekend_limit and weekday < 5: stime = datetime.datetime.now() - datetime.timedelta(days=sche_weekend_delta) if Patch.objects.filter(type = rtype, date__gte=stime).count() == 0: logger.info('Delay scan type %d to weekend' % test.get_type()) test.next_token() continue logger.info('Starting scan type %d, total %d files' % (test.get_type(), len(rflists))) exceptfiles = [] for fn in ExceptFile.objects.filter(type = rtype): exceptfiles.append(fn.file) pcount = 0 for sfile in rflists: if not is_source_file(sfile): continue if exceptfiles.count(sfile) != 0: logger.logger.info('skip except file %s, type %d' % (sfile, rtype.id)) continue # treat patch marked with Rejected as except file if Patch.objects.filter(file = sfile, type = rtype, status = STATUS_REJECTED).count() > 0: continue # treat patch marked with Applied and commit is '' as EXISTS patch if Patch.objects.filter(file = sfile, type = rtype, status = STATUS_ACCEPTED, commit = '').count() > 0: continue patchs = Patch.objects.filter(file = sfile, type = rtype) rpatchs = [] for p in patchs: if not p.status in [STATUS_NEW, STATUS_SENT, STATUS_MARKED]: continue rpatchs.append(p) test.set_filename(sfile) # source file maybe removed if not os.path.exists(test._get_file_path()): for p in rpatchs: p.status = STATUS_REMOVED p.save() continue # if the same file has a patch for this type, ignore it # because the last patch does not accepted should_patch = test.should_patch() if test.has_error(): continue if len(rpatchs) != 0 and should_patch == False: for p in rpatchs: if p.status == STATUS_SENT: p.status = STATUS_ACCEPTED elif p.mergered != 0: mpatch = Patch.objects.filter(id = p.mergered) if len(mpatch) != 0: if mpatch[0].status == STATUS_SENT: mpatch[0].status = STATUS_ACCEPTED p.status = STATUS_ACCEPTED else: mpatch[0].status = STATUS_FIXED p.status = STATUS_FIXED mpatch[0].save() else: p.status = STATUS_FIXED else: p.status = STATUS_FIXED p.save() if should_patch == True and len(rpatchs) == 0: text = test.get_patch() if (rtype.flags & TYPE_CHANGE_DATE_CHECK) == TYPE_CHANGE_DATE_CHECK: if git.is_change_obsoleted(sfile, text) is True: continue elif rtype.id > 3000 and rtype.type == 0 and sche_obsolete_skip is True: if git.is_change_obsoleted(sfile, text) is True: logger.logger.info('skip obsoleted file %s, type %d' % (sfile, rtype.id)) continue patch = Patch(tag = rtag, file = sfile, type = rtype, status = STATUS_NEW, diff = text) patch.save() # format patch and cache to patch user = patch.username() email = patch.email() desc = test.get_patch_description() title = test.get_patch_title() if desc is None: desc = rtype.pdesc if title is None: title = rtype.ptitle formater = PatchFormater(repo.dirname(), sfile, user, email, title, desc, text) patch.content = formater.format_patch() patch.title = formater.format_title() patch.desc = formater.format_desc() patch.emails = formater.get_mail_list() patch.module = formater.get_module() patch.save() scount += 1 pcount += 1 cmt.commit = commit cmt.save() except: logger.info('Scan ERROR: type %d' % test.get_type()) logger.info('End scan type %d, patch %d' % (test.get_type(), pcount)) logs.logs = logger.getlog() logs.save() test.next_token() count += scount scaninfo.append("%s: %d" % (test.name(), scount)) scaninfo.append("total: %d" % (count)) logs.desc = ', '.join(scaninfo) logs.endtime = strftime("%Y-%m-%d %H:%M:%S", localtime()) logs.logs = logger.getlog() logs.save() return count
def patch_new(request): if request.method == "POST": tagid = get_request_paramter(request, 'tag') typeid = get_request_paramter(request, 'type') rfile = get_request_paramter(request, 'file') rtags = GitTag.objects.filter(id=tagid) if len(rtags) == 0: logevent("NEW: patch , ERROR: tag id %s does not exists" % tagid) return HttpResponse( 'NEW: patch, ERROR: tag id %s does not exists' % tagid) rtypes = Type.objects.filter(id=typeid) if len(rtypes) == 0: logevent("NEW: patch , ERROR: type id %s does not exists" % typeid) return HttpResponse( 'NEW: patch, ERROR: type id %s does not exists' % typeid) rtype = rtypes[0] patch = Patch(tag=rtags[0], type=rtype, file=rfile, status=STATUS_NEW, diff='') if not os.path.exists(patch.sourcefile()): logevent("NEW: patch , ERROR: type id %s does not exists" % typeid) return HttpResponse( 'NEW: patch, ERROR: type id %s does not exists' % typeid) patch.save() for dot in patch_engine_list(): test = dot(rtags[0].repo.dirname(), None, rtags[0].repo.builddir()) for i in range(test.tokens()): if test.get_type() != rtype.id: test.next_token() continue test.set_filename(rfile) if test.should_patch(): text = test.get_patch() patch.diff = text user = patch.username() email = patch.email() desc = test.get_patch_description() title = test.get_patch_title() if desc is None: desc = rtype.pdesc if title is None: title = rtype.ptitle formater = PatchFormater(rtags[0].repo.dirname(), rfile, user, email, title, desc, text) patch.content = formater.format_patch() patch.title = formater.format_title() patch.desc = formater.format_desc() patch.emails = formater.get_mail_list() patch.module = formater.get_module() patch.save() break rtags[0].total += 1 rtags[0].save() logevent("NEW: patch for %s, SUCCEED: new id %s" % (rfile, patch.id), True) return HttpResponse('NEW: patch for file, SUCCEED') else: repoid = int(get_request_paramter(request, 'repo', '1')) tagname = get_request_paramter(request, 'tag') context = RequestContext(request) context['form'] = PatchNewForm(repoid, tagname) return render_to_response("patch/patchnew.html", context)
def patch_fix(request, patch_id): patch = get_object_or_404(Patch, id=patch_id) if request.method == "POST": src = get_request_paramter(request, 'src', '') if len(src) == 0: return HttpResponse('FIX: report, ERROR: no source specified') sfile = patch.sourcefile() if not os.path.exists(sfile) or not os.path.isfile(sfile): return HttpResponse('FIX: report, ERROR: %s does not exists' % sfile) try: rtype = patch.type repo = patch.tag.repo srcfile = open(sfile, "w") try: src = srcfile.write(src) except: src = srcfile.write(unicode.encode(src, 'utf-8')) srcfile.close() diff = _get_diff_and_revert(repo.dirname(), patch.file) user = patch.username() email = patch.email() if re.search(r'{{[^}]*}}', rtype.ptitle): title = rtype.ptitle else: title = patch.title if re.search(r'{{[^}]*}}', rtype.pdesc): desc = rtype.pdesc else: desc = patch.desc formater = PatchFormater(repo.dirname(), patch.file, user, email, title, desc, diff) patch.content = formater.format_patch() patch.title = formater.format_title() patch.desc = formater.format_desc() patch.emails = formater.get_mail_list() if patch.diff != diff: patch.diff = diff patch.status = STATUS_NEW patch.build = 0 patch.save() return HttpResponse('FIX: patch %d, SUCCEED' % patch.id, True) except: return HttpResponse('FIX: patch, ERROR: write file error') else: context = RequestContext(request) sfile = patch.sourcefile() src = '' if os.path.exists(sfile) and os.path.isfile(sfile): srcfile = open(sfile, "r") src = srcfile.read() srcfile.close() try: tmpsrcfname = tempfile.mktemp() tmpsrcfile = open(tmpsrcfname, "w") tmpsrcfile.write(src) tmpsrcfile.close() tmpdiffname = tempfile.mktemp() tmpdiffile = open(tmpdiffname, "w") tmpdiffile.write(patch.diff) tmpdiffile.close() os.system('patch %s -i %s' % (tmpsrcfname, tmpdiffname)) srcfile = open(tmpsrcfname, "r") src = srcfile.read() srcfile.close() os.unlink(tmpsrcfname) os.unlink(tmpdiffname) except: pass context['patch'] = patch context['src'] = src return render_to_response("patch/patchfix.html", context)