/setup.py

https://bitbucket.org/tortoisehg/hgtk/ · Python · 388 lines · 361 code · 17 blank · 10 comment · 23 complexity · 13b1b93f72868d5b4c6c404abab60e97 MD5 · raw file

  1. # setup.py
  2. # A distutils setup script to install TortoiseHg in Windows and Posix
  3. # environments.
  4. #
  5. # On Windows, this script is mostly used to build a stand-alone
  6. # TortoiseHg package. See installer\build.txt for details. The other
  7. # use is to report the current version of the TortoiseHg source.
  8. import time
  9. import sys
  10. import os
  11. import shutil
  12. import subprocess
  13. from fnmatch import fnmatch
  14. from distutils import log
  15. from distutils.core import setup, Command
  16. from distutils.command.build import build as _build_orig
  17. from distutils.command.clean import clean as _clean_orig
  18. from distutils.dep_util import newer
  19. from distutils.spawn import spawn, find_executable
  20. from os.path import isdir, exists, join, walk, splitext
  21. thgcopyright = 'Copyright (C) 2010 Steve Borho and others'
  22. hgcopyright = 'Copyright (C) 2005-2010 Matt Mackall and others'
  23. class build_mo(Command):
  24. description = "build translations (.mo files)"
  25. user_options = []
  26. def initialize_options(self):
  27. pass
  28. def finalize_options(self):
  29. pass
  30. def run(self):
  31. if not find_executable('msgfmt'):
  32. self.warn("could not find msgfmt executable, no translations "
  33. "will be built")
  34. return
  35. podir = 'i18n/tortoisehg'
  36. if not os.path.isdir(podir):
  37. self.warn("could not find %s/ directory" % podir)
  38. return
  39. join = os.path.join
  40. for po in os.listdir(podir):
  41. if not po.endswith('.po'):
  42. continue
  43. pofile = join(podir, po)
  44. modir = join('locale', po[:-3], 'LC_MESSAGES')
  45. mofile = join(modir, 'tortoisehg.mo')
  46. cmd = ['msgfmt', '-v', '-o', mofile, pofile]
  47. if sys.platform != 'sunos5':
  48. # msgfmt on Solaris does not know about -c
  49. cmd.append('-c')
  50. self.mkpath(modir)
  51. self.make_file([pofile], mofile, spawn, (cmd,))
  52. class build_qt(Command):
  53. description = "build PyQt GUIs (.ui) and resources (.qrc)"
  54. user_options = [('force', 'f', 'forcibly compile everything'
  55. ' (ignore file timestamps)')]
  56. boolean_options = ('force',)
  57. def initialize_options(self):
  58. self.force = None
  59. def finalize_options(self):
  60. self.set_undefined_options('build', ('force', 'force'))
  61. def compile_ui(self, ui_file, py_file=None):
  62. # Search for pyuic4 in python bin dir, then in the $Path.
  63. if py_file is None:
  64. py_file = splitext(ui_file)[0] + "_ui.py"
  65. if not(self.force or newer(ui_file, py_file)):
  66. return
  67. try:
  68. from PyQt4 import uic
  69. fp = open(py_file, 'w')
  70. uic.compileUi(ui_file, fp)
  71. fp.close()
  72. log.info('compiled %s into %s' % (ui_file, py_file))
  73. except Exception, e:
  74. self.warn('Unable to compile user interface %s: %s' % (py_file, e))
  75. if not exists(py_file) or not file(py_file).read():
  76. raise SystemExit(1)
  77. return
  78. def compile_rc(self, qrc_file, py_file=None):
  79. # Search for pyuic4 in python bin dir, then in the $Path.
  80. if py_file is None:
  81. py_file = splitext(qrc_file)[0] + "_rc.py"
  82. if not(self.force or newer(qrc_file, py_file)):
  83. return
  84. if os.system('pyrcc4 "%s" -o "%s"' % (qrc_file, py_file)) > 0:
  85. self.warn("Unable to generate python module %s for resource file %s"
  86. % (py_file, qrc_file))
  87. if not exists(py_file) or not file(py_file).read():
  88. raise SystemExit(1)
  89. else:
  90. log.info('compiled %s into %s' % (qrc_file, py_file))
  91. def run(self):
  92. self._wrapuic()
  93. basepath = join(os.path.dirname(__file__), 'tortoisehg', 'hgqt')
  94. for dirpath, _, filenames in os.walk(basepath):
  95. for filename in filenames:
  96. if filename.endswith('.ui'):
  97. self.compile_ui(join(dirpath, filename))
  98. elif filename.endswith('.qrc'):
  99. self.compile_rc(join(dirpath, filename))
  100. _wrappeduic = False
  101. @classmethod
  102. def _wrapuic(cls):
  103. """wrap uic to use gettext's _() in place of tr()"""
  104. if cls._wrappeduic:
  105. return
  106. from PyQt4.uic.Compiler import compiler, qtproxies, indenter
  107. class _UICompiler(compiler.UICompiler):
  108. def createToplevelWidget(self, classname, widgetname):
  109. o = indenter.getIndenter()
  110. o.level = 0
  111. o.write('from tortoisehg.hgqt.i18n import _')
  112. return super(_UICompiler, self).createToplevelWidget(classname, widgetname)
  113. compiler.UICompiler = _UICompiler
  114. class _i18n_string(qtproxies.i18n_string):
  115. def __str__(self):
  116. return "_('%s')" % self.string.encode('string-escape')
  117. qtproxies.i18n_string = _i18n_string
  118. cls._wrappeduic = True
  119. class clean_local(Command):
  120. pats = ['*.py[co]', '*_ui.py', '*_rc.py', '*.orig', '*.rej']
  121. excludedirs = ['.hg', 'build', 'dist']
  122. description = 'clean up generated files (%s)' % ', '.join(pats)
  123. user_options = []
  124. def initialize_options(self):
  125. pass
  126. def finalize_options(self):
  127. pass
  128. def run(self):
  129. for e in self._walkpaths('.'):
  130. log.info("removing '%s'" % e)
  131. os.remove(e)
  132. def _walkpaths(self, path):
  133. for root, _dirs, files in os.walk(path):
  134. if any(root == join(path, e) or root.startswith(join(path, e, ''))
  135. for e in self.excludedirs):
  136. continue
  137. for e in files:
  138. fpath = join(root, e)
  139. if any(fnmatch(fpath, p) for p in self.pats):
  140. yield fpath
  141. class build(_build_orig):
  142. sub_commands = [
  143. ('build_qt', None),
  144. ('build_mo', None),
  145. ] + _build_orig.sub_commands
  146. class clean(_clean_orig):
  147. sub_commands = [
  148. ('clean_local', None),
  149. ] + _clean_orig.sub_commands
  150. def run(self):
  151. _clean_orig.run(self)
  152. for e in self.get_sub_commands():
  153. self.run_command(e)
  154. cmdclass = {
  155. 'build': build,
  156. 'build_qt': build_qt ,
  157. 'build_mo': build_mo ,
  158. 'clean': clean,
  159. 'clean_local': clean_local,
  160. }
  161. def setup_windows(version):
  162. # Specific definitios for Windows NT-alike installations
  163. _scripts = []
  164. _data_files = []
  165. _packages = ['tortoisehg.hgqt', 'tortoisehg.util', 'tortoisehg']
  166. extra = {}
  167. hgextmods = []
  168. # py2exe needs to be installed to work
  169. try:
  170. import py2exe
  171. # Help py2exe to find win32com.shell
  172. try:
  173. import modulefinder
  174. import win32com
  175. for p in win32com.__path__[1:]: # Take the path to win32comext
  176. modulefinder.AddPackagePath("win32com", p)
  177. pn = "win32com.shell"
  178. __import__(pn)
  179. m = sys.modules[pn]
  180. for p in m.__path__[1:]:
  181. modulefinder.AddPackagePath(pn, p)
  182. except ImportError:
  183. pass
  184. except ImportError:
  185. if '--version' not in sys.argv:
  186. raise
  187. if 'py2exe' in sys.argv:
  188. import hgext
  189. hgextdir = os.path.dirname(hgext.__file__)
  190. hgextmods = set(["hgext." + os.path.splitext(f)[0]
  191. for f in os.listdir(hgextdir)])
  192. _data_files = [(root, [os.path.join(root, file_) for file_ in files])
  193. for root, dirs, files in os.walk('icons')]
  194. # for PyQt, see http://www.py2exe.org/index.cgi/Py2exeAndPyQt
  195. includes = ['sip']
  196. # Qt4 plugins, see http://stackoverflow.com/questions/2206406/
  197. def qt4_plugins(subdir, *dlls):
  198. import PyQt4
  199. pluginsdir = join(os.path.dirname(PyQt4.__file__), 'plugins')
  200. return (subdir, [join(pluginsdir, subdir, e) for e in dlls])
  201. _data_files.append(qt4_plugins('imageformats', 'qico4.dll', 'qsvg4.dll'))
  202. # Manually include other modules py2exe can't find by itself.
  203. if 'hgext.highlight' in hgextmods:
  204. includes += ['pygments.*', 'pygments.lexers.*', 'pygments.formatters.*',
  205. 'pygments.filters.*', 'pygments.styles.*']
  206. if 'hgext.patchbomb' in hgextmods:
  207. includes += ['email.*', 'email.mime.*']
  208. extra['options'] = {
  209. "py2exe" : {
  210. "skip_archive" : 0,
  211. # Don't pull in all this MFC stuff used by the makepy UI.
  212. "excludes" : "pywin,pywin.dialogs,pywin.dialogs.list"
  213. ",setup,distutils", # required only for in-place use
  214. "includes" : includes,
  215. "optimize" : 1
  216. }
  217. }
  218. shutil.copyfile('thg', 'thgw')
  219. extra['console'] = [
  220. {'script':'thg',
  221. 'icon_resources':[(0,'icons/thg_logo.ico')],
  222. 'description':'TortoiseHg GUI tools for Mercurial SCM',
  223. 'copyright':thgcopyright,
  224. 'product_version':version},
  225. {'script':'contrib/hg',
  226. 'icon_resources':[(0,'icons/hg.ico')],
  227. 'description':'Mercurial Distributed SCM',
  228. 'copyright':hgcopyright,
  229. 'product_version':version},
  230. {'script':'win32/docdiff.py',
  231. 'icon_resources':[(0,'icons/TortoiseMerge.ico')],
  232. 'copyright':thgcopyright,
  233. 'product_version':version}
  234. ]
  235. extra['windows'] = [
  236. {'script':'thgw',
  237. 'icon_resources':[(0,'icons/thg_logo.ico')],
  238. 'description':'TortoiseHg GUI tools for Mercurial SCM',
  239. 'copyright':thgcopyright,
  240. 'product_version':version},
  241. {'script':'TortoiseHgOverlayServer.py',
  242. 'icon_resources':[(0,'icons/thg_logo.ico')],
  243. 'description':'TortoiseHg Overlay Icon Server',
  244. 'copyright':thgcopyright,
  245. 'product_version':version}
  246. ]
  247. return _scripts, _packages, _data_files, extra
  248. def setup_posix():
  249. # Specific definitios for Posix installations
  250. _extra = {}
  251. _scripts = ['thg']
  252. _packages = ['tortoisehg', 'tortoisehg.hgqt', 'tortoisehg.util']
  253. _data_files = [(os.path.join('share/pixmaps/tortoisehg', root),
  254. [os.path.join(root, file_) for file_ in files])
  255. for root, dirs, files in os.walk('icons')]
  256. _data_files += [(os.path.join('share', root),
  257. [os.path.join(root, file_) for file_ in files])
  258. for root, dirs, files in os.walk('locale')]
  259. _data_files += [('lib/nautilus/extensions-2.0/python',
  260. ['contrib/nautilus-thg.py'])]
  261. # Create a config.py. Distributions will need to supply their own
  262. cfgfile = os.path.join('tortoisehg', 'util', 'config.py')
  263. if not os.path.exists(cfgfile) and not os.path.exists('.hg/requires'):
  264. f = open(cfgfile, "w")
  265. f.write('bin_path = "/usr/bin"\n')
  266. f.write('license_path = "/usr/share/doc/tortoisehg/Copying.txt.gz"\n')
  267. f.write('locale_path = "/usr/share/locale"\n')
  268. f.write('icon_path = "/usr/share/pixmaps/tortoisehg/icons"\n')
  269. f.write('nofork = True\n')
  270. f.close()
  271. return _scripts, _packages, _data_files, _extra
  272. def runcmd(cmd, env):
  273. p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
  274. stderr=subprocess.PIPE, env=env)
  275. out, err = p.communicate()
  276. # If root is executing setup.py, but the repository is owned by
  277. # another user (as in "sudo python setup.py install") we will get
  278. # trust warnings since the .hg/hgrc file is untrusted. That is
  279. # fine, we don't want to load it anyway.
  280. err = [e for e in err.splitlines()
  281. if not e.startswith('Not trusting file')]
  282. if err:
  283. return ''
  284. return out
  285. if __name__ == '__main__':
  286. version = ''
  287. if os.path.isdir('.hg'):
  288. from tortoisehg.util import version as _version
  289. branch, version = _version.liveversion()
  290. if version.endswith('+'):
  291. version += time.strftime('%Y%m%d')
  292. elif os.path.exists('.hg_archival.txt'):
  293. kw = dict([t.strip() for t in l.split(':', 1)]
  294. for l in open('.hg_archival.txt'))
  295. if 'tag' in kw:
  296. version = kw['tag']
  297. elif 'latesttag' in kw:
  298. version = '%(latesttag)s+%(latesttagdistance)s-%(node).12s' % kw
  299. else:
  300. version = kw.get('node', '')[:12]
  301. if version:
  302. f = open("tortoisehg/util/__version__.py", "w")
  303. f.write('# this file is autogenerated by setup.py\n')
  304. f.write('version = "%s"\n' % version)
  305. f.close()
  306. try:
  307. import tortoisehg.util.__version__
  308. version = tortoisehg.util.__version__.version
  309. except ImportError:
  310. version = 'unknown'
  311. if os.name == "nt":
  312. (scripts, packages, data_files, extra) = setup_windows(version)
  313. desc = 'Windows shell extension for Mercurial VCS'
  314. # Windows binary file versions for exe/dll files must have the
  315. # form W.X.Y.Z, where W,X,Y,Z are numbers in the range 0..65535
  316. from tortoisehg.util.version import package_version
  317. setupversion = package_version()
  318. productname = 'TortoiseHg'
  319. else:
  320. (scripts, packages, data_files, extra) = setup_posix()
  321. desc = 'TortoiseHg dialogs for Mercurial VCS'
  322. setupversion = version
  323. productname = 'tortoisehg'
  324. setup(name=productname,
  325. version=setupversion,
  326. author='Steve Borho',
  327. author_email='steve@borho.org',
  328. url='http://tortoisehg.org',
  329. description=desc,
  330. license='GNU GPL2',
  331. scripts=scripts,
  332. packages=packages,
  333. data_files=data_files,
  334. cmdclass=cmdclass,
  335. **extra
  336. )