Thread: [Cvsshell-devel] CVS: cvsshell/src cvs_cmds.py,NONE,1.1
Status: Beta
Brought to you by:
stefanheimann
From: Stefan H. <ste...@us...> - 2002-03-11 09:45:54
|
Update of /cvsroot/cvsshell/cvsshell/src In directory usw-pr-cvs1:/tmp/cvs-serv29992/src Added Files: cvs_cmds.py Log Message: initial checkin --- NEW FILE: cvs_cmds.py --- ############################################################################### # This file is part of CvsShell # # CvsShell is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # CvsShell is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with CvsShell; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Copyright 2002 by Stefan Heimann # Website: http://cvsshell.sourceforge.net/ ############################################################################### import os, re, string, getopt, utils from cvs_shell import CvsShell, Listing, Entry, CvsError, InternalCvsError from app import AppError ############################## # CVS commands ############################## def status(app, name, args): entries = [] regexHead = re.compile(r'File: (?P<name>\S+)\s+Status: (?P<status>.+)') regexDir = re.compile(r'.*Examining (?P<dir>\S+)') regexUnknown = re.compile(r'^\?\s+(?P<filename>\S+)') try: (globOpts, opts, myOpts, rest) = app.parseArgs(args, app.getCmdToAllowedOpts().get(name, '')) except getopt.GetoptError, msg: app.printErr(msg) return try: lines = app.runCvsCmd('status', globOpts=globOpts, args=opts, getStderr=1) except CvsError: return entries = [] dir = None for line in lines: unknownRes = regexUnknown.search(line) if unknownRes: name = os.path.basename(unknownRes.group('filename')) dir = os.path.dirname(unknownRes.group('filename')) entries.append(Entry(dir, name, Entry.S_UNKNOWN)) continue headRes = regexHead.search(line) if headRes and dir is None: raise InternalCvsError, "Parsing of status message failed: " \ "`dir' is not set when get file information." elif headRes: name = headRes.group('name') status = headRes.group('status').strip() entries.append(Entry(dir, name, status)) continue dirRes = regexDir.search(line) if dirRes: dir = dirRes.group('dir') if dir == '.': dir = '' app.setListing(Listing(app, os.getcwd(), entries)) app.setDirtyListing(0) app.getListing().sortEntries() app.printListing() def update(app, args, name, simulate=0): try: (globOpts, opts, myOpts, rest) = app.parseArgs(args, app.getCmdToAllowedOpts().get(name, '')) except getopt.GetoptError, msg: app.printErr(msg) return if simulate: globOpts += '-n' try: lines = app.runCvsCmd('update', globOpts=globOpts, args=opts) except CvsError: return entries = [] for line in lines: x = line.split() if len(x) < 2 or x[0] == 'cvs': continue status = x[0] name = os.path.basename(x[1]) dir = os.path.dirname(x[1]) entries.append(Entry(dir, name, status)) app.setListing(Listing(app, os.getcwd(), entries)) app.setDirtyListing(0) app.getListing().sortEntries() app.printListing() def simulateUpdate(app, name, args): update(app, name, args, simulate=1) def checkout(app, name, args): try: (globOpts, opts, myOpts, rest) = app.parseArgs(args, app.getCmdToAllowedOpts().get(name, '')) except getopt.GetoptError, msg: app.printErr(msg) return rest = rest.split() n = len(rest) cvsRoot = app.getCvsRoot() if cvsRoot is None: if n < 1: setCvsRoot(app, '', '') cvsRoot = app.getCvsRoot() else: cvsRoot = app.getCvsRootAliases().get(rest[0], rest[0]) if n < 2: module = app.prompt('enter name of module: ') if module is None: return else: module = rest[1] else: if n < 1: module = app.prompt('enter name of module: ') if module is None: return else: module = rest[0] try: app.runCvsCmd('checkout', cvsRoot=cvsRoot, globOpts=globOpts, args=opts+' '+module) except CvsError: pass app.setDirtyListing(1) def add(app, name, args, isBinary=0): try: (globOpts, opts, myOpts, rest) = app.parseArgs(args, app.getCmdToAllowedOpts().get(name, '')) except getopt.GetoptError, msg: app.printErr(msg) return if isBinary: opts += '-kb' if not rest: rest = app.prompt('enter filenumbers/filenames to add: ') if rest is None: return def __doIt(e, filename): e.status = Entry.S_ADDED return filename try: filenames = app.applyOnEntryList(rest, __doIt) try: if filenames: app.runCvsCmd('add', rootDir=app.getListing().getRootDir(), globOpts=globOpts, args=opts+' '+string.join(filenames)) except CvsError: pass except utils.ParseError: # args do not spefiy ids in the current listing try: app.runCvsCmd('add', globOpts=globOpts, args=opts+' '+rest) app.setDirtyListing(1) except CvsError: pass except AppError, msg: app.printErr(msg) def addBinary(app, name, args): app.printMsg('The file will be added in binary mode.') add(app, name, args, isBinary=1) def commit(app, name, args): try: (globOpts, opts, myOpts, rest) = app.parseArgs(args, app.getCmdToAllowedOpts().get(name, '')) except getopt.GetoptError, msg: app.printErr(msg) return if not rest: # commit without an argument try: if not app.getListing(): rootDir = '' else: rootDir = app.getListing().getRootDir() app.runCvsCmd('commit', rootDir=rootDir, globOpts=globOpts, args=opts, fork=0) app.setDirtyListing(1) except CvsError: pass return def __doIt(e,filename): if e.status == Entry.S_REMOVED: e.status = Entry.S_DELETED else: e.status = Entry.S_OK return filename try: filenames = app.applyOnEntryList(rest, __doIt) try: if filenames: app.runCvsCmd('commit', rootDir=app.getListing().getRootDir(), globOpts=globOpts, args=opts+' '+string.join(filenames), fork=0) except CvsError: pass except utils.ParseError: # args do not spefiy ids in the current listing try: app.runCvsCmd('commit', globOpts=globOpts, args=opts+' '+rest, fork=0) app.setDirtyListing(1) except CvsError: pass except AppError, msg: app.printErr(msg) def remove(app, name, args): try: (globOpts, opts, myOpts, rest) = app.parseArgs(args, app.getCmdToAllowedOpts().get(name, '')) except getopt.GetoptError, msg: app.printErr(msg) return if not rest: rest = app.prompt('enter filenumbers/filenames to remove: ') if rest is None: return def __doIt(e,filename): return e filenames = [] toDeleteListing = Listing(app, '') try: toDeleteListing.setEntries(app.applyOnEntryList(rest, __doIt)) toDeleteListing.setRootDir(app.getListing().getRootDir()) for e in toDeleteListing.entries: name = os.path.join(toDeleteListing.rootDir, e.dir, e.name) print name if os.path.isdir(name): app.printMsg("Refusing the delete a directory: %s" % name) continue try: os.unlink(name) except OSError, msg: app.printErr(msg) # Hope that the file already has been removed filenames.append(os.path.join(e.dir, e.name)) e.status = Entry.S_REMOVED except utils.ParseError: # args do not specify ids in the current listing fs = utils.splitquoted(rest) for name in fs: if os.path.isdir(name): app.printMsg("Refusing the delete a directory: %s" % name) continue try: os.unlink(name) except OSError, msg: app.printErr(msg) # Hope that the file already has been removed filenames.append(name) except AppError, msg: app.printErr(msg) return if filenames: args = string.join(filenames) try: app.runCvsCmd('remove', rootDir=toDeleteListing.getRootDir(), globOpts=globOpts, args=opts+' '+args) except CvsError: return toDeleteListing.printEntries(verbose=0) answ = app.prompt("\nThe files have been scheduled for removal.\n" \ "Should I run `commit' to remove the files " \ "permanently (yes|no)? ") if answ == 'yes': for e in toDeleteListing.entries: e.status = Entry.S_DELETED app.runCvsCmd('commit', rootDir=toDeleteListing.getRootDir(), fork=0, args=args) def printListing(app, name, args): app.printListing() def setCvsRoot(app, name, args): "Sets the cvsroot variable manually." roots = app.getCvsRootAliases() if len(args) != 0: newRoot = args.split()[0] else: if roots == None: app.printMsg("No aliases specified.") else: app.printMsg("Aliases:") for x in roots.items(): print " %s => %s" % (x[0], x[1]) newRoot = app.prompt('enter new cvsroot: ') if newRoot is None: return if roots.has_key(newRoot): newRoot = roots[newRoot] else: a = app.prompt("You can specify an alias for the cvsroot: ") if a: roots[a] = newRoot app.appendToSection('CVSROOT', ['%s = %s' % (a, newRoot)]) if newRoot is not None: app.setCvsRoot(newRoot) def readCvsRoot(app, name, args): "Reads the cvsroot variable from CVS/Root." newRoot = app.readCvsRootFromFile() if newRoot == None: app.printErr('Could not read CVSROOT from file.') else: app.setCvsRoot(newRoot) def clearCvsRoot(app, name, args): "Unsets the cvsroot variable." app.setCvsRoot(None) def toggleCvsRootAutoUpdate(app, name, args): """Toggles the auto-update feature for the CVSROOT var. If this option is set, the CVSROOT variable is automatically updated when a new directory is entered. Without a argument the option is toggled. You can set the option to a new value with `on' or `off' as argument.""" app.cvsRootAutoUpdate = app.toggle(app.cvsRootAutoUpdate, args) if app.cvsRootAutoUpdate: newRoot = app.readCvsRootFromFile() if newRoot is not None: app.setCvsRoot(newRoot) def toggleFullPrompt(app, name, args): app.showFullPrompt = app.toggle(app.showFullPrompt, args) |