PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/thg

https://bitbucket.org/tortoisehg/hgtk/
Python | 107 lines | 86 code | 10 blank | 11 comment | 31 complexity | 66cfe662ab70effe89698b967230afde MD5 | raw file
  1#!/usr/bin/env python
  2#
  3# thg - front-end script for TortoiseHg dialogs
  4#
  5# Copyright (C) 2008-2010 Steve Borho <steve@borho.org>
  6# Copyright (C) 2008 TK Soh <teekaysoh@gmail.com>
  7#
  8# This software may be used and distributed according to the terms of the
  9# GNU General Public License version 2, incorporated herein by reference.
 10
 11import os
 12import sys
 13
 14if hasattr(sys, "frozen"):
 15    if sys.frozen == 'windows_exe' and 'THGDEBUG' in os.environ:
 16        import win32traceutil
 17        print 'starting'
 18    # os.Popen() needs this, and Mercurial still uses os.Popen
 19    if 'COMSPEC' not in os.environ:
 20        comspec = os.path.join(os.environ.get('SystemRoot', r'C:\Windows'),
 21                               'system32', 'cmd.exe')
 22        os.environ['COMSPEC'] = comspec
 23else:
 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    # compile .ui and .qrc for in-place use
 30    fpath = os.path.realpath(__file__)
 31    if os.path.exists(os.path.join(os.path.dirname(fpath), 'setup.py')):
 32        from distutils.dist import Distribution
 33        from setup import build_qt
 34        build_qt(Distribution()).run()
 35
 36    if 'HGPATH' in os.environ:
 37        hgpath = os.environ['HGPATH']
 38        testpath = os.path.join(hgpath, 'mercurial')
 39        if os.path.isdir(testpath) and hgpath not in sys.path:
 40            sys.path.insert(0, hgpath)
 41
 42from mercurial import demandimport
 43demandimport.ignore.append('win32com.shell')
 44demandimport.ignore.append('tortoisehg.util.config')
 45demandimport.ignore.append('workbench_rc')
 46demandimport.enable()
 47from mercurial import ui as uimod, util
 48from tortoisehg.util.hgversion import hgversion, checkhgversion
 49import cStringIO
 50import traceback
 51
 52try:
 53    import tortoisehg.hgqt.run
 54except ImportError, e:
 55    sys.stderr.write(str(e)+'\n')
 56    sys.stderr.write("abort: couldn't find tortoisehg libraries in [%s]\n" %
 57                     os.pathsep.join(sys.path))
 58    sys.stderr.write("(check your install and PYTHONPATH)\n")
 59    sys.exit(-1)
 60
 61ui = uimod.ui()
 62capt = ui.configbool('tortoisehg', 'stderrcapt', True)
 63
 64errors = ('Traceback', 'TypeError', 'NameError', 'AttributeError',
 65          'NotImplementedError')
 66
 67err = checkhgversion(hgversion)
 68if err:
 69    from tortoisehg.hgqt.bugreport import run
 70    from tortoisehg.hgqt.run import qtrun
 71    opts = {}
 72    opts['cmd'] = ' '.join(sys.argv[1:])
 73    opts['error'] = '\n' + err + '\n'
 74    opts['nofork'] = True
 75    qtrun(run, ui, **opts)
 76    sys.exit(1)
 77
 78if not capt or 'THGDEBUG' in os.environ or '--profile' in sys.argv:
 79    sys.exit(tortoisehg.hgqt.run.dispatch(sys.argv[1:]))
 80else:
 81    mystderr = cStringIO.StringIO()
 82    origstderr = sys.stderr
 83    sys.stderr = mystderr
 84    ret = 0
 85    try:
 86        ret = tortoisehg.hgqt.run.dispatch(sys.argv[1:])
 87        sys.stderr = origstderr
 88        mystderr.seek(0)
 89        for l in mystderr.readlines():
 90            if l.startswith(errors):
 91                from tortoisehg.hgqt.bugreport import run
 92                from tortoisehg.hgqt.run import qtrun
 93                error = 'Recoverable runtime error (stderr):\n'
 94                error += mystderr.getvalue()
 95                opts = {}
 96                opts['cmd'] = ' '.join(sys.argv[1:])
 97                opts['error'] = error
 98                opts['nofork'] = True
 99                qtrun(run, ui, **opts)
100                break
101        sys.exit(ret)
102    except:
103        if sys.exc_info()[0] not in [SystemExit, KeyboardInterrupt]:
104            sys.stderr = origstderr
105            traceback.print_exc()
106        else:
107            raise SystemExit(ret)