from cvsgui.Macro import * from cvsgui.CvsEntry import * from cvsgui.ColorConsole import * import cvsgui.App, os.path, os import zipfile, string import stat import re class RenameSelected(Macro): def __init__(self): Macro.__init__(self, "Rename selected file on CVS-Entries", MACRO_SELECTION, 0, "Rename") def OnCmdUI(self, cmdui): # enable only if 1 regular folder and only 1 file is selected sel = cvsgui.App.GetSelection() isOnlyExistingFiles = len(sel) > 0 if isOnlyExistingFiles: if not sel[0].IsFile() or sel[0].IsMissing(): isOnlyExistingFiles = 0 cmdui.Enable(isOnlyExistingFiles) if isOnlyExistingFiles: cmdui.SetText("Rename '" + sel[0].GetName() + "' on local repository") else: cmdui.SetText("Rename file on local repository") def Run(self): print "Renaming selected file..." # at this point we are sure we have only one regular folder sel = cvsgui.App.GetSelection() # find the top path of the selection and entry name entrypath = os.getcwd() entryname = sel[0].GetName() entryfile = entrypath + os.sep + "CVS" + os.sep + "Entries" msg = "Please enter the new name for the file:" title = "Rename entry" newname = entryname res, newname = cvsgui.App.PromptEditMessage(msg, newname, title) if res and len(newname) > 0: console = ColorConsole() console << kMagenta << "\tRenaming " << entryname << kNormal << " to " << kMagenta << newname << kNormal << '\n' # rename entry on cvs Entries file entryfilehandle = file(entryfile, 'r') oldcontent = entryfilehandle.read() entryfilehandle.close() newcontent = re.sub(entryname, newname, oldcontent) entryfilehandle = file(entryfile, 'w', 0) entryfilehandle.write(newcontent) entryfilehandle.close # rename file on local repository os.chdir(entrypath) os.rename(entryname, newname) print "%d file renamed." % (len(sel)) else: console << kRed << "\t\tOperation aborted. No new name for" << entryname << kNormal << '\n' RenameSelected()