1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
#!/usr/bin/env python
#******************************************************************************\
#* $Id: xxcleartool 682 2003-12-25 21:32:57Z blais $
#* $Date: 2003-12-25 16:32:57 -0500 (Thu, 25 Dec 2003) $
#*
#* Copyright (C) 2001 Martin Blais <blais@furius.ca>
#*
#* This program 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.
#*
#* This program 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 this program; if not, write to the Free Software
#* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#*
#*****************************************************************************/
"""Wrapper to ClearCase and xxdiff.
This script forms glue around ClearCase and xxdiff. It allows one to go through
commonly performed file comparison tasks in relation to ClearCase use. See the
individual command descriptions to find out what it can do.
If you find a task that you perform often and it is not project- or
company-specific, send a suggestion to the author at blais@furius.ca.
"""
__version__ = "$Revision: 682 $"
__author__ = "Martin Blais <blais@furius.ca>"
__state__ = "$State$"
# $Source$
#===============================================================================
# EXTERNAL DECLARATIONS
#===============================================================================
import sys, getopt, os.path
import re, string
import time
import tempfile
import errno
import commands
#===============================================================================
# LOCAL DECLARATIONS
#===============================================================================
#-------------------------------------------------------------------------------
#
def runDiff(cargs):
xargs = string.split( gopts.xxdiffargs ) + cargs
run( gopts.xxdiff, xargs )
if not gopts.nowait:
os.wait()
#-------------------------------------------------------------------------------
#
def run(program, args, stdinn = None):
command = string.join( [ program ] + filter( lambda x: x, args ) )
if gopts.debug or gopts.fake:
print "running:", command
if gopts.fake:
return
try:
( child_stdin, child_stdout ) = os.popen2( command )
if stdinn:
child_stdin.write( stdinn )
#sopts = os.P_NOWAIT
#os.spawnv( sopts, program, [ program ] + args )
child_stdin.close()
child_stdout.close()
except os.error, e:
sys.stderr.write( "Error spawning program:" + e.strerror + "\n" )
#-------------------------------------------------------------------------------
#
def runCt(cmd):
if gopts.debug or gopts.fake:
print "running: cleartool", cmd
if gopts.fake:
return
out = commands.getoutput( gopts.cleartool + cmd )
return out
#-------------------------------------------------------------------------------
#
def checkEqualFiles(files):
"""Compares filename to make sure that no two of them are the same."""
# FIXME todo
pass
#===============================================================================
# CLASS CmFile
#===============================================================================
class CmFile:
"""Class that contains information about a specific file.
This class contains everything that a file is about, original pathname,
revision, etc.
"""
_mre = re.compile( '([^@]*)@@(.*)' )
def __init__(self, origname):
self.fullname = origname
( self.filename, self.rev ) = self.parseName( origname )
self.rev_cur = None
self.rev_pre = None
self.rev_anc = None
self.rev_latest = None
self.checkedout = None
def dump(self):
print
print "current", self.getCurrentRevision()
print "previous", self.getPreviousRevision()
print "ancestor", self.getAncestorRevision()
print "mainlatest", self.getMainLatestRevision()
print
print "fullname", self.fullname
print "rev", self.rev
print "rev_cur", self.rev_cur
print "rev_pre", self.rev_pre
print "rev_anc", self.rev_anc
print "rev_latest", self.rev_latest
print "checkedout", self.checkedout
print
def parseName(self, fname):
"""Parses extended names and returns a list of ( filename, revision )
pairs."""
mo = CmFile._mre.match( fname )
if mo:
return mo.groups()
return [fname, None]
def isCheckedOut(self):
self.getCurrentRevision()
return self.checkedout
def getCurrent(self):
rev_cur = self.getCurrentRevision()
if rev_cur:
return self.filename + '@@' + rev_cur
return self.filename
def getCurrentRevision(self):
"""Gets the current revision."""
if not self.rev_cur:
out = runCt( ' describe -s ' + self.filename )
mo = CmFile._mre.match( out )
if mo:
self.rev_cur = mo.group( 2 )
if re.compile( 'CHECKEDOUT' ).search( self.rev_cur ):
self.rev_cur = None
self.checkedout = 1
return self.rev_cur
def getPrevious(self):
return self.filename + '@@' + self.getPreviousRevision()
def getPreviousRevision(self):
"""Gets the previous revision."""
if not self.rev_pre:
preout = runCt( ' describe -s -pre ' + self.filename )
self.rev_pre = preout
return self.rev_pre
def getAncestorRevision(self):
"""Gets the ancestor revision."""
if not self.rev_anc:
self.rev_anc = self.getAncestorRevisionFile( self.fullname )
return self.rev_anc
def getAncestorRevisionFile(self, filename):
"""Gets the ancestor revision of a particular file."""
out = runCt( ' describe -s -anc ' + \
filename + ' ' + self.getMainLatestSimple() )
mo = CmFile._mre.match( out )
if mo:
return mo.group( 2 )
return None
def getAncestor(self):
return self.filename + '@@' + self.getAncestorRevision()
def getMainLatestSimple(self):
return self.filename + '@@/main/LATEST'
def getMainLatest(self):
return self.filename + '@@' + self.getMainLatestRevision()
def getMainLatestRevision(self):
"""Gets the revision for the latest in the main branch."""
if not self.rev_latest:
out = runCt( ' describe -s ' + self.getMainLatestSimple() )
mo = CmFile._mre.match( out )
if mo:
self.rev_latest = mo.group( 2 )
return self.rev_latest
#===============================================================================
# CLASS ExtCmdBase
#===============================================================================
class ExtCmdBase:
"""Base class for command classes.
FIXME do doc."""
def __init__(self):
self.child_stdin = None
def getFiles(self, names):
files = map( lambda x: CmFile(x), names )
return files
#===============================================================================
# CLASS CmdDiff
#===============================================================================
class CmdDiff(ExtCmdBase):
"""Diff files, graphically.
The diff subcommand is used simply to launch xxdiff on a set of files.
"""
name = 'diff'
def __init__(self):
ExtCmdBase.__init__( self )
def execute(self, args):
args = self.getFiles( args )
runDiff( map( lambda x: x.fullname, args ) )
#===============================================================================
# CLASS CmdPrevious
#===============================================================================
class CmdPrevious(ExtCmdBase):
"""Diffs each file with its previous revision
FIXME do doc.
"""
name = 'previous'
def __init__(self):
ExtCmdBase.__init__( self )
def execute(self, args):
args = self.getFiles( args )
for f in args:
current = f.getCurrent()
previous = f.getPrevious()
if gopts.verbose:
print "------------------------------"
print "current: ", current
print "previous:", previous
print "------------------------------"
cargs = [ current, previous ]
checkEqualFiles( cargs )
runDiff( cargs )
#===============================================================================
# CLASS CmdBranch
#===============================================================================
class CmdBranch(ExtCmdBase):
"""Diffs each file with the previous tagged revision
FIXME do doc."""
name = 'branch'
optmap = []
def __init__(self):
ExtCmdBase.__init__( self )
def execute(self, args):
args = self.getFiles( args )
for f in args:
current = f.getCurrent()
ancestor = f.getAncestor()
if gopts.verbose:
print "------------------------------"
print "current: ", current
print "ancestor:", ancestor
print "------------------------------"
cargs = [ current, ancestor ]
checkEqualFiles( cargs )
runDiff( cargs )
#===============================================================================
# CLASS CmdMerge
#===============================================================================
class CmdMerge(ExtCmdBase):
"""Diff with each ancestor
Diffs each file with its ancestor and the latest in the main
branch, if it is different.
This can be used to inspect the results of DL_fu_update at discreet.
The options are a bit confusing. Imagine you have:
main/3
/ \
main/4 main/yourbranch/6
and you run an update, getting
main/3
/ \
main/4 main/yourbranch/6
\
main/yourbranch/CHECKEDOUT
Using normal mode you'll get a diff of
main/yourbranch/CHECKEDOUT main/4
Using the 'remerge' option will spawn a diff with
main/yourbranch/6 main/3 main/4
Using the 'leave_merged' option will spawn a diff with
main/yourbranch/CHECKEDOUT main/3 main/4
Using the 'latest' option will spawn a diff with
main/yourbranch/CHECKEDOUT main/yourbranch/6 main/4
Use the one that suits you best.
"""
name = 'merge'
optmap = [
( 'remerge', 'r', "If file is still checked out, look for real ancestor and current file."),
( 'leave-merged', 'm', "If file is still checked out, leave current file as merged result." "If file is still checked out, leave current file as merged result." ),
( 'latest', 'l', "If file is still checked out, don't bother showing ancestor." ),
]
def __init__(self):
ExtCmdBase.__init__( self )
def execute(self, args):
args = self.getFiles( args )
s = 0
if self.opts.remerge: s += 1
if self.opts.leave_merged: s += 1
if self.opts.latest: s += 1
if s > 1:
sys.stderr.write( "Cannot specify more than one mode." )
sys.exit( 1 )
for f in args:
latest = f.getMainLatest()
cargs = []
if f.isCheckedOut():
if self.opts.remerge:
previous = f.getPrevious()
ancestor = f.fullname + '@@' + \
f.getAncestorRevisionFile( previous )
cargs += [ '--merge' ]
cargs += [ previous, ancestor, latest ]
if gopts.verbose:
print "------------------------------"
print "previous: ", previous
print "ancestor:", ancestor
print "latest: ", latest
print "------------------------------"
elif self.opts.leave_merged:
current = f.getCurrent()
previous = f.getPrevious()
ancestor = f.fullname + '@@' + \
f.getAncestorRevisionFile( previous )
cargs += [ current, ancestor, latest ]
if gopts.verbose:
print "------------------------------"
print "current: ", current
print "ancestor:", ancestor
print "latest: ", latest
print "------------------------------"
elif self.opts.latest:
current = f.getCurrent()
previous = f.getPrevious()
cargs += [ current, previous, latest ]
if gopts.verbose:
print "------------------------------"
print "current: ", current
print "previous:", previous
print "latest: ", latest
print "------------------------------"
else:
current = f.getCurrent()
ancestor = f.getAncestor()
cargs += [ current, ancestor ]
if gopts.verbose:
print "------------------------------"
print "current: ", current
print "ancestor:", ancestor
print "latest: ", latest
print "------------------------------"
else:
current = f.getCurrent()
ancestor = f.getAncestor()
cargs += [ current, ancestor ]
if gopts.verbose:
print "------------------------------"
print "current: ", current
print "ancestor:", ancestor
print "latest: ", latest
print "------------------------------"
checkEqualFiles( cargs )
runDiff( cargs )
#===============================================================================
# CLASS CmdUpdate
#===============================================================================
class CmdUpdate(ExtCmdBase):
"""Updates, with graphical merge where needed.
updates the specified files (or all hierarchy if none specified), and invoke
graphical diff/merge tool to resolve conflicts if any are present.
"""
name = 'update'
optmap = []
def __init__(self):
ExtCmdBase.__init__( self )
def execute(self, args):
args = self.getFiles( args )
pass
#-------------------------------------------------------------------------------
#
# Standard options parsing routine with subcommands support.
# Takes care of handling --help and --version, as well as --help for subcmd.
#
# Subcmd objects must have 'name', 'optmap' (optional) and a doc string
# (optional) which is in the GvR format..
#
# Returns (opts, subcmd, args)
import distutils.fancy_getopt as fgetopt # getopt()
from distutils.errors import DistutilsArgError
def parse_options_subcmds(optmap, subcmds=[]):
optmap = [
( 'help', 'h', "Prints help message." ),
( 'version', 'V', "Prints version." ),
] + optmap
def opt_translate(opt):
o = opt[0]
if o[-1] == '=': # option takes an argument?
o = o[:-1]
return fgetopt.translate_longopt( o )
# parse global options
opts = fgetopt.OptionDummy( map( opt_translate, optmap ) )
parser = fgetopt.FancyGetopt( optmap )
try:
args = parser.getopt( args=sys.argv[1:], object=opts )
except DistutilsArgError, e:
print >> sys.stderr, "Argument error:", e
sys.exit(1)
# print version information
if opts.version:
print os.path.basename(sys.argv[0]), "--", __version__[1:-1]
print "Written by", __author__
sys.exit(1)
# print global help
if opts.help:
for i in parser.generate_help( __doc__ ):
print i
print
print "Available subcommands"
print "------------------------------"
for c in subcmds:
first_line = c.__doc__.split('\n')[0]
print " %s:" % c.name, first_line
print
sys.exit(1)
# find current subcommand
if len(args) == 0:
print >> sys.stderr, "Error: no subcommand"
sys.exit(1)
try:
sc = None
for s in subcmds:
if s.name.startswith( args[0] ):
if not sc:
sc = s
else:
raise ValueError
if not sc:
raise ValueError
except ValueError:
print >> sys.stderr, "Error: invalid or ambiguous subcommand", args[0]
sys.exit(1)
# parse command options
suboptmap = [( 'help', 'h', "Prints this subcommand's help." )]
if getattr(sc, 'optmap', None):
suboptmap = sc.optmap + suboptmap
sc.opts = fgetopt.OptionDummy( map( opt_translate, suboptmap ) )
subparser = fgetopt.FancyGetopt( suboptmap )
try:
subargs = subparser.getopt( args[1:], object=sc.opts )
except DistutilsArgError, e:
print >> sys.stderr, "Argument error:", e
sys.exit(1)
# print command help
if opts.help:
print __doc__
print "Help for '" + sc.name + "' subcommand:"
print "------------------------------"
for i in subparser.generate_help( sc.__doc__ ):
print i
print
sys.exit(1)
return (opts, sc, subargs)
#===============================================================================
# MAIN
#===============================================================================
def main():
#
# options parsing
#
optmap = [
( 'verbose', 'v', "Verbose mode. Prints lots of stuff on stdout." ),
( 'debug', 'd', "Self debugging utility (do not use)." ),
( 'fake', 'n', "Print the commands that would be executed, but do not execute them." ),
( 'nowait', 'w', "Don't wait after spawning child processes." ),
( 'cleartool=', 'c', "Specify cleartool program location." ),
( 'xxdiff=', 'g', "Specify xxdiff program location." ),
( 'xxdiffargs=', 'A', "Arguments to pass directly to diff program." ),
]
# declare subcommands.
subcmds = [ CmdDiff(), \
CmdPrevious(), \
CmdBranch(), \
CmdMerge() ]
global gopts
(gopts, subcmd, subargs) = parse_options_subcmds( optmap, subcmds )
if not gopts.cleartool:
gopts.cleartool = 'cleartool'
if not gopts.xxdiff:
gopts.xxdiff = 'xxdiff'
if not gopts.xxdiffargs:
gopts.xxdiffargs = ''
# execute command
subcmd.execute( subargs )
# Run main if loaded as a script
if __name__ == "__main__":
main()
|