hgproc.py
author TK Soh <teekaysoh@yahoo.com>
Mon, 24 Dec 2007 14:16:04 +0000
changeset 422 ccdb70ac0a3c
parent 417 977ce2ff9fd4
child 470 eb1424f64d96
permissions -rw-r--r--
hggtk: add about dialog with context menu icon from TortoiseSVN
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
     1
#
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
     2
# front-end for TortoiseHg dialogs
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
     3
#
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
     4
# Copyright (C) 2007 TK Soh <teekaysoh@gmail.com>
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
     5
#
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
     6
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
     7
import os
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
     8
import sys
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
     9
from mercurial import ui
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    10
from tortoise.thgutil import find_path, get_prog_root, shellquote
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    11
413
fedc552d40c1 hgproc: always use our own hg exe
TK Soh <teekaysoh@yahoo.com>
parents: 412
diff changeset
    12
# always use hg exe installed with TortoiseHg
417
977ce2ff9fd4 hgproc: portable path insertion code
Steve Borho <steve@borho.org>
parents: 413
diff changeset
    13
thgdir = get_prog_root()
977ce2ff9fd4 hgproc: portable path insertion code
Steve Borho <steve@borho.org>
parents: 413
diff changeset
    14
try:
977ce2ff9fd4 hgproc: portable path insertion code
Steve Borho <steve@borho.org>
parents: 413
diff changeset
    15
    os.environ['PATH'] = os.path.pathsep.join([thgdir, os.environ['PATH']])
977ce2ff9fd4 hgproc: portable path insertion code
Steve Borho <steve@borho.org>
parents: 413
diff changeset
    16
except KeyError:
977ce2ff9fd4 hgproc: portable path insertion code
Steve Borho <steve@borho.org>
parents: 413
diff changeset
    17
    os.environ['PATH'] = thgdir
413
fedc552d40c1 hgproc: always use our own hg exe
TK Soh <teekaysoh@yahoo.com>
parents: 412
diff changeset
    18
412
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    19
if not sys.stdin.isatty():
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    20
    try:
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    21
        import win32traceutil
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    22
        
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    23
        # FIXME: quick workaround traceback caused by missing "closed" 
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    24
        # attribute in win32trace.
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    25
        from mercurial import ui
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    26
        def write_err(self, *args):
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    27
            for a in args:
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    28
                sys.stderr.write(str(a))
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    29
        ui.ui.write_err = write_err
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    30
    except ImportError:
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    31
        pass
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    32
    except pywintypes.error:
a7fb2f5548fa hgproc: do not redirect output if run from command line
TK Soh <teekaysoh@yahoo.com>
parents: 411
diff changeset
    33
        pass
279
7e4ceb3c0df8 hgproc: Safely catch win32traceutil import failure
Steve Borho <steve@borho.org>
parents: 258
diff changeset
    34
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    35
# Map hgproc commands to dialog modules in hggtk/
411
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
    36
from hggtk import commit, status, addremove, tagadd, tags, history, merge
422
ccdb70ac0a3c hggtk: add about dialog
TK Soh <teekaysoh@yahoo.com>
parents: 417
diff changeset
    37
from hggtk import diff, revisions, update, serve, clone, synch, hgcmd, about
411
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
    38
_dialogs = { 'commit' : commit,    'status' : status,    'revert' : status,
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
    39
             'add'    : addremove, 'remove' : addremove, 'tag'    : tagadd,
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
    40
             'tags'   : tags,      'log'    : history,   'history': history,
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
    41
             'diff'   : diff,      'merge'  : merge,     'tip'    : revisions,
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
    42
             'parents': revisions, 'heads'  : revisions, 'update' : update,
422
ccdb70ac0a3c hggtk: add about dialog
TK Soh <teekaysoh@yahoo.com>
parents: 417
diff changeset
    43
             'clone'  : clone,     'serve'  : serve,     'synch'  : synch,
ccdb70ac0a3c hggtk: add about dialog
TK Soh <teekaysoh@yahoo.com>
parents: 417
diff changeset
    44
             'about'  : about }
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    45
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    46
def get_list_from_file(filename):
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    47
    fd = open(filename, "r")
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    48
    lines = [ x.replace("\n", "") for x in fd.readlines() ]
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    49
    fd.close()
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    50
    return lines
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    51
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    52
def get_option(args):
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    53
    import getopt
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    54
    long_opt_list = ('command=', 'exepath=', 'listfile=', 'title=',
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    55
                      'root=', 'cwd=', 'notify', 'deletelistfile')
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    56
    opts, args = getopt.getopt(args, "c:e:l:ndt:R:", long_opt_list)
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    57
    # Set default options
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    58
    options = {}
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    59
    options['hgcmd'] = 'help'
