def _handle_preview(self): data = self.request.get('data').replace('\t', ' ').replace('\r\n', '\n') path = self.request.get('path') title = self.request.get('title') old_file = File.from_path(path) new_lines = [line+'\n' for line in data.splitlines()] old_lines = [line+'\n' for line in old_file.data.splitlines()] diff = difflib.unified_diff(old_lines, new_lines, 'a/'+path, 'b/'+path) diff_data = ''.join(diff) lexer = pygments.lexers.get_lexer_by_name('diff') formatter = pygments.formatters.get_formatter_by_name('html', nobackground=True) highlighted = pygments.highlight(diff_data, lexer, formatter) user_props = Proposal.all().filter('state', 'private').filter('user', user.user).fetch(100) add_stylesheet(self.request, '/pygments.css') self.response.out.write(self.env.get_template('proposal_preview.html').render(locals()))
def get(self): path = self.request.get('path') title = path f = File.from_path(path) if not f: send_error(self, '%s not found', path) return mime_type, encoding = mimetypes.guess_type(f.path, False) if mime_type.startswith('text'): data = f.data.replace(' ', '\t') add_script(self.request, 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js') add_script(self.request, '/htdocs/jquery.tabby.js') self.response.out.write(self.env.get_template('proposal_create.html').render(locals())) else: user_props = Proposal.all().filter('state', 'private').filter('user', user.user).fetch(100) self.response.out.write(self.env.get_template('proposal_create_binary.html').render(locals()))
def checkfile(self, filename, hunks): """ Check if file is alrady patched """ matched = True fdb = File.from_path(filename) fp = StringIO(fdb.data) class NoMatch(Exception): pass lineno = 1 line = fp.readline() hno = None try: if not len(line): raise NoMatch for hno, h in enumerate(hunks): # skip to line just before hunk starts while lineno < h.starttgt-1: line = fp.readline() lineno += 1 if not len(line): raise NoMatch for hline in h.text: # todo: \ No newline at the end of file if not hline.startswith("-") and not hline.startswith("\\"): line = fp.readline() lineno += 1 if not len(line): raise NoMatch if line.rstrip("\r\n") != hline[1:].rstrip("\r\n"): warning("file is not patched - failed hunk: %d" % (hno+1)) raise NoMatch except NoMatch: matched = False # todo: display failed hunk, i.e. expected/found fp.close() return matched
def apply(self): """ apply parsed patch """ total = len(self.source) for fileno, filename in enumerate(self.source): f2db = File.from_path(filename) if not f2db: continue info("processing %d/%d:\t %s" % (fileno+1, total, filename)) # validate before patching f2fp = StringIO(f2db.data) hunkno = 0 hunk = self.hunks[fileno][hunkno] hunkfind = [] hunkreplace = [] validhunks = 0 canpatch = False for lineno, line in enumerate(f2fp): if lineno+1 < hunk.startsrc: continue elif lineno+1 == hunk.startsrc: hunkfind = [x[1:].rstrip("\r\n") for x in hunk.text if x[0] in " -"] hunkreplace = [x[1:].rstrip("\r\n") for x in hunk.text if x[0] in " +"] #pprint(hunkreplace) hunklineno = 0 # todo \ No newline at end of file # check hunks in source file if lineno+1 < hunk.startsrc+len(hunkfind)-1: if line.rstrip("\r\n") == hunkfind[hunklineno]: hunklineno+=1 else: debug("hunk no.%d doesn't match source file %s" % (hunkno+1, filename)) # file may be already patched, but we will check other hunks anyway hunkno += 1 if hunkno < len(self.hunks[fileno]): hunk = self.hunks[fileno][hunkno] continue else: break # check if processed line is the last line if lineno+1 == hunk.startsrc+len(hunkfind)-1: debug("file %s hunk no.%d -- is ready to be patched" % (filename, hunkno+1)) hunkno+=1 validhunks+=1 if hunkno < len(self.hunks[fileno]): hunk = self.hunks[fileno][hunkno] else: if validhunks == len(self.hunks[fileno]): # patch file canpatch = True break else: if hunkno < len(self.hunks[fileno]): warning("premature end of source file %s at hunk %d" % (filename, hunkno+1)) f2fp.close() if validhunks < len(self.hunks[fileno]): if self.checkfile(filename, self.hunks[fileno]): warning("already patched %s" % filename) else: warning("source file is different - %s" % filename) if canpatch: if self.writehunks(f2db, self.hunks[fileno]): info("successfully patched %s" % filename) else: warning("error patching file %s" % filename)