#!/usr/bin/env python # # Copyright (c) 2006-2007, Hans Meine # All rights reserved. # # This is licensed according to the new BSD license. # Please send patches / comments, I would be happy about any feedback. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # * Neither the name of the University of Hamburg nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import os, sys # USAGE: cvs-chroot [old] [new] from optparse import OptionParser op = OptionParser( usage = "%prog [options] [ ]", description = "Recursively looks for CVS/Root files, and changes occurences of to (without arguments, just displays current roots).\nUse empty strings for or to remove/create CVS/Root files.") op.add_option("-v", "--verbose", action = "count", default = 1, dest = "verbosity", help = "verbose output (default)") op.add_option("-q", "--quiet", action = "store_const", const = 0, dest = "verbosity", help = "quite mode (only errors)") op.add_option("-c", "--columns", action = "store_true", default = False, dest = "outputColumns", help = "format output in columns") op.add_option("-s", "--skiplinks", action = "store_true", default = False, dest = "skipLinks", help = "skip symbolic links to directories (default: follow)") options, args = op.parse_args() changeMode = (len(args) == 2) if changeMode: sourceRoot = args[0] if not sourceRoot or sourceRoot == "": sourceRoot = None targetRoot = args[1] if not targetRoot: targetRoot = None LOG_ERROR = 0 LOG_NORMAL = 1 LOG_VERBOSE = 2 def log(level, str): if level: if level <= options.verbosity: sys.stdout.write(str) else: sys.stderr.write(str) def recursiveCVSDirs(startDir): result = [] for name in os.listdir(startDir): subPath = os.path.join(startDir, name) if os.path.isdir(subPath) and ( not options.skipLinks or not os.path.islink(subPath)): if name == "CVS": result.append(startDir[2:]) else: result.extend(recursiveCVSDirs(subPath)) return result # ATTENTION: first 2 chars from paths are stripped, expected to be "./" cvsPaths = recursiveCVSDirs(".") formatStr = "%s: %s\n" if options.outputColumns: maxLen = 0 for cp in cvsPaths: maxLen = max(maxLen, len(cp)) formatStr = "%-"+str(maxLen)+"s | %s\n" for cvsPath in cvsPaths: if not cvsPath: cvsPath = "." # nicer display rootPath = os.path.join(cvsPath, "CVS/Root") if os.path.isfile(rootPath): root = file(rootPath).readline()[:-1] else: root = None if not changeMode: if root: log(LOG_NORMAL, formatStr % (cvsPath, root)) else: log(LOG_VERBOSE, formatStr % (cvsPath, "")) else: if root == sourceRoot: if targetRoot: file(rootPath, "w").write(targetRoot + "\n") log(LOG_NORMAL, formatStr % (cvsPath, "changed")) else: os.unlink(rootPath) log(LOG_NORMAL, formatStr % (cvsPath, "removed")) else: log(LOG_VERBOSE, formatStr % (cvsPath, "IGNORED"))