import os, commands, re, sets, tempfile
from subprocess import Popen, PIPE, STDOUT

import buffer, default, dirutil, lex, regex, util, window
from point import Point

from method import DATATYPES, Method, Argument, arg

class CvsCommit(Method):
    '''diff the current file with the version in CVS'''
    args = [arg("msg", t=type(""), p="Commit Message: ",
                h="commit message to send to CVS")]
    regex = re.compile('^new revision: ([0-9.]+); previous revision: ([0-9.]+)$')
    def _execute(self, w, **vargs):
        if not hasattr(w.buffer, 'path'):
            w.set_error("Buffer has no corresponding file")
            return

        cwd = os.getcwd() + os.path.sep
        path = w.buffer.path
        if path.startswith(cwd):
            path = path[len(cwd):]

        cmd = "cvs ci -m %r %r" % (vargs['msg'], path)
        (status, data) = commands.getstatusoutput(cmd)
        status = status >> 8
        lines = data.split('\n')

        if status == 0:
            for line in lines:
                m = self.regex.match(line)
                if m:
                    w.set_error("Committed [%s -> %s]" % (m.group(2), m.group(1)))
                    return
            w.set_error("Up-to-date")
        else:
            w.set_error("Problems with CVS commit: %d" % status)
            w.application.data_buffer("*Commit*", data, switch_to=True)
    
class CvsStatus(Method):
    regex1 = re.compile('^File: (.+?) *\tStatus: (.*?)$')
    regex2 = re.compile('^   Working revision:\t([0-9\.]+)$')
    regex3 = re.compile('^   Repository revision:\t([0-9\.]+)\t(.*)$')
    regex4 = re.compile('^   Sticky Tag:\t\t\((.*)\)$')
    regex5 = re.compile('^   Sticky Date:\t\t\((.*)\)$')
    regex6 = re.compile('^   Sticky Options:\t\((.*)\)$')
    def _execute(self, w, **vargs):
        if not hasattr(w.buffer, 'path'):
            w.set_error("Buffer has no corresponding file")
            return

        cwd = os.getcwd() + os.path.sep
        path = w.buffer.path
        if path.startswith(cwd):
            path = path[len(cwd):]

        cmd = "cvs status %r" % path
        (status, data) = commands.getstatusoutput(cmd)
        status = status >> 8

        if status != 0:
            w.set_error("Problems with CVS status: %d" % status)
            return

        lines = data.split('\n')

        if lines[0].startswith('cvs status: nothing known about '):
            w.set_error('File is not under CVS control')
            return

        m = self.regex1.match(lines[1])
        assert m, "regex1 %r" % lines[1]
        ffile = m.group(1)
        fstatus = m.group(2)

        m = self.regex2.match(lines[3])
        assert m, "regex2 %r" % lines[3]
        wrev = m.group(1)

        m = self.regex3.match(lines[4])
        assert m, "regex3 %r" % lines[4]
        rrev = m.group(1)
        rpath = m.group(2)

        m = self.regex4.match(lines[5])
        assert m, "regex4 %r" % lines[5]
        stag = m.group(1)

        m = self.regex5.match(lines[6])
        assert m, "regex5 %r" % lines[6]
        sdate = m.group(1)

        m = self.regex6.match(lines[7])
        assert m, "regex6 %r" % lines[7]
        soptions = m.group(1)

        w.set_error('%s  %s  %s/%s  [%s|%s|%s]' % (ffile, fstatus,
                                                               wrev, rrev, stag,
                                                               sdate, soptions))

class CvsDiff(Method):
    '''diff the current file with the version in CVS'''
    def _execute(self, w, **vargs):
        if not hasattr(w.buffer, 'path'):
            w.set_error("Buffer has no corresponding file")
            return

        cwd = os.getcwd() + os.path.sep
        path = w.buffer.path
        if path.startswith(cwd):
            path = path[len(cwd):]

        cmd = "cvs diff -u %r" % path
        (status, data) = commands.getstatusoutput(cmd)
        status = status >> 8

        if status == 0:
            w.set_error("No difference found")
        else:
            w.application.data_buffer("*Diff*", data, switch_to=True, modename='diff')
            w.set_error("Differences were found")
