view hq/main.py @ 1:67e06ca7c56e

documentation notes
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 May 2010 08:36:21 -0700
parents b5671297a0db
children dedf4c4c2ba2
line wrap: on
line source

#!/usr/bin/env python
"""
mercurial queue extension front-end policy manager
"""

import os
import subprocess
import sys

from command import CommandParser

def call(command, *args, **kw):
    code = subprocess.call(command, *args, **kw)
    if code:
        if isinstance(command, basestring):
            cmdstr = command
        else:
            cmdstr = ' '.join(command)
        raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))

class HQ(object):
    """
    mercurial queue extension front-end policy manager
    """

    def __init__(self, parser, options):
        """initialize global options"""
        # TODO: look at hgrc file
        # for [defaults] repository_host

    def clone(self, repo, patch=None):
        """clone the repository and begin a patch queue"""
        directory = repo.rsplit('/', 1)
        call(['hg', 'clone', repo, directory])
        os.chdir(directory)
        call(['hg', 'qinit', '-c'])
        if patch:
            call(['hg', 'qnew', patch])

    def edit(self, patch=None):
        """
        edit a patch
        - patch: the patch to edit
        """

    def commit(self, message):
        """
        commit a patch and push it to the master repository
        """
        call(['hg', 'qrefresh'])
        call(['hg', 'qcommit', '-m', message])
        root = subprocess.Popen(['hg', 'root'], stdout=subprocess.PIPE).communicate()[0]
        os.chdir(os.path.join(root, '.hg', 'patches'))
        call(['hg', 'push'])

    def pull(self, repo=None):
        """
        pull from the root repository
        """
        # TODO: commit prior to realignment
        call(['hg', 'qpop', '--all'])
        call(['hg', 'pull'] + repo and [repo] or [])
        call(['hg', 'qpush', '--all'])
        

def main(args=sys.argv[1:]):
    parser = CommandParser(HQ)
    options, args = parser.parse_args(args)
    parser.invoke(args)

if __name__ == '__main__':
    main()