__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: UTF-8 -*-
  2. # The contents of this file are subject to the BitTorrent Open Source License
  3. # Version 1.1 (the License). You may not copy or use this file, in either
  4. # source code or executable form, except in compliance with the License. You
  5. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  6. #
  7. # Software distributed under the License is distributed on an AS IS basis,
  8. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  9. # for the specific language governing rights and limitations under the
  10. # License.
  11. version = '5.2.0'
  12. URL = 'http://www.bittorrent.com/'
  13. DONATE_URL = URL + 'donate.html?client=%(client)s'
  14. FAQ_URL = URL + 'FAQ.html?client=%(client)s'
  15. SEARCH_URL = 'http://www.bittorrent.com/search_result.html?client=%(client)s&search=%(search)s'
  16. #LOCALE_URL = URL + 'translations/'
  17. # Moved to BTL. Needed for get_language <- BTL.write_language_file
  18. #LOCALE_URL = 'http://translations.bittorrent.com/'
  19. NAG_FREQUENCY = 3
  20. PORT_RANGE = 5
  21. import sys
  22. assert sys.version_info >= (2, 3, 0), "Python %s or newer required" % '2.3.0'
  23. import os
  24. import logging
  25. import logging.handlers
  26. from StringIO import StringIO
  27. from BTL import BTFailure, InfoHashType
  28. from BTL import atexit_threads
  29. # failure due to user error. Should output differently (e.g., not outputting
  30. # a backtrace).
  31. class UserFailure(BTFailure):
  32. pass
  33. branch = None
  34. p = os.path.realpath(os.path.split(sys.argv[0])[0])
  35. if (os.path.exists(os.path.join(p, '.cdv')) or
  36. os.path.exists(os.path.join(p, '.svn'))):
  37. branch = os.path.split(p)[1]
  38. del p
  39. from BitTorrent.platform import get_temp_subdir, get_dot_dir, is_frozen_exe
  40. if os.name == 'posix':
  41. if os.uname()[0] == "Darwin":
  42. from BTL.translation import _
  43. if "-u" in sys.argv or "--use_factory_defaults" in sys.argv:
  44. logroot = get_temp_subdir()
  45. else:
  46. #logroot = get_home_dir()
  47. logroot = get_dot_dir()
  48. if is_frozen_exe:
  49. if logroot is None:
  50. logroot = os.path.splitdrive(sys.executable)[0]
  51. if logroot[-1] != os.sep:
  52. logroot += os.sep
  53. logname = os.path.split(sys.executable)[1]
  54. else:
  55. logname = os.path.split(os.path.abspath(sys.argv[0]))[1]
  56. logname = os.path.splitext(logname)[0] + '.log'
  57. if logroot != '' and not os.path.exists(logroot):
  58. os.makedirs(logroot)
  59. logpath = os.path.join(logroot, logname)
  60. # becuase I'm generous.
  61. STDERR = logging.CRITICAL + 10
  62. logging.addLevelName(STDERR, 'STDERR')
  63. # define a Handler which writes INFO messages or higher to the sys.stderr
  64. console = logging.StreamHandler()
  65. console.setLevel(logging.DEBUG)
  66. # set a format which is simpler for console use
  67. #formatter = logging.Formatter(u'%(name)-12s: %(levelname)-8s %(message)s')
  68. formatter = logging.Formatter(u'%(message)s')
  69. # tell the handler to use this format
  70. console.setFormatter(formatter)
  71. # add the handler to the root logger
  72. logging.getLogger('').addHandler(console)
  73. bt_log_fmt = logging.Formatter(u'[' + unicode(version) + u' %(asctime)s] %(levelname)-8s: %(message)s',
  74. datefmt=u'%Y-%m-%d %H:%M:%S')
  75. stderr_console = None
  76. old_stderr = sys.stderr
  77. def inject_main_logfile():
  78. # the main log file. log every kind of message, format properly,
  79. # rotate the log. someday - SocketHandler
  80. mainlog = logging.handlers.RotatingFileHandler(filename=logpath,
  81. mode='a', maxBytes=2**20, backupCount=1)
  82. mainlog.setFormatter(bt_log_fmt)
  83. mainlog.setLevel(logging.DEBUG)
  84. logger = logging.getLogger('')
  85. logging.getLogger('').addHandler(mainlog)
  86. logging.getLogger('').removeHandler(console)
  87. atexit_threads.register(lambda : logging.getLogger('').removeHandler(mainlog))
  88. global stderr_console
  89. if not is_frozen_exe:
  90. # write all stderr messages to stderr (unformatted)
  91. # as well as the main log (formatted)
  92. stderr_console = logging.StreamHandler(old_stderr)
  93. stderr_console.setLevel(STDERR)
  94. stderr_console.setFormatter(logging.Formatter(u'%(message)s'))
  95. logging.getLogger('').addHandler(stderr_console)
  96. root_logger = logging.getLogger('')
  97. class StderrProxy(StringIO):
  98. # whew. ugly. is there a simpler way to write this?
  99. # the goal is to stop every '\n' and flush to the log
  100. # otherwise keep buffering.
  101. def write(self, text, *args):
  102. lines = text.split('\n')
  103. for t in lines[:-1]:
  104. if len(t) > 0:
  105. StringIO.write(self, t)
  106. try:
  107. # the docs don't say it, but logging.log is new in 2.4
  108. #logging.log(STDERR, self.getvalue())
  109. root_logger.log(STDERR, self.getvalue())
  110. except:
  111. # logging failed. throwing a traceback would recurse
  112. pass
  113. self.truncate(0)
  114. if len(lines[-1]) > 0:
  115. StringIO.write(self, lines[-1])
  116. sys.stderr = StderrProxy()
  117. def reset_stderr():
  118. sys.stderr = old_stderr
  119. atexit_threads.register(reset_stderr)