class CvsDiff2(Method):
    '''diff the current file's contents with a version in CVS'''
    rev_regex = re.compile('^[0-9]+\.[0-9]+$')
    args = [arg("revision", t=type(""), p="Old Revision: ",
                h="revision number")]
    def _execute(self, w, **vargs):
        if not hasattr(w.buffer, 'path'):
            w.set_error("Buffer has no corresponding file")
            return

        rev = vargs['revision']
        if not self.rev_regex.match(rev):
            w.set_error("Could not parse revision: %r" % rev)
            return

        cwd = os.getcwd() + os.path.sep
        path = w.buffer.path
        if path.startswith(cwd):
            path = path[len(cwd):]

        cmd = "cvs diff -r %s -u %r" % (rev, path)
        (status, data) = commands.getstatusoutput(cmd)
        status = status >> 8

        if status == 0:
            w.set_error("No difference found")
        else:
            w.application.data_buffer("*Diff*", data, switch_to=True, modename='diff')
            w.set_error("Differences were found")
class CvsDiff3(Method):
    '''diff the current file with the version in CVS'''
    rev_regex = re.compile('^[0-9]+\.[0-9]+$')
    args = [arg("revision1", t=type(""), p="Old Revision: ", h='old revision number'),
            arg("revision2", t=type(""), p="New Revision: ", h='new revision number')]
    def _execute(self, w, **vargs):
        if not hasattr(w.buffer, 'path'):
            w.set_error("Buffer has no corresponding file")
            return

        rev1 = vargs['revision1']
        if not self.rev_regex.match(rev1):
            w.set_error("Could not parse revision1: %r" % rev)
            return

        rev2 = vargs['revision2']
        if not self.rev_regex.match(rev2):
            w.set_error("Could not parse revision2: %r" % rev)
            return

        cwd = os.getcwd() + os.path.sep
        path = w.buffer.path
        if path.startswith(cwd):
            path = path[len(cwd):]

        cmd = "cvs diff -r %s -r %s -u %r" % (rev1, rev2, path)
        (status, data) = commands.getstatusoutput(cmd)
        status = status >> 8

        if status == 0:
            w.set_error("No difference found")
        else:
            w.application.data_buffer("*Diff*", data, switch_to=True, modename='diff')
            w.set_error("Differences were found")

class CvsBlame(Method):
    '''show blame output for the current version in SVN'''
    line_re = re.compile('^([0-9.]+) +\(*([a-zA-Z0-9_]+) +([-0-9A-Za-z]+)\): (.*)$')
    def _execute(self, w, **vargs):
        if not hasattr(w.buffer, 'path'):
            w.set_error("Buffer has no corresponding file")
            return

        cwd = os.getcwd() + os.path.sep
        path = w.buffer.path
        if path.startswith(cwd):
            path = path[len(cwd):]

        cmd = ("/usr/bin/cvs", 'annotate', path)
        pipe = Popen(cmd, stdout=PIPE, stderr=PIPE)

        linetokens = []
        max_rev    = 0
        max_user   = 0
        for line in pipe.stdout:
            m = self.line_re.match(line)
            if not m:
                raise Exception, line
            (rev, user, date, content) = m.groups()
            max_rev = max(max_rev, len(rev))
            max_user = max(max_user, len(user))
            linetokens.append([rev, user, date, content, []])
        status = pipe.wait() >> 8

        lines = [x[3] for x in linetokens]
        if w.mode.grammar:
            lexer     = lex.Lexer(w.mode, w.mode.grammar)
            lextokens = [[] for l in lines]
            for t in lexer.lex(lines):
                linetokens[t.y][4].append(t)

        lines = []
        for linetoken in linetokens:
            (rev, user, date, content, lextokens) = linetoken
            prefix = '[b:d:*]%-8s [c:d:*]%-10s [b:d:*]%10s[d:d:*]' % (rev, user, date)
            if lextokens:
                suffixes = []
                for lt in lextokens:
                    s = lt.string.replace('\\', '\\\\')
                    s = s.replace('[', '\\[').replace(']', '\\]')
                    suffixes.append('[%s:%s:*]%s' % (lt.color[0], lt.color[1], s))
                suffix = ''.join(suffixes)
            else:
                suffix = content + '\n'
            lines.append('%s %s' % (prefix, suffix))
        data = ''.join(lines)

        status = pipe.wait() >> 8
        if status == 0:
            w.application.color_data_buffer("*Blame*", data, switch_to=True)
        else:
            w.set_error("There was an error (%s)" % (status))