def DoDiff(self, filename): # open files (use in-memmory file-like buffer for working base) file_old = cStringIO.StringIO(self.repo.cat(filename)) file_new = open(filename, "U") # read files returning proper unicode text and other flags old_text, old_encoding, old_bom, old_eol, old_nl = \ fileutil.unicode_file_read(file_old, encoding=None) new_text, new_encoding, new_bom, new_eol, new_nl = \ fileutil.unicode_file_read(file_new, encoding=None) # normalize newlines (eol changes not supported by wxPyDiff/SM by now) nl = '\r\n' if old_nl != nl: old_text = old_text.replace(old_nl, nl) if new_nl != nl: new_text = new_text.replace(new_nl, nl) # re-encode unicode to same encoding old_text = old_text.encode("utf8") new_text = new_text.encode("utf8") # render the diff from wxpydiff import PyDiff PyDiff(None, 'wxPyDiff', "repository", filename, old_text, new_text)
def OnDiffPSP(self, event): "Event to calc diff and update metadata" # this is a temporary and auxiliar function just to rebuild metadata if self.active_child: import fileutil import locutil filename = self.active_child.GetFilename() if filename: with open(filename, "r") as f: new_text, encoding, bom, eol, new_newlines = fileutil.unicode_file_read( f, "utf8") dlg = wx.TextEntryDialog(self, 'Compare with:', 'PSP DIFF', filename) if dlg.ShowModal() == wx.ID_OK: old_filename = dlg.GetValue() with open(old_filename, "r") as f: old_text, encoding, bom, eol, old_newlines = fileutil.unicode_file_read( f, "utf8") else: old_filename = '' old_text = u'' old_newlines = '\n' dlg.Destroy() # re-encode unicode to same encoding #old_text = old_text.encode("utf8") #new_text = new_text.encode("utf8") # render the diff from wxpydiff import PyDiff PyDiff(None, 'wxPyDiff (PSP)', old_filename, filename, old_text, new_text) # compare old and new lines: old_lines = old_text.split(old_newlines) new_lines = new_text.split(new_newlines) changes = locutil.analize_line_changes(old_lines, new_lines) objects, locs = locutil.count_logical_lines_per_object(filename, changes=changes) # add filename to each object (TODO: check several files) for obj in objects: obj.append(filename) # send metrics to remote server self.psp_update_project(locs, objects)
def OnDiffPSP(self, event): "Event to calc diff and update metadata" # this is a temporary and auxiliar function just to rebuild metadata if self.active_child: import fileutil import locutil filename = self.active_child.GetFilename() if filename: with open(filename, "r") as f: new_text, encoding, bom, eol, new_newlines = fileutil.unicode_file_read(f, "utf8") dlg = wx.TextEntryDialog(self, 'Compare with:', 'PSP DIFF', filename) if dlg.ShowModal() == wx.ID_OK: old_filename = dlg.GetValue() with open(old_filename, "r") as f: old_text, encoding, bom, eol, old_newlines = fileutil.unicode_file_read(f, "utf8") else: old_filename = '' old_text = u'' old_newlines = '\n' dlg.Destroy() # re-encode unicode to same encoding #old_text = old_text.encode("utf8") #new_text = new_text.encode("utf8") # render the diff from wxpydiff import PyDiff PyDiff(None, 'wxPyDiff (PSP)', old_filename, filename, old_text, new_text) # compare old and new lines: old_lines = old_text.split(old_newlines) new_lines = new_text.split(new_newlines) changes = locutil.analize_line_changes(old_lines, new_lines) objects, locs = locutil.count_logical_lines_per_object(filename, changes=changes) # add filename to each object (TODO: check several files) for obj in objects: obj.append(filename) # send metrics to remote server self.psp_update_project(locs, objects)
def update_metadata(self, filename, show=False): import fileutil import diffutil # check if file was modified (cache stale): timestamp, metadata = self.psp_metadata_cache.get(filename, (None, None)) if timestamp is None or timestamp != os.stat(filename).st_mtime: timestamp = os.stat(filename).st_mtime metadata = None # if not metadata valid in cached, rebuild it: if metadata is None: # read current text file and split lines with open(filename, "r") as f: text, encoding, bom, eol, newlines = fileutil.unicode_file_read(f, "utf8") # prepare new text lines new = text.split(newlines) # check to see if there is old metadata fn_hash = hashlib.sha224(filename).hexdigest() metadata_fn = os.path.join(self.psp_metadata_dir, "%s.dat" % fn_hash) if not os.path.exists(metadata_fn): # create metadata metadata = dict([(i, (self.current_psp_phase, l)) for i, l in enumerate(new)]) else: with open(metadata_fn, 'rb') as pkl: old_metadata = pickle.load(pkl) # get old text lines old = [l for (phase, l) in old_metadata.values()] # compare new and old lines changes = diffutil.track_lines_changes(old, new) new_metadata = {} for old_lno, new_lno in changes: if new_lno is not None and old_lno is None: # new or replaced, change metadata new_metadata[new_lno] = self.current_psp_phase , new[new_lno] elif new_lno is not None and old_lno is not None: # equal, maintain metadata new_metadata[new_lno] = old_metadata[old_lno][0], new[new_lno] else: # deleted, do not copy to new metadata pass metadata = new_metadata with open(metadata_fn, 'wb') as pkl: pickle.dump(metadata, pkl) self.psp_metadata_cache[filename] = timestamp, metadata if show: msg = '\n'.join(["%10s - %s" % metadata[key] for key in sorted(metadata.keys())]) dlg = wx.lib.dialogs.ScrolledMessageDialog(self, msg, "PSP METADATA") dlg.ShowModal() dlg.Destroy() return metadata
def LoadFile(self, filename, encoding=None): "Replace STC.LoadFile for non-unicode files and BOM support" # open the file with universal line-endings support f = None try: if self.debugger and self.debugger.is_remote(): f = self.debugger.ReadFile(filename) readonly = True else: f = open(filename, "Ur") readonly = False # analyze encoding and line ending, get text properly decoded text, encoding, bom, eol, nl = fileutil.unicode_file_read(f, encoding) # store internal values for future reference self.encoding = encoding self.bom = bom self.eol = eol # set line endings mode self.SetEOLMode(self.eol) # load text (unicode!) self.SetText(text) # generate internal uuid + column origin (0) for each line: self.uuids = [[str(uuid.uuid1()), 0] for i in range(self.GetLineCount())] # remote text cannot be modified: if readonly: self.SetReadOnly(True) return True except Exception, e: dlg = wx.MessageDialog(self, unicode(e), "Unable to Load File", wx.OK | wx.ICON_EXCLAMATION) dlg.ShowModal() dlg.Destroy() return False
def LoadFile(self, filename, encoding=None): "Replace STC.LoadFile for non-unicode files and BOM support" # open the file with universal line-endings support f = None try: if self.debugger and self.debugger.is_remote(): f = self.debugger.ReadFile(filename) readonly = True else: f = open(filename, "Ur") readonly = False # analyze encoding and line ending, get text properly decoded text, encoding, bom, eol, nl = fileutil.unicode_file_read( f, encoding) # store internal values for future reference self.encoding = encoding self.bom = bom self.eol = eol # set line endings mode self.SetEOLMode(self.eol) # load text (unicode!) self.SetText(text) # remote text cannot be modified: if readonly: self.SetReadOnly(True) return True except Exception, e: dlg = wx.MessageDialog(self, unicode(e), "Unable to Load File", wx.OK | wx.ICON_EXCLAMATION) dlg.ShowModal() dlg.Destroy() return False
def update_metadata(self, filename, show=False): import fileutil import diffutil # check if file was modified (cache stale): timestamp, metadata = self.psp_metadata_cache.get( filename, (None, None)) if timestamp is None or timestamp != os.stat(filename).st_mtime: timestamp = os.stat(filename).st_mtime metadata = None # if not metadata valid in cached, rebuild it: if metadata is None: # read current text file and split lines with open(filename, "r") as f: text, encoding, bom, eol, newlines = fileutil.unicode_file_read( f, "utf8") # prepare new text lines new = text.split(newlines) # check to see if there is old metadata fn_hash = hashlib.sha224(filename).hexdigest() metadata_fn = os.path.join(self.psp_metadata_dir, "%s.dat" % fn_hash) if not os.path.exists(metadata_fn): # create metadata metadata = dict([(i, (self.current_psp_phase, l)) for i, l in enumerate(new)]) else: with open(metadata_fn, 'rb') as pkl: old_metadata = pickle.load(pkl) # get old text lines old = [l for (phase, l) in old_metadata.values()] # compare new and old lines changes = diffutil.track_lines_changes(old, new) new_metadata = {} for old_lno, new_lno in changes: if new_lno is not None and old_lno is None: # new or replaced, change metadata new_metadata[new_lno] = self.current_psp_phase, new[ new_lno] elif new_lno is not None and old_lno is not None: # equal, maintain metadata new_metadata[new_lno] = old_metadata[old_lno][0], new[ new_lno] else: # deleted, do not copy to new metadata pass metadata = new_metadata with open(metadata_fn, 'wb') as pkl: pickle.dump(metadata, pkl) self.psp_metadata_cache[filename] = timestamp, metadata if show: msg = '\n'.join([ "%10s - %s" % metadata[key] for key in sorted(metadata.keys()) ]) dlg = wx.lib.dialogs.ScrolledMessageDialog(self, msg, "PSP METADATA") dlg.ShowModal() dlg.Destroy() return metadata