PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/thg

https://bitbucket.org/tortoisehg/hgtk/
Python | 107 lines | 86 code | 10 blank | 11 comment | 23 complexity | 66cfe662ab70effe89698b967230afde MD5 | raw file
Possible License(s): GPL-2.0
  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. import os
  11. import sys
  12. if hasattr(sys, "frozen"):
  13. if sys.frozen == 'windows_exe' and 'THGDEBUG' in os.environ:
  14. import win32traceutil
  15. print 'starting'
  16. # os.Popen() needs this, and Mercurial still uses os.Popen
  17. if 'COMSPEC' not in os.environ:
  18. comspec = os.path.join(os.environ.get('SystemRoot', r'C:\Windows'),
  19. 'system32', 'cmd.exe')
  20. os.environ['COMSPEC'] = comspec
  21. else:
  22. thgpath = os.path.dirname(os.path.realpath(__file__))
  23. testpath = os.path.join(thgpath, 'tortoisehg')
  24. if os.path.isdir(testpath) and thgpath not in sys.path:
  25. sys.path.insert(0, thgpath)
  26. # compile .ui and .qrc for in-place use
  27. fpath = os.path.realpath(__file__)
  28. if os.path.exists(os.path.join(os.path.dirname(fpath), 'setup.py')):
  29. from distutils.dist import Distribution
  30. from setup import build_qt
  31. build_qt(Distribution()).run()
  32. if 'HGPATH' in os.environ:
  33. hgpath = os.environ['HGPATH']
  34. testpath = os.path.join(hgpath, 'mercurial')
  35. if os.path.isdir(testpath) and hgpath not in sys.path:
  36. sys.path.insert(0, hgpath)
  37. from mercurial import demandimport
  38. demandimport.ignore.append('win32com.shell')
  39. demandimport.ignore.append('tortoisehg.util.config')
  40. demandimport.ignore.append('workbench_rc')
  41. demandimport.enable()
  42. from mercurial import ui as uimod, util
  43. from tortoisehg.util.hgversion import hgversion, checkhgversion
  44. import cStringIO
  45. import traceback
  46. try:
  47. import tortoisehg.hgqt.run
  48. except ImportError, e:
  49. sys.stderr.write(str(e)+'\n')
  50. sys.stderr.write("abort: couldn't find tortoisehg libraries in [%s]\n" %
  51. os.pathsep.join(sys.path))
  52. sys.stderr.write("(check your install and PYTHONPATH)\n")
  53. sys.exit(-1)
  54. ui = uimod.ui()
  55. capt = ui.configbool('tortoisehg', 'stderrcapt', True)
  56. errors = ('Traceback', 'TypeError', 'NameError', 'AttributeError',
  57. 'NotImplementedError')
  58. err = checkhgversion(hgversion)
  59. if err:
  60. from tortoisehg.hgqt.bugreport import run
  61. from tortoisehg.hgqt.run import qtrun
  62. opts = {}
  63. opts['cmd'] = ' '.join(sys.argv[1:])
  64. opts['error'] = '\n' + err + '\n'
  65. opts['nofork'] = True
  66. qtrun(run, ui, **opts)
  67. sys.exit(1)
  68. if not capt or 'THGDEBUG' in os.environ or '--profile' in sys.argv:
  69. sys.exit(tortoisehg.hgqt.run.dispatch(sys.argv[1:]))
  70. else:
  71. mystderr = cStringIO.StringIO()
  72. origstderr = sys.stderr
  73. sys.stderr = mystderr
  74. ret = 0
  75. try:
  76. ret = tortoisehg.hgqt.run.dispatch(sys.argv[1:])
  77. sys.stderr = origstderr
  78. mystderr.seek(0)
  79. for l in mystderr.readlines():
  80. if l.startswith(errors):
  81. from tortoisehg.hgqt.bugreport import run
  82. from tortoisehg.hgqt.run import qtrun
  83. error = 'Recoverable runtime error (stderr):\n'
  84. error += mystderr.getvalue()
  85. opts = {}
  86. opts['cmd'] = ' '.join(sys.argv[1:])
  87. opts['error'] = error
  88. opts['nofork'] = True
  89. qtrun(run, ui, **opts)
  90. break
  91. sys.exit(ret)
  92. except:
  93. if sys.exc_info()[0] not in [SystemExit, KeyboardInterrupt]:
  94. sys.stderr = origstderr
  95. traceback.print_exc()
  96. else:
  97. raise SystemExit(ret)