PageRenderTime 5ms CodeModel.GetById 2ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

/hgtk

https://bitbucket.org/tortoisehg/hgtk/
Python | 94 lines | 75 code | 9 blank | 10 comment | 18 complexity | 764701bedf56ef5b1d20a4fa74b7dc14 MD5 | raw file
 1#!/usr/bin/env python
 2#
 3# front-end script for TortoiseHg dialogs
 4#
 5# Copyright (C) 2008-10 Steve Borho <steve@borho.org>
 6# Copyright (C) 2008 TK Soh <teekaysoh@gmail.com>
 7#
 8
 9import os
10import sys
11
12if hasattr(sys, "frozen"):
13    from tortoisehg.util import paths
14    gtkpath = os.path.join(paths.bin_path, 'gtk')
15    try:
16        # may fail on Windows XP without SP1
17        import win32api
18        win32api.SetDllDirectory(gtkpath)
19    except:
20        # fallback to prepending gtk/ folder at start of %PATH%
21        os.environ['PATH'] = os.pathsep.join([gtkpath, os.environ['PATH']])
22else:
23    # if hgtk is a symlink, insert symlink target directory in sys.path
24    thgpath = os.path.dirname(os.path.realpath(__file__))
25    testpath = os.path.join(thgpath, 'tortoisehg')
26    if os.path.isdir(testpath) and thgpath not in sys.path:
27        sys.path.insert(0, thgpath)
28
29
30import pygtk
31pygtk.require('2.0')
32import gtk
33
34from mercurial import demandimport
35demandimport.ignore.append('win32com.shell')
36demandimport.ignore.append('tortoisehg.util.config')
37demandimport.enable()
38from mercurial import ui, util
39from tortoisehg.util.hgversion import hgversion, checkhgversion
40import cStringIO
41import traceback
42
43try:
44    import tortoisehg.hgtk.hgtk
45except ImportError:
46    sys.stderr.write("abort: couldn't find hgtk libraries in [%s]\n" %
47                     ';'.join(sys.path))
48    sys.stderr.write("(check your install and PYTHONPATH)\n")
49    sys.exit(-1)
50
51_ui = ui.ui()
52capt = _ui.configbool('tortoisehg', 'stderrcapt', True)
53
54err = checkhgversion(hgversion)
55if err:
56    from tortoisehg.hgtk.bugreport import run
57    from tortoisehg.hgtk.hgtk import gtkrun
58    opts = {}
59    opts['cmd'] = ' '.join(sys.argv[1:])
60    opts['error'] = err
61    opts['nofork'] = True
62    gtkrun(run, _ui, **opts)
63    sys.exit(1)
64
65if not capt or 'THGDEBUG' in os.environ or '--profile' in sys.argv:
66    sys.exit(tortoisehg.hgtk.hgtk.dispatch(sys.argv[1:]))
67else:
68    mystderr = cStringIO.StringIO()
69    origstderr = sys.stderr
70    sys.stderr = mystderr
71    ret = 0
72    try:
73        ret = tortoisehg.hgtk.hgtk.dispatch(sys.argv[1:])
74        sys.stderr = origstderr
75        mystderr.seek(0)
76        for l in mystderr.readlines():
77            if l.startswith(('Traceback', 'TypeError', 'NameError')):
78                from tortoisehg.hgtk.bugreport import run
79                from tortoisehg.hgtk.hgtk import gtkrun
80                error = 'Recoverable runtime error (stderr):\n'
81                error += mystderr.getvalue()
82                opts = {}
83                opts['cmd'] = ' '.join(sys.argv[1:])
84                opts['error'] = error
85                opts['nofork'] = True
86                gtkrun(run, _ui, **opts)
87                break
88        sys.exit(ret)
89    except:
90        if sys.exc_info()[0] not in [SystemExit, KeyboardInterrupt]:
91            sys.stderr = origstderr
92            traceback.print_exc()
93        else:
94            raise SystemExit(ret)