git-artemis
author Sean E. Russell <sean.russell@internationalsos.com>
Wed, 15 Mar 2017 13:12:28 -0400 (2017-03-15)
changeset 90 979145ac3ccc
parent 73 b78234a1bf04
permissions -rwxr-xr-x
Ooops. Everything but ilist was broken.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     1
#!/usr/bin/env python
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     2
# coding: utf-8
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     3
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     4
# use artemis with git
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     5
#  John Kozak <jgak@thameslighter.net> 2016
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     6
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     7
import artemis
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     8
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
     9
import os
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    10
import sys
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    11
import tempfile
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    12
import subprocess
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    13
from argparse import ArgumentParser
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    14
from collections import namedtuple
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    15
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    16
# munge arguments and pass on to artemis
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    17
def iadd(args,repo,ui):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    18
    id = args.id
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    19
    d  = dict(args.__dict__)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    20
    del d['id']
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    21
    artemis.iadd(ui,repo,id,**d)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    22
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    23
def ilist(args,repo,ui):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    24
    artemis.ilist(ui,repo,**args.__dict__)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    25
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    26
def ishow(args,repo,ui):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    27
    id = args.id
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    28
    d  = dict(args.__dict__)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    29
    del d['id']
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    30
    artemis.ishow(ui,repo,id,**d)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    31
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    32
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    33
class Repo(object):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    34
    """Implement a subset of hgext's Repo object in git."""
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    35
    def __init__(self):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    36
        git_sp    = subprocess.Popen(['git','rev-parse','--show-toplevel'],stdout=subprocess.PIPE)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    37
        self.root = git_sp.communicate()[0].rstrip()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    38
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    39
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    40
class UI(object):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    41
    """Implement a subset of hgext's UI object in git."""
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    42
    def __init__(self,config):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    43
        self._config  = config
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    44
        self.verbose  = True
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    45
    def config(self,group,name,**opts):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    46
        if group=='artemis':
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    47
            return self._config.get(name,opts['default'])
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    48
        else:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    49
            raise NotFound(group,name)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    50
    def configitems(self,_):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    51
        return self._config.items()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    52
    def write(self,s):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    53
        print s,
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    54
    def warn(self,s):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    55
        print s,
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    56
    def status(self,s):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    57
        print s,
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    58
    def username(self):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    59
        sp = subprocess.Popen(['git','config','user.name'],stdout=subprocess.PIPE)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    60
        return sp.communicate()[0].rstrip()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    61
    def edit(self,text,user):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    62
        fd,fn = tempfile.mkstemp(suffix='.txt')
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    63
        try:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    64
            os.write(fd,text)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    65
            os.close(fd)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    66
            rc = subprocess.call("%s '%s'"%(os.environ['EDITOR'],fn),shell=True)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    67
            if rc!=0:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    68
                raise Exception('edit failed')
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    69
            else:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    70
                return file(fn).read()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    71
        finally:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    72
            os.unlink(fn)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    73
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    74
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    75
# Monkeypatch the hg commands object to implement git equivalent functionality.
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    76
# It would be nice to disable the commands we don't re-implement.
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    77
def git_add(ui,repo,path):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    78
    rc = subprocess.call("git add '%s'"%(path,),shell=True)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    79
    if rc!=0:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    80
        raise Exception("git add failed")
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    81
def git_commit(ui,repo,path):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    82
    rc = subprocess.call("git commit -m 'commit from artemis' -- '%s'"%(path,),shell=True)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    83
    if rc!=0:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    84
        raise Exception("git commit failed")
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    85
#artemis.commands.clear()  # how to do this?
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    86
artemis.commands.add    = git_add
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    87
artemis.commands.commit = git_commit
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    88
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    89
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    90
def _build_argparse_from_cmdtable():
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    91
    """Build an ArgumentParser equivalent to artemis' cmdtable.
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    92
       This is a bit hacky."""
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    93
    parser     = ArgumentParser()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    94
    subparsers = parser.add_subparsers(help="simple issue tracker")
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    95
    for k,v in artemis.cmdtable.items():
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    96
        assert k[0]=='i'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    97
        n  = k[1:]
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    98
        sp = subparsers.add_parser(n)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
    99
        for f in v[1]:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   100
            args   = ([] if f[0]=='' else ['-'+f[0]])+['--'+f[1]]
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   101
            kwargs = {'help':f[3]}
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   102
            if f[2] in [False,None]:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   103
                kwargs['action']  = 'store_true'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   104
            elif f[2]==[]:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   105
                kwargs['action']  = 'append'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   106
                kwargs['default'] = []
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   107
            else:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   108
                kwargs['action']  = 'store'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   109
            if isinstance(f[2],basestring):
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   110
                kwargs['default'] = ''
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   111
            sp.add_argument(*args,**kwargs)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   112
        sc = v[2].split(' ')
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   113
        assert sc[0]=='hg'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   114
        assert sc[1]==k
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   115
        assert sc[2]=='[OPTIONS]'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   116
        for a in sc[3:]:
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   117
            kwargs = {'action':'store'}
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   118
            n      = a
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   119
            if a[0]=='[':
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   120
                assert a[-1]==']'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   121
                n               = a[1:-1]
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   122
                kwargs['nargs'] = '?'
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   123
                if n=='COMMENT':
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   124
                    kwargs['default'] = 0
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   125
            sp.add_argument(n.lower(),**kwargs)
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   126
        sp.set_defaults(func=globals()[k])
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   127
    return parser
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   128
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   129
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   130
if __name__=='__main__':
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   131
    repo   = Repo()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   132
    ui     = UI({'issues':artemis.default_issues_dir})
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   133
    parser = _build_argparse_from_cmdtable()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   134
    args   = parser.parse_args()
b78234a1bf04 Support for git.
John Kozak <jk@thameslighter.net>
parents:
diff changeset
   135
    args.func(args,repo,ui)