410
82b9c4ff8bc0 hgproc: more bug fixes
Steve Borho <steve@borho.org>
parents: 407
diff changeset
    60
    options['hgpath'] = find_path('hg') or 'hg'
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    61
    options['cwd'] = os.getcwd()
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    62
    options['files'] = []
406
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    63
    listfile = None
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    64
    delfile = False
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    65
    
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    66
    for o, a in opts:
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    67
        if o in ("-c", "--command"):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    68
            options['hgcmd'] = a
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    69
        elif o in ("-l", "--listfile"):
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    70
            listfile = a
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    71
        elif o in ("-d", "--deletelistfile"):
406
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    72
            delfile = True
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    73
        elif o in ("-e", "--exepath"):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    74
            options['hgpath'] = a
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    75
        elif o in ("-n", "--notify"):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    76
            options['notify'] = True
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    77
        elif o in ("-t", "--title"):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    78
            options['title'] = a
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    79
        elif o in ("-R", "--root"):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    80
            options['root'] = a
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    81
        elif o in ("--cwd"):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    82
            options['cwd'] = a
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    83
406
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    84
    if listfile:
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    85
        options['files'] = get_list_from_file(listfile)
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    86
        if delfile:
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    87
            os.unlink(listfile)
4c73d42e0f73 hgproc: prevent deletion before listfile is read
Steve Borho <steve@borho.org>
parents: 401
diff changeset
    88
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    89
    return (options, args)
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    90
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    91
def parse(args):
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    92
    option, args = get_option(args)
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    93
    
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    94
    cmdline = [option['hgpath'], option['hgcmd']] 
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
    95
    if option.has_key('root'):
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    96
        cmdline.append('--repository')
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    97
        cmdline.append(option['root'])
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    98
    cmdline.extend(args)
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
    99
    cmdline.extend(option['files'])
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   100
    option['cmdline'] = cmdline
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   101
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   102
    # Failsafe choice for merge tool
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   103
    if os.environ.get('HGMERGE', None):
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   104
        pass
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   105
    elif ui.ui().config('ui', 'merge', None):
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   106
        pass
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   107
    else:
410
82b9c4ff8bc0 hgproc: more bug fixes
Steve Borho <steve@borho.org>
parents: 407
diff changeset
   108
        path = find_path('simplemerge') or 'simplemerge'
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   109
        os.environ['HGMERGE'] = '%s -L my -L other' % shellquote(path)
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   110
        print "override HGMERGE =", os.environ['HGMERGE']
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   111
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   112
    global _dialogs
411
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
   113
    dialog = _dialogs.get(option['hgcmd'], hgcmd)
5dce1d702fd9 hgproc: import dialog modules so py2exe can pick them up
TK Soh <teekaysoh@yahoo.com>
parents: 410
diff changeset
   114
    dialog.run(**option)
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   115
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   116
248
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   117
def run_trapped(args):
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   118
    try:
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   119
        dlg = parse(sys.argv[1:])
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   120
    except:
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   121
        import traceback
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   122
        from hggtk.dialog import error_dialog
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   123
        tr = traceback.format_exc()
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   124
        print tr
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   125
        error_dialog("Error executing hgproc", tr)
248
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   126
157
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   127
if __name__=='__main__':
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   128
    #dlg = parse(['-c', 'help', '--', '-v'])
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   129
    #dlg = parse(['-c', 'log', '--root', 'c:\hg\h1', '--', '-l1'])
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   130
    #dlg = parse(['-c', 'status', '--root', 'c:\hg\h1', ])
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   131
    #dlg = parse(['-c', 'add', '--root', 'c:\hg\h1', '--listfile', 'c:\\hg\\h1\\f1', '--notify'])
b2d6126fa639 add hgproc module as dialog front-end
TK Soh <teekaysoh@yahoo.com>
parents:
diff changeset
   132
    #dlg = parse(['-c', 'rollback', '--root', 'c:\\hg\\h1'])
401
9ab309fbc25c hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents: 370
diff changeset
   133
    print "hgproc sys.argv =", sys.argv
248
332bc04020ec hgproc: add basic error handling
TK Soh <teekaysoh@yahoo.com>
parents: 237
diff changeset
   134
    dlg = run_trapped(sys.argv[1:])