review/api.py @ cc9456bb289b

Add more API documentation.
author Steve Losh <steve@stevelosh.com>
date Thu, 08 Oct 2009 17:58:57 -0400
parents 8cea29aa955f
children 022175cc8a90
from __future__ import with_statement

"""The data structures used by hg-review."""

import os
import messages, templates
from datetime import datetime
from mercurial import cmdutil, hg, patch
from mercurial.node import hex
from mercurial.util import sha1


class PreexistingDatastore(Exception):
    """Raised when trying to initialize a datastore when one seems to exist."""
    def __init__(self, committed):
        super(PreexistingDatastore, self).__init__()
        self.committed = committed
    

class SignoffExists(Exception):
    """Raised when trying to signoff twice without forcing."""
    pass

class CannotDeleteObject(Exception):
    """Raised when trying to delete an object that does not support deletion."""
    pass

class FileNotInChangeset(Exception):
    """Raised when trying to add a comment on a file not in the changeset."""
    def __init__(self, filename):
        super(FileNotInChangeset, self).__init__()
        self.filename = filename
    


def _split_path_dammit(p):
    """Take a file path (from the current platform) and split it.  Really.
    
    os.path doesn't seem to have an easy way to say "Split this path into a
    list of pieces."
    
    >>> _split_path_dammit('')
    []
    >>> _split_path_dammit('one')
    ['one']
    >>> _split_path_dammit('one/two/three')
    ['one', 'two', 'three']
    >>> _split_path_dammit('one/two/three/')
    ['one', 'two', 'three']
    >>> _split_path_dammit('one/two/three.py')
    ['one', 'two', 'three.py']
    
    """
    def _spd(p):
        p, i = os.path.split(p)
        while i or p:
            yield i
            p, i = os.path.split(p)
    
    return filter(None, list(_spd(p)))[::-1]

def _parse_hgrf(repo):
    """Parse the .hgreview file and return the data inside.
    
    The .hgreview file will be pulled from the tip revision of the given
    repository.  If it is not committed it will not be found!
    
    """
    data = {}
    hgrd = repo['tip']['.hgreview'].data().split('\n')
    lines = [line for line in hgrd if line.strip()]
    for line in lines:
        label, _, path = [i.strip() for i in line.partition('=')]
        if label == 'local':
            data['lpath'] = path
        elif label == 'remote':
            data['rpath'] = path
    
    return data

def _commitfunc(ui, repo, message, match, opts):
    """A function used by the guts of Mercurial.
    
    Mercurial needs a "commit function" parameter when using cmdutil.commit.
    This is a simple function for *only* that purpose.
    
    """
    return repo.commit(message, opts.get('user'), opts.get('date'), match)

def _parse_data(data):
    """Parse the data (string) of a stored _ReviewObject and return a dict."""
    meta, _, message = data.partition('\n\n')
    
    data = {}
    for m in meta.split('\n'):
        label, _, val = m.partition(':')
        data[label] = val
    data['message'] = message
    return data


def sanitize_path(p, repo=None):
    """Sanitize a (platform-specific) path.
    
    If no repository is given, the path's separators will be replaced with
    forward slashes (the form Mercurial uses internally).
    
    If a repository is given, the result will be relative to the root of the
    repository.  This is useful for turning relative paths into normalized
    paths that can be used to look up files from a changectx.
    
    This function is idempotent.  If you sanitize a path multiple times
    against the same repository the result will not change.
    
    """
    if repo:
        p = os.path.relpath(os.path.realpath(p), start=repo.root)
    return '/'.join(_split_path_dammit(p))


