def show_patch(self, prefix, patch, silent=False): tmpdir = tempfile.mkdtemp("-meld") self.tempdirs.append(tmpdir) diffs = [] for fname in self.vc.get_patch_files(patch): destfile = os.path.join(tmpdir,fname) destdir = os.path.dirname( destfile ) if not os.path.exists(destdir): os.makedirs(destdir) pathtofile = os.path.join(prefix, fname) try: shutil.copyfile( pathtofile, destfile) except IOError: # it is missing, create empty file open(destfile,"w").close() diffs.append( (destfile, pathtofile) ) patchcmd = self.vc.patch_command(tmpdir) try: result = misc.write_pipe(patchcmd, patch, error=misc.NULL) except OSError: result = 1 if result == 0: for d in diffs: os.chmod(d[0], 0444) self.emit("create-diff", d) return True elif not silent: import meldapp msg = _(""" Invoking 'patch' failed. Maybe you don't have 'GNU patch' installed, or you use an untested version of %s. Please send email bug report to: [email protected] Containing the following information: - meld version: '%s' - source control software type: '%s' - source control software version: 'X.Y.Z' - the output of '%s somefile.txt' - patch command: '%s' (no need to actually run it, just provide the command line) Replace 'X.Y.Z' by the actual version for the source control software you use. """) % (self.vc.NAME, meldapp.version, self.vc.NAME, " ".join(self.vc.diff_command()), " ".join(patchcmd)) msg = '\n'.join([line.strip() for line in msg.split('\n')]) misc.run_dialog(msg, parent=self) return False
def _command_iter(self, command, files, refresh): """Run 'command' on 'files'. Return a tuple of the directory the command was executed in and the output of the command. """ msg = misc.shelljoin(command) yield "[%s] %s" % (self.label_text, msg.replace("\n", u"\u21b2") ) def relpath(pbase, p): kill = 0 if len(pbase) and p.startswith(pbase): kill = len(pbase) + 1 return p[kill:] or "." if len(files) == 1 and os.path.isdir(files[0]): workdir = self.vc.get_working_directory(files[0]) else: workdir = self.vc.get_working_directory( _commonprefix(files) ) files = [ relpath(workdir, f) for f in files ] r = None self.consolestream.write( misc.shelljoin(command+files) + " (in %s)\n" % workdir) readfunc = misc.read_pipe_iter(command + files, self.consolestream, workdir=workdir).next try: while r is None: r = readfunc() self.consolestream.write(r) yield 1 except IOError, e: misc.run_dialog("Error running command.\n'%s'\n\nThe error was:\n%s" % ( misc.shelljoin(command), e), parent=self, messagetype=gtk.MESSAGE_ERROR)
def append_diff(self, paths, auto_compare=False): dirslist = [p for p in paths if os.path.isdir(p)] fileslist = [p for p in paths if os.path.isfile(p)] if dirslist and fileslist: # build new file list appending previous found filename to dirs (like diff) lastfilename = fileslist[0] builtfilelist = [] for elem in paths: if os.path.isdir(elem): builtfilename = os.path.join(elem, lastfilename) if os.path.isfile(builtfilename): elem = builtfilename else: # exit at first non found directory + file misc.run_dialog(_("Cannot compare a mixture of files and directories.\n"), parent=self, buttonstype=gtk.BUTTONS_OK) return else: lastfilename = os.path.basename(elem) builtfilelist.append(elem) return self.append_filediff(builtfilelist) elif dirslist: return self.append_dirdiff(paths, auto_compare) else: return self.append_filediff(paths)
def append_diff(self, paths, auto_compare=False): aredirs = [ os.path.isdir(p) for p in paths ] arefiles = [ os.path.isfile(p) for p in paths ] if (1 in aredirs) and (1 in arefiles): misc.run_dialog( _("Cannot compare a mixture of files and directories.\n"), parent = self, buttonstype = gtk.BUTTONS_OK) elif 1 in aredirs: return self.append_dirdiff(paths, auto_compare) else: return self.append_filediff(paths)
def append_diff(self, paths, auto_compare=False): aredirs = [os.path.isdir(p) for p in paths] arefiles = [os.path.isfile(p) for p in paths] if (1 in aredirs) and (1 in arefiles): misc.run_dialog( _("Cannot compare a mixture of files and directories.\n"), parent=self, buttonstype=gtk.BUTTONS_OK) elif 1 in aredirs: return self.append_dirdiff(paths, auto_compare) else: return self.append_filediff(paths)
def on_button_delete_clicked(self, obj): files = self._get_selected_files() for name in files: try: if os.path.isfile(name): os.remove(name) elif os.path.isdir(name): if misc.run_dialog(_("'%s' is a directory.\nRemove recursively?") % os.path.basename(name), parent = self, buttonstype=gtk.BUTTONS_OK_CANCEL) == gtk.RESPONSE_OK: shutil.rmtree(name) except OSError, e: misc.run_dialog(_("Error removing %s\n\n%s.") % (name,e), parent = self)
def run_diff_iter(self, path_list, empty_patch_ok): yield _("[%s] Fetching differences") % self.label_text difffunc = self._command_iter(self.vc.diff_command(), path_list, 0).next diff = None while type(diff) != type(()): diff = difffunc() yield 1 prefix, patch = diff[0], diff[1] yield _("[%s] Applying patch") % self.label_text if patch: self.show_patch(prefix, patch) elif empty_patch_ok: misc.run_dialog( _("No differences found."), parent=self, messagetype=gtk.MESSAGE_INFO) else: for path in path_list: self.emit("create-diff", [path])
def usage(self, msg): response = misc.run_dialog(msg, self, gtk.MESSAGE_ERROR, gtk.BUTTONS_NONE, [(gtk.STOCK_QUIT, gtk.RESPONSE_CANCEL), (gtk.STOCK_OK, gtk.RESPONSE_OK)]) if response == gtk.RESPONSE_CANCEL: sys.exit(0)
def usage(self, msg): response = misc.run_dialog(msg, self, gtk.MESSAGE_ERROR, gtk.BUTTONS_NONE, [(gtk.STOCK_QUIT, gtk.RESPONSE_CANCEL), (gtk.STOCK_OK, gtk.RESPONSE_OK)] ) if response == gtk.RESPONSE_CANCEL: sys.exit(0)
def _find_text(self, start_offset=1, backwards=False, wrap=True): match_case = self.match_case.get_active() whole_word = self.whole_word.get_active() regex = self.regex.get_active() assert self.textview buf = self.textview.get_buffer() insert = buf.get_iter_at_mark( buf.get_insert() ) tofind_utf8 = self.find_entry.get_text() tofind = tofind_utf8.decode("utf-8") # tofind is utf-8 encoded text = buf.get_text(*buf.get_bounds() ).decode("utf-8") # as is buffer if not regex: tofind = re.escape(tofind) if whole_word: tofind = r'\b' + tofind + r'\b' try: pattern = re.compile( tofind, (match_case and re.M or (re.M|re.I)) ) except re.error, e: misc.run_dialog( _("Regular expression error\n'%s'") % e, self, messagetype=gtk.MESSAGE_ERROR)
def _command_on_selected(self, command, refresh=1): files = self._get_selected_files() if len(files): self._command(command, files, refresh) else: misc.run_dialog( _("Select some files first."), parent=self, messagetype=gtk.MESSAGE_INFO)
try: ignored = open(os.path.join(os.environ["HOME"], ".cvsignore")).read().split() except (IOError, KeyError): ignored = [] try: ignored += open( os.path.join(directory, ".cvsignore")).read().split() except IOError: pass if len(ignored): try: regexes = [ misc.shell_to_regex(i)[:-1] for i in ignored ] ignore_re = re.compile( "(" + "|".join(regexes) + ")" ) except re.error, e: misc.run_dialog(_("Error converting to a regular expression\n" "The pattern was '%s'\n" "The error was '%s'") % (",".join(ignored), e)) else: class dummy(object): def match(self, *args): return None ignore_re = dummy() for f,path in files: if f not in cvsfiles: state = ignore_re.match(f) == None and _vc.STATE_NONE or _vc.STATE_IGNORED retfiles.append( _vc.File(path, f, state, "") ) for d,path in dirs: if d not in cvsfiles: state = ignore_re.match(d) == None and _vc.STATE_NONE or _vc.STATE_IGNORED retdirs.append( _vc.Dir(path, d, state) )
".cvsignore")).read().split() except (IOError, KeyError): ignored = [] try: ignored += open(os.path.join(directory, ".cvsignore")).read().split() except IOError: pass if len(ignored): try: regexes = [misc.shell_to_regex(i)[:-1] for i in ignored] ignore_re = re.compile("(" + "|".join(regexes) + ")") except re.error, e: misc.run_dialog( _("Error converting to a regular expression\n" "The pattern was '%s'\n" "The error was '%s'") % (",".join(ignored), e)) else: class dummy(object): def match(self, *args): return None ignore_re = dummy() for f, path in files: if f not in cvsfiles: state = ignore_re.match( f) == None and _vc.STATE_NONE or _vc.STATE_IGNORED retfiles.append(_vc.File(path, f, state, "")) for d, path in dirs: