#!/usr/bin/env python
#
# $Source$
# $Id: xxcvs.old 762 2004-02-14 18:23:26Z blais $
#
# Copyright (c) 2002, Martin Blais. All rights reserved.
#

"""Interface to CVS and xxdiff.

This script is also used as a test for the CVS python library.

Usage:
------------------------------
  xxcvs [<global options>] <subcmd> [<subcmd options>] <args> ...

"""


__version__ = "$Revision: 762 $"
__author__ = "Martin Blais <blais@furius.ca>"

#===============================================================================
# EXTERNAL DECLARATIONS
#===============================================================================

import os, shutil
import string

from os.path import isfile

import cvs

from pprint import PrettyPrinter
pprint = PrettyPrinter().pprint


#===============================================================================
# LOCAL DECLARATIONS
#===============================================================================

#===============================================================================
# CLASS Error
#===============================================================================

class Error:
    """Exception class for this module."""

    def __init__(self, msg):
        self.msg = msg
    def __repr__(self):
        return repr(self.msg)
    def __str__(self):
        return str(self.msg)

#===============================================================================
# CLASS CmdStatus
#===============================================================================

class CmdStatus:

    name = ['status']

    def addopts(self, parser):
        parser.add_option('-v', '--tags', action='store_true',
                          help="Include tags")
        parser.add_option('-R', '--recursive', action='store_true',
                          help="Recursive")

    def __init__(self):
        self.fmt = "%-50s %-12s %-16s %s"

    def execute(self, subargs):
        if len(subargs) > 1 or ( len(subargs) == 1 and os.path.isdir(subargs[0]) ):
            mstatii = cvs.MultipleStatii(subargs,
                                        self.opts.tags,
                                        self.opts.recursive)
            while 1:
                status = mstatii.next()
                if not status:
                    break
                print self.fmt % (status.filename, status.workingRev(), \
                                  status.stickyTag(), status.statusStr())

        elif len(subargs) == 1:
            status = cvs.Status(subargs[0], self.opts.tags)
            print self.fmt % (status.filename, status.workingRev(), \
                              status.stickyTag(), status.statusStr())


#===============================================================================
# CLASS CmdLog
#===============================================================================

class CmdLog:

    name = ['log']

    def execute(self, subargs):
        for f in subargs:
            log = cvs.Log(f)
            print f
            print "------------------------------"
            print log

#===============================================================================
# CLASS CmdRootPath
#===============================================================================

class CmdRootPath:

    name = ['rootpath']

    def execute(self, subargs):
        print cvs.getRootPath()

#===============================================================================
# MAIN
#===============================================================================

try:
    
    import optparse
    parser = optparse.OptionParser(__doc__.strip(), version=__version__)

    parser.add_option('-V', '--verbose', action='store_true',
                      help="Verbose mode (useful for debugging).")
    parser.add_option('--debug', action='store_true',
                      help="Full-on debug mode, including CVS commands.")
        
    # Declare subcommands
    subcmds = [ 
        CmdStatus(),
        CmdLog(),
        CmdRootPath()
        ]
    
    gopts, sc, opts, args = parse_subcommands(parser, subcmds)
    
    if gopts.debug:
        gopts.verbose = 1
        cvs.trace = 1

    sc.execute(args)

except Error, e:
    print 'Error:', e

except cvs.Error, e:
    print 'CVS Error:', e
    if gopts.debug:
        raise