class ReviewDatastore(object):
    """The code review data for a particular repository."""
    def __init__(self, ui, repo, lpath=None, rpath=None, create=False):
        self.ui = ui
        
        if not create:
            data = _parse_hgrf(repo)
            self.lpath = data['lpath']
            self.rpath = data['rpath']
        else:
            if '.hgreview' in repo['tip']:
                raise PreexistingDatastore(True)
            if os.path.exists(os.path.join(repo.root, '.hgreview')):
                raise PreexistingDatastore(False)
            self.lpath = lpath or '.review'
            self.rpath = rpath or ('../%s-review' % os.path.basename(repo.root))
        
        root = os.path.join(repo.root, self.lpath)
        self.target = repo
        self.repo = hg.repository(ui, root, create)
        
        if create:
            hgrpath = os.path.join(repo.root, '.hgreview')
            with open(hgrpath, 'w') as hgrf:
                hgrf.write('local = %s\n' % self.lpath)
                hgrf.write('remote = %s\n' % self.rpath)
            repo.add(['.hgreview'])
    
    def __getitem__(self, rev):
        """Return a ReviewChangeset for the given revision."""
        node = hex(self.target[rev].node())
        return ReviewChangeset(self.ui, self.repo, self.target, node)
    

class ReviewChangeset(object):
    """The review data about one changeset in the target repository."""
    def __init__(self, ui, repo, target, node):
        """A changeset being reviewed.
        
        You shouldn't need to create these directly -- use a ReviewDatastore
        object to get them:
        
            review_data = ReviewDatastore(ui, repo)
            tip_review_data = review_data['tip']
        
        """
        self.repo = repo
        self.target = target
        self.ui = ui
        self.node = node
        
        if '%s/.exists' % self.node in self.repo['tip']:
            _match = lambda p: lambda fn: fn.startswith(p)
            
            relevant = filter(_match(node), self.repo['tip'])
            commentfns = filter(_match('%s/comments' % node), relevant)
            signofffns = filter(_match('%s/signoffs' % node), relevant)
            
            self.comments = []
            for fn in commentfns:
                data = _parse_data(self.repo['tip'][fn].data())
                data['lines'] = data['lines'].split(',')
                data['lines'] = map(int, filter(None, data['lines']))
                self.comments.append(ReviewComment(**data))
            
            self.signoffs = []
            for fn in signofffns:
                data = _parse_data(self.repo['tip'][fn].data())
                self.signoffs.append(ReviewSignoff(**data))
        else:
            self.comments = []
            self.signoffs = []
            
            path = os.path.join(self.repo.root, self.node)
            os.mkdir(path)
            with open(os.path.join(path, '.exists'), 'w') as e:
                pass
            
            cmdutil.commit(ui, self.repo, _commitfunc,
                [os.path.join(path, '.exists')],
                { 'message': 'Initialize review data for changeset %s' % self.node,
                  'addremove': True, })
    
    def add_signoff(self, message, opinion='', force=False):
        """Add (and commit) a signoff for the given revision.
        
        The opinion argument should be 'yes', 'no', or ''.
        
        If a signoff from the user already exists, a SignoffExists exception
        will be raised unless the force argument is used.
        
        """
        existing = filter(lambda s: s.author == self.ui.username(), self.signoffs)
        
        if existing:
            if not force:
                raise SignoffExists
            existing[0].delete(self.ui, self.repo)
        
        signoff = ReviewSignoff(self.ui.username(), datetime.utcnow(),
            self.node, opinion, message)
        signoff.commit(self.ui, self.repo)
    
    def add_comment(self, message, filename='', lines=[]):
        """Add (and commit) a comment for the given file and lines.
        
        The filename should be normalized to the format Mercurial expects,
        that is: relative to the root of the repository and using forward
        slashes as the separator.  Paths can be converted with the
        sanitize_path function in this module.
        
        If the comment is on one or more lines, a filename *must* be given.
        
        Line numbers should be passed as a list, even if there is only one.
        See the full_diffs function for how to refer to line numbers.
        
        """
        if filename and filename not in self.target[self.node].files():
            raise FileNotInChangeset(filename)
        
        comment = ReviewComment(self.ui.username(), datetime.utcnow(),
            self.node, filename, lines, message)
        comment.commit(self.ui, self.repo)
    
    def full_diffs(self, filenames=None, opts={}):
        """Return full diffs of the given files (or all files).
        
        If the filenames argument is not used, diffs for every file in the
        changeset will be returned.
        
        The diffs are returned as a dictionary in the form:
        
            { 'filename': 'string of the diff' }
        
        All headers are stripped, so the an entire diff looks like this:
        
             unchanged line
             unchanged line
            -removed line
            -removed line
            +added line
             unchanged line
            -removed line
            -removed line
             unchanged line
             unchanged line
        
        When adding a comment, the line number given should be the line
        number from this diff (starting at 0).  To comment on the first two
        removed lines in the above example you would pass [2, 3].
        
        """
        
        target_files = self.target[self.node].files()
        if not filenames:
            filenames = target_files
        else:
            filenames = filter(lambda f: f in target_files, filenames)
        
        opts['unified'] = '100000'
        node2 = self.node
        node1 = self.target[node2].parents()[0].node()
        
        diffs = {}
        for filename in filenames:
            m = cmdutil.matchfiles(self.target, [filename])
            d = patch.diff(self.target, node1, node2, match=m,
                opts=patch.diffopts(self.ui, opts))
            
            # patch.diff will give us back a generator with two items
            # the first is the diff --git header, which we don't care about
            d.next()
            
            # the second is the diff's contents, which is what we want,
            # minus the header
            diffs[filename] = '\n'.join(d.next().splitlines()[3:])
        
        return diffs
    

class _ReviewObject(object):
    """A base object for some kind of review data (a signoff or comment)."""
    def __init__(self, container, commit_message, delete_message=None):
        self.container = container
        self.commit_message = commit_message
        self.delete_message = delete_message
    
    def commit(self, ui, repo):
        """Write and commit this object to the given repo."""
        
        path = os.path.join(repo.root, self.node, self.container)
        if not os.path.exists(path):
            os.mkdir(path)
        
        data = self.render_data()
        filename = sha1(data).hexdigest()
        objectpath = os.path.join(path, filename)
        
        with open(objectpath, 'w') as objectfile:
            objectfile.write(data)
        
        cmdutil.commit(ui, repo, _commitfunc, [objectpath],
            { 'message': self.commit_message % self.node, 'addremove': True, })
    
    def delete(self, ui, repo):
        """Delete and commit this object in the given repo."""
        
        if not self.delete_message:
            raise CannotDeleteObject
        
        data = self.render_data()
        filename = sha1(data).hexdigest()
        objectpath = os.path.join(repo.root, self.node, self.container, filename)
        
        os.remove(objectpath)
        
        cmdutil.commit(ui, repo, _commitfunc, [objectpath],
            { 'message': self.delete_message % self.node, 'addremove': True, })
    

class ReviewComment(_ReviewObject):
    """A single review comment."""
    def __init__(self, author, datetime, node, filename, lines, message, **extra):
        super(ReviewComment, self).__init__(
            container='comments', commit_message=messages.COMMIT_COMMENT,
        )
        self.author = author
        self.datetime = datetime
        self.node = node
        self.filename = filename
        self.lines = lines
        self.message = message
    
    def render_data(self):
        datetime = str(self.datetime)
        lines = ','.join(self.lines)
        return templates.COMMENT_FILE_TEMPLATE % ( self.author, datetime,
            self.node, self.filename, lines, self.message )
    
    def __str__(self):
        return '\n'.join(map(str, [
            self.author,
            self.datetime,
            self.node,
            self.filename,
            self.lines,
            self.message,
            '\n',
        ]))
    

class ReviewSignoff(_ReviewObject):
    """A single review signoff."""
    def __init__(self, author, datetime, node, opinion, message, **extra):
        super(ReviewSignoff, self).__init__(
            container='signoffs', commit_message=messages.COMMIT_SIGNOFF,
            delete_message=messages.DELETE_SIGNOFF,
        )
        self.author = author
        self.datetime = datetime
        self.node = node
        self.opinion = opinion
        self.message = message
    
    def render_data(self):
        datetime = str(self.datetime)
        return templates.SIGNOFF_FILE_TEMPLATE % ( self.author, datetime,
            self.node, self.opinion, self.message )