#!/usr/bin/env python## thg - front-end script for TortoiseHg dialogs## Copyright (C) 2008-2011 Steve Borho <steve@borho.org># Copyright (C) 2008 TK Soh <teekaysoh@gmail.com>## This software may be used and distributed according to the terms of the# GNU General Public License version 2, incorporated herein by reference.importosimportsysargv=sys.argv[1:]if'THG_OSX_APP'inos.environ:# Remove the -psn argument supplied by launchdifargv[0].startswith('-psn'):argv=argv[1:]# sys.path as created by py2app doesn't work quite right with demandimport# Add the explicit path where PyQt4 and other libs arebundlepath=os.path.dirname(os.path.realpath(__file__))sys.path.insert(0,os.path.join(bundlepath,'lib/python2.6/lib-dynload'))ifhasattr(sys,"frozen"):ifsys.frozen=='windows_exe'and'THGDEBUG'inos.environ:importwin32traceutilprint'starting'# os.Popen() needs this, and Mercurial still uses os.Popenif'COMSPEC'notinos.environ:comspec=os.path.join(os.environ.get('SystemRoot',r'C:\Windows'),'system32','cmd.exe')os.environ['COMSPEC']=comspecelse:thgpath=os.path.dirname(os.path.realpath(__file__))testpath=os.path.join(thgpath,'tortoisehg')ifos.path.isdir(testpath)andthgpathnotinsys.path:sys.path.insert(0,thgpath)# compile .ui and .qrc for in-place usefpath=os.path.realpath(__file__)ifos.path.exists(os.path.join(os.path.dirname(fpath),'setup.py')):fromdistutils.distimportDistributionfromsetupimportbuild_qtbuild_qt(Distribution()).run()if'HGPATH'inos.environ:hgpath=os.environ['HGPATH']testpath=os.path.join(hgpath,'mercurial')ifos.path.isdir(testpath)andhgpathnotinsys.path:sys.path.insert(0,hgpath)frommercurialimportdemandimportdemandimport.ignore.append('win32com.shell')demandimport.ignore.append('tortoisehg.util.config')demandimport.ignore.append('icons_rc')demandimport.ignore.append('translations_rc')demandimport.enable()# Verify we can reach TortoiseHg sources firsttry:importtortoisehg.hgqt.runexceptImportError,e:sys.stderr.write(str(e)+'\n')sys.stderr.write("abort: couldn't find tortoisehg libraries in [%s]\n"%os.pathsep.join(sys.path))sys.stderr.write("(check your install and PYTHONPATH)\n")sys.exit(-1)# Verify we have an acceptable version of Mercurialfromtortoisehg.util.hgversionimporthgversion,checkhgversionerrmsg=checkhgversion(hgversion)iferrmsg:frommercurialimportuifromtortoisehg.hgqt.bugreportimportrunfromtortoisehg.hgqt.runimportqtrunopts={}opts['cmd']=' '.join(argv)opts['error']='\n'+errmsg+'\n'opts['nofork']=Trueqtrun(run,ui.ui(),**opts)sys.exit(1)if'THGDEBUG'inos.environor'--profile'insys.argv:sys.exit(tortoisehg.hgqt.run.dispatch(argv))else:importcStringIOmystderr=cStringIO.StringIO()origstderr=sys.stderrsys.stderr=mystderrsys.__stdout__=sys.stdoutsys.__stderr__=sys.stderrret=0try:ret=tortoisehg.hgqt.run.dispatch(argv)sys.stderr=origstderrstderrout=mystderr.getvalue()errors=('Traceback','TypeError','NameError','AttributeError','NotImplementedError')forlinstderrout.splitlines():ifl.startswith(errors):frommercurialimportuifromtortoisehg.hgqt.bugreportimportrunfromtortoisehg.hgqt.runimportqtrunopts={}opts['cmd']=' '.join(argv)opts['error']='Recoverable error (stderr):\n'+stderroutopts['nofork']=Trueqtrun(run,ui.ui(),**opts)breaksys.exit(ret)except:ifsys.exc_info()[0]notin[SystemExit,KeyboardInterrupt]:importtracebacksys.stderr=origstderrtraceback.print_exc()else:raiseSystemExit(ret)