disthelpers.py 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2009-2011 CEA
  4. # Pierre Raybaut
  5. # Licensed under the terms of the CECILL License
  6. # (see guidata/__init__.py for details)
  7. # pylint: disable=W0613
  8. """
  9. disthelpers
  10. -----------
  11. The ``guidata.disthelpers`` module provides helper functions for Python
  12. package distribution on Microsoft Windows platforms with ``py2exe`` or on
  13. all platforms thanks to ``cx_Freeze``.
  14. """
  15. from __future__ import print_function
  16. import sys
  17. import os
  18. import os.path as osp
  19. import shutil
  20. import traceback
  21. import atexit
  22. import imp
  23. from subprocess import Popen, PIPE
  24. import warnings
  25. from distutils.version import LooseVersion, StrictVersion
  26. # ==============================================================================
  27. # Module, scripts, programs
  28. # ==============================================================================
  29. def get_module_path(modname):
  30. """Return module *modname* base path"""
  31. module = sys.modules.get(modname, __import__(modname))
  32. return osp.abspath(osp.dirname(module.__file__))
  33. # ==============================================================================
  34. # Dependency management
  35. # ==============================================================================
  36. def get_changeset(path, rev=None):
  37. """Return Mercurial repository *path* revision number"""
  38. args = ['hg', 'parent']
  39. if rev is not None:
  40. args += ['--rev', str(rev)]
  41. process = Popen(
  42. args, stdout=PIPE, stderr=PIPE, cwd=path, shell=True
  43. )
  44. try:
  45. return (
  46. process.stdout.read().splitlines()[0].split()[1]
  47. )
  48. except IndexError:
  49. raise RuntimeError(process.stderr.read())
  50. def prepend_module_to_path(module_path):
  51. """
  52. Prepend to sys.path module located in *module_path*
  53. Return string with module infos: name, revision, changeset
  54. Use this function:
  55. 1) In your application to import local frozen copies of internal libraries
  56. 2) In your py2exe distributed package to add a text file containing the returned string
  57. """
  58. if not osp.isdir(module_path):
  59. # Assuming py2exe distribution
  60. return
  61. sys.path.insert(0, osp.abspath(module_path))
  62. changeset = get_changeset(module_path)
  63. name = osp.basename(module_path)
  64. prefix = "Prepending module to sys.path"
  65. message = prefix + (
  66. "%s [revision %s]" % (name, changeset)
  67. ).rjust(80 - len(prefix), ".")
  68. print(message, file=sys.stderr)
  69. if name in sys.modules:
  70. sys.modules.pop(name)
  71. nbsp = 0
  72. for modname in sys.modules.keys():
  73. if modname.startswith(name + '.'):
  74. sys.modules.pop(modname)
  75. nbsp += 1
  76. warning = '(removed %s from sys.modules' % name
  77. if nbsp:
  78. warning += ' and %d subpackages' % nbsp
  79. warning += ')'
  80. print(warning.rjust(80), file=sys.stderr)
  81. return message
  82. def prepend_modules_to_path(module_base_path):
  83. """Prepend to sys.path all modules located in *module_base_path*"""
  84. if not osp.isdir(module_base_path):
  85. # Assuming py2exe distribution
  86. return
  87. fnames = [
  88. osp.join(module_base_path, name)
  89. for name in os.listdir(module_base_path)
  90. ]
  91. messages = [
  92. prepend_module_to_path(dirname)
  93. for dirname in fnames
  94. if osp.isdir(dirname)
  95. ]
  96. return os.linesep.join(messages)
  97. # ==============================================================================
  98. # Distribution helpers
  99. # ==============================================================================
  100. def _remove_later(fname):
  101. """Try to remove file later (at exit)"""
  102. def try_to_remove(fname):
  103. if osp.exists(fname):
  104. os.remove(fname)
  105. atexit.register(try_to_remove, osp.abspath(fname))
  106. def get_msvc_version(python_version):
  107. """Return Microsoft Visual C++ version used to build this Python version"""
  108. if python_version is None:
  109. python_version = '2.7'
  110. warnings.warn("assuming Python 2.7 target")
  111. if python_version in (
  112. '2.6',
  113. '2.7',
  114. '3.0',
  115. '3.1',
  116. '3.2',
  117. ):
  118. # Python 2.6-2.7, 3.0-3.2 were built with Visual Studio 9.0.21022.8
  119. # (i.e. Visual C++ 2008, not Visual C++ 2008 SP1!)
  120. return "9.0.21022.8"
  121. elif python_version in ('3.3', '3.4'):
  122. # Python 3.3+ were built with Visual Studio 10.0.30319.1
  123. # (i.e. Visual C++ 2010)
  124. return '10.0'
  125. elif python_version in ('3.5', '3.6'):
  126. return '15.0'
  127. elif python_version in ('3.7', '3.8'):
  128. return '15.0'
  129. elif StrictVersion(python_version) >= StrictVersion('3.9'):
  130. return '15.0'
  131. else:
  132. raise RuntimeError(
  133. "Unsupported Python version %s" % python_version
  134. )
  135. def get_msvc_dlls(msvc_version, architecture=None):
  136. """Get the list of Microsoft Visual C++ DLLs associated to
  137. architecture and Python version, create the manifest file.
  138. architecture: integer (32 or 64) -- if None, take the Python build arch
  139. python_version: X.Y"""
  140. current_architecture = (
  141. 64 if sys.maxsize > 2 ** 32 else 32
  142. )
  143. if architecture is None:
  144. architecture = current_architecture
  145. filelist = []
  146. # simple vs2015 situation: nothing (system dll)
  147. if msvc_version == '14.0':
  148. return filelist
  149. msvc_major = msvc_version.split('.')[0]
  150. msvc_minor = msvc_version.split('.')[1]
  151. if msvc_major == '9':
  152. key = "1fc8b3b9a1e18e3b"
  153. atype = "" if architecture == 64 else "win32"
  154. arch = "amd64" if architecture == 64 else "x86"
  155. groups = {
  156. 'CRT': (
  157. 'msvcr90.dll',
  158. 'msvcp90.dll',
  159. 'msvcm90.dll',
  160. ),
  161. # 'OPENMP': ('vcomp90.dll',)
  162. }
  163. for group, dll_list in groups.items():
  164. dlls = ''
  165. for dll in dll_list:
  166. dlls += ' <file name="%s" />%s' % (
  167. dll,
  168. os.linesep,
  169. )
  170. manifest = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  171. <!-- Copyright (c) Microsoft Corporation. All rights reserved. -->
  172. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  173. <noInheritable/>
  174. <assemblyIdentity
  175. type="%(atype)s"
  176. name="Microsoft.VC90.%(group)s"
  177. version="%(version)s"
  178. processorArchitecture="%(arch)s"
  179. publicKeyToken="%(key)s"
  180. />
  181. %(dlls)s</assembly>
  182. """ % dict(
  183. version=msvc_version,
  184. key=key,
  185. atype=atype,
  186. arch=arch,
  187. group=group,
  188. dlls=dlls,
  189. )
  190. vc90man = "Microsoft.VC90.%s.manifest" % group
  191. open(vc90man, 'w').write(manifest)
  192. _remove_later(vc90man)
  193. filelist += [vc90man]
  194. winsxs = osp.join(
  195. os.environ['windir'], 'WinSxS'
  196. )
  197. vcstr = '%s_Microsoft.VC90.%s_%s_%s' % (
  198. arch,
  199. group,
  200. key,
  201. msvc_version,
  202. )
  203. for fname in os.listdir(winsxs):
  204. path = osp.join(winsxs, fname)
  205. if osp.isdir(
  206. path
  207. ) and fname.lower().startswith(
  208. vcstr.lower()
  209. ):
  210. for dllname in os.listdir(path):
  211. filelist.append(
  212. osp.join(path, dllname)
  213. )
  214. break
  215. else:
  216. raise RuntimeError(
  217. "Microsoft Visual C++ %s DLLs version %s "
  218. "were not found" % (group, msvc_version)
  219. )
  220. elif (
  221. msvc_major == '10' or msvc_major == '15'
  222. ): # 15 for vs 2015
  223. namelist = [
  224. name % (msvc_major + msvc_minor)
  225. for name in (
  226. 'msvcp%s.dll',
  227. 'msvcr%s.dll',
  228. 'vcomp%s.dll',
  229. )
  230. ]
  231. if msvc_major == '15' and architecture == 64:
  232. namelist = [
  233. name % ('14' + msvc_minor)
  234. for name in (
  235. 'vcruntime%s.dll',
  236. 'vcruntime%s_1.dll',
  237. 'msvcp%s.dll',
  238. 'vccorlib%s.dll',
  239. 'concrt%s.dll',
  240. 'vcomp%s.dll',
  241. )
  242. ]
  243. if msvc_major == '15' and architecture != 64:
  244. namelist = [
  245. name % ('14' + msvc_minor)
  246. for name in (
  247. 'vcruntime%s.dll',
  248. 'msvcp%s.dll',
  249. 'vccorlib%s.dll',
  250. 'concrt%s.dll',
  251. 'vcomp%s.dll',
  252. )
  253. ]
  254. windir = os.environ['windir']
  255. is_64bit_windows = osp.isdir(
  256. osp.join(windir, "SysWOW64")
  257. )
  258. # Reminder: WoW64 (*W*indows 32-bit *o*n *W*indows *64*-bit) is a
  259. # subsystem of the Windows operating system capable of running 32-bit
  260. # applications and is included on all 64-bit versions of Windows
  261. # (source: http://en.wikipedia.org/wiki/WoW64)
  262. #
  263. # In other words, "SysWOW64" contains 64-bit DLL and applications,
  264. # whereas "System32" contains 64-bit DLL and applications on a 64-bit
  265. # system.
  266. sysdir = "System32"
  267. if not is_64bit_windows and architecture == 64:
  268. raise RuntimeError(
  269. "Can't find 64-bit MSVC DLLs on a 32-bit OS"
  270. )
  271. if is_64bit_windows and architecture == 32:
  272. sysdir = "SysWOW64"
  273. for dllname in namelist:
  274. fname = osp.join(windir, sysdir, dllname)
  275. print('searching', fname)
  276. if osp.exists(fname):
  277. filelist.append(fname)
  278. else:
  279. raise RuntimeError(
  280. "Microsoft Visual C++ DLLs version %s "
  281. "were not found" % msvc_version
  282. )
  283. else:
  284. raise RuntimeError(
  285. "Unsupported MSVC version %s" % msvc_version
  286. )
  287. return filelist
  288. def create_msvc_data_files(
  289. architecture=None, python_version=None, verbose=False
  290. ):
  291. """Including Microsoft Visual C++ DLLs"""
  292. msvc_version = get_msvc_version(python_version)
  293. filelist = get_msvc_dlls(
  294. msvc_version, architecture=architecture
  295. )
  296. print(create_msvc_data_files.__doc__)
  297. if verbose:
  298. for name in filelist:
  299. print(" ", name)
  300. msvc_major = msvc_version.split('.')[0]
  301. if msvc_major == '9':
  302. return [("Microsoft.VC90.CRT", filelist)]
  303. else:
  304. return [("", filelist)]
  305. def to_include_files(data_files):
  306. """Convert data_files list to include_files list
  307. data_files:
  308. * this is the ``py2exe`` data files format
  309. * list of tuples (dest_dirname, (src_fname1, src_fname2, ...))
  310. include_files:
  311. * this is the ``cx_Freeze`` data files format
  312. * list of tuples ((src_fname1, dst_fname1),
  313. (src_fname2, dst_fname2), ...))
  314. """
  315. include_files = []
  316. for dest_dir, fnames in data_files:
  317. for source_fname in fnames:
  318. dest_fname = osp.join(
  319. dest_dir, osp.basename(source_fname)
  320. )
  321. include_files.append((source_fname, dest_fname))
  322. return include_files
  323. def strip_version(version):
  324. """Return version number with digits only
  325. (Windows does not support strings in version numbers)"""
  326. return (
  327. version.split('beta')[0]
  328. .split('alpha')[0]
  329. .split('rc')[0]
  330. .split('dev')[0]
  331. )
  332. def remove_dir(dirname):
  333. """Remove directory *dirname* and all its contents
  334. Print details about the operation (progress, success/failure)"""
  335. print("Removing directory '%s'..." % dirname, end=' ')
  336. try:
  337. shutil.rmtree(dirname, ignore_errors=True)
  338. print("OK")
  339. except Exception:
  340. print("Failed!")
  341. traceback.print_exc()
  342. class Distribution(object):
  343. """Distribution object
  344. Help creating an executable using ``py2exe`` or ``cx_Freeze``
  345. """
  346. DEFAULT_EXCLUDES = [
  347. 'Tkconstants',
  348. 'Tkinter',
  349. 'tcl',
  350. 'tk',
  351. 'wx',
  352. '_imagingtk',
  353. 'curses',
  354. 'PIL._imagingtk',
  355. 'ImageTk',
  356. 'PIL.ImageTk',
  357. 'FixTk',
  358. 'bsddb',
  359. 'email',
  360. 'pywin.debugger',
  361. 'pywin.debugger.dbgcon',
  362. 'matplotlib',
  363. ]
  364. DEFAULT_INCLUDES = []
  365. DEFAULT_BIN_EXCLUDES = [
  366. 'MSVCP100.dll',
  367. 'MSVCP90.dll',
  368. 'w9xpopen.exe',
  369. 'MSVCP80.dll',
  370. 'MSVCR80.dll',
  371. ]
  372. DEFAULT_BIN_INCLUDES = []
  373. DEFAULT_BIN_PATH_INCLUDES = []
  374. DEFAULT_BIN_PATH_EXCLUDES = []
  375. def __init__(self):
  376. self.name = None
  377. self.version = None
  378. self.description = None
  379. self.target_name = None
  380. self._target_dir = None
  381. self.icon = None
  382. self.data_files = []
  383. self.includes = self.DEFAULT_INCLUDES
  384. self.excludes = self.DEFAULT_EXCLUDES
  385. self.bin_includes = self.DEFAULT_BIN_INCLUDES
  386. self.bin_excludes = self.DEFAULT_BIN_EXCLUDES
  387. self.bin_path_includes = (
  388. self.DEFAULT_BIN_PATH_INCLUDES
  389. )
  390. self.bin_path_excludes = (
  391. self.DEFAULT_BIN_PATH_EXCLUDES
  392. )
  393. self.msvc = os.name == 'nt'
  394. self._py2exe_is_loaded = False
  395. self._pyqt4_added = False
  396. self._pyside_added = False
  397. # Attributes relative to cx_Freeze:
  398. self.executables = []
  399. @property
  400. def target_dir(self):
  401. """Return target directory (default: 'dist')"""
  402. dirname = self._target_dir
  403. if dirname is None:
  404. return 'dist'
  405. else:
  406. return dirname
  407. @target_dir.setter # analysis:ignore
  408. def target_dir(self, value):
  409. self._target_dir = value
  410. def setup(
  411. self,
  412. name,
  413. version,
  414. description,
  415. script,
  416. target_name=None,
  417. target_dir=None,
  418. icon=None,
  419. data_files=None,
  420. includes=None,
  421. excludes=None,
  422. bin_includes=None,
  423. bin_excludes=None,
  424. bin_path_includes=None,
  425. bin_path_excludes=None,
  426. msvc=None,
  427. ):
  428. """Setup distribution object
  429. Notes:
  430. * bin_path_excludes is specific to cx_Freeze (ignored if it's None)
  431. * if msvc is None, it's set to True by default on Windows
  432. platforms, False on non-Windows platforms
  433. """
  434. self.name = name
  435. self.version = (
  436. strip_version(version)
  437. if os.name == 'nt'
  438. else version
  439. )
  440. self.description = description
  441. assert osp.isfile(script)
  442. self.script = script
  443. self.target_name = target_name
  444. self.target_dir = target_dir
  445. self.icon = icon
  446. if data_files is not None:
  447. self.data_files += data_files
  448. if includes is not None:
  449. self.includes += includes
  450. if excludes is not None:
  451. self.excludes += excludes
  452. if bin_includes is not None:
  453. self.bin_includes += bin_includes
  454. if bin_excludes is not None:
  455. self.bin_excludes += bin_excludes
  456. if bin_path_includes is not None:
  457. self.bin_path_includes += bin_path_includes
  458. if bin_path_excludes is not None:
  459. self.bin_path_excludes += bin_path_excludes
  460. if msvc is not None:
  461. self.msvc = msvc
  462. if self.msvc:
  463. try:
  464. self.data_files += create_msvc_data_files()
  465. except IOError:
  466. print(
  467. "Setting the msvc option to False "
  468. "will avoid this error",
  469. file=sys.stderr,
  470. )
  471. raise
  472. # cx_Freeze:
  473. self.add_executable(
  474. self.script, self.target_name, icon=self.icon
  475. )
  476. def add_text_data_file(self, filename, contents):
  477. """Create temporary data file *filename* with *contents*
  478. and add it to *data_files*"""
  479. open(filename, 'wb').write(contents)
  480. self.data_files += [("", (filename,))]
  481. _remove_later(filename)
  482. def add_data_file(self, filename, destdir=''):
  483. self.data_files += [(destdir, (filename,))]
  484. # ------ Adding packages
  485. def add_pyqt4(self):
  486. """Include module PyQt4 to the distribution"""
  487. if self._pyqt4_added:
  488. return
  489. self._pyqt4_added = True
  490. self.includes += [
  491. 'sip',
  492. 'PyQt4.Qt',
  493. 'PyQt4.QtSvg',
  494. 'PyQt4.QtNetwork',
  495. ]
  496. import PyQt4
  497. pyqt_path = osp.dirname(PyQt4.__file__)
  498. # Configuring PyQt4
  499. conf = os.linesep.join(
  500. ["[Paths]", "Prefix = .", "Binaries = ."]
  501. )
  502. self.add_text_data_file('qt.conf', conf)
  503. # Including plugins (.svg icons support, QtDesigner support, ...)
  504. if self.msvc:
  505. vc90man = "Microsoft.VC90.CRT.manifest"
  506. pyqt_tmp = 'pyqt_tmp'
  507. if osp.isdir(pyqt_tmp):
  508. shutil.rmtree(pyqt_tmp)
  509. os.mkdir(pyqt_tmp)
  510. vc90man_pyqt = osp.join(pyqt_tmp, vc90man)
  511. man = (
  512. open(vc90man, "r")
  513. .read()
  514. .replace(
  515. '<file name="',
  516. '<file name="Microsoft.VC90.CRT\\',
  517. )
  518. )
  519. open(vc90man_pyqt, 'w').write(man)
  520. for dirpath, _, filenames in os.walk(
  521. osp.join(pyqt_path, "plugins")
  522. ):
  523. filelist = [
  524. osp.join(dirpath, f)
  525. for f in filenames
  526. if osp.splitext(f)[1] in ('.dll', '.py')
  527. ]
  528. if self.msvc and [
  529. f
  530. for f in filelist
  531. if osp.splitext(f)[1] == '.dll'
  532. ]:
  533. # Where there is a DLL build with Microsoft Visual C++ 2008,
  534. # there must be a manifest file as well...
  535. # ...congrats to Microsoft for this great simplification!
  536. filelist.append(vc90man_pyqt)
  537. self.data_files.append(
  538. (
  539. dirpath[
  540. len(pyqt_path) + len(os.pathsep) :
  541. ],
  542. filelist,
  543. )
  544. )
  545. if self.msvc:
  546. atexit.register(remove_dir, pyqt_tmp)
  547. # Including french translation
  548. fr_trans = osp.join(
  549. pyqt_path, "translations", "qt_fr.qm"
  550. )
  551. if osp.exists(fr_trans):
  552. self.data_files.append(
  553. ('translations', (fr_trans,))
  554. )
  555. def add_pyside(self):
  556. """Include module PySide to the distribution"""
  557. if self._pyside_added:
  558. return
  559. self._pyside_added = True
  560. self.includes += [
  561. 'PySide.QtDeclarative',
  562. 'PySide.QtHelp',
  563. 'PySide.QtMultimedia',
  564. 'PySide.QtNetwork',
  565. 'PySide.QtOpenGL',
  566. 'PySide.QtScript',
  567. 'PySide.QtScriptTools',
  568. 'PySide.QtSql',
  569. 'PySide.QtSvg',
  570. 'PySide.QtTest',
  571. 'PySide.QtUiTools',
  572. 'PySide.QtWebKit',
  573. 'PySide.QtXml',
  574. 'PySide.QtXmlPatterns',
  575. ]
  576. import PySide
  577. pyside_path = osp.dirname(PySide.__file__)
  578. # Configuring PySide
  579. conf = os.linesep.join(
  580. ["[Paths]", "Prefix = .", "Binaries = ."]
  581. )
  582. self.add_text_data_file('qt.conf', conf)
  583. # Including plugins (.svg icons support, QtDesigner support, ...)
  584. if self.msvc:
  585. vc90man = "Microsoft.VC90.CRT.manifest"
  586. os.mkdir('pyside_tmp')
  587. vc90man_pyside = osp.join('pyside_tmp', vc90man)
  588. man = (
  589. open(vc90man, "r")
  590. .read()
  591. .replace(
  592. '<file name="',
  593. '<file name="Microsoft.VC90.CRT\\',
  594. )
  595. )
  596. open(vc90man_pyside, 'w').write(man)
  597. for dirpath, _, filenames in os.walk(
  598. osp.join(pyside_path, "plugins")
  599. ):
  600. filelist = [
  601. osp.join(dirpath, f)
  602. for f in filenames
  603. if osp.splitext(f)[1] in ('.dll', '.py')
  604. ]
  605. if self.msvc and [
  606. f
  607. for f in filelist
  608. if osp.splitext(f)[1] == '.dll'
  609. ]:
  610. # Where there is a DLL build with Microsoft Visual C++ 2008,
  611. # there must be a manifest file as well...
  612. # ...congrats to Microsoft for this great simplification!
  613. filelist.append(vc90man_pyside)
  614. self.data_files.append(
  615. (
  616. dirpath[
  617. len(pyside_path) + len(os.pathsep) :
  618. ],
  619. filelist,
  620. )
  621. )
  622. # Replacing dlls found by cx_Freeze by the real PySide Qt dlls:
  623. # (http://qt-project.org/wiki/Packaging_PySide_applications_on_Windows)
  624. dlls = [
  625. osp.join(pyside_path, fname)
  626. for fname in os.listdir(pyside_path)
  627. if osp.splitext(fname)[1] == '.dll'
  628. ]
  629. self.data_files.append(('', dlls))
  630. if self.msvc:
  631. atexit.register(remove_dir, 'pyside_tmp')
  632. # Including french translation
  633. fr_trans = osp.join(
  634. pyside_path, "translations", "qt_fr.qm"
  635. )
  636. if osp.exists(fr_trans):
  637. self.data_files.append(
  638. ('translations', (fr_trans,))
  639. )
  640. def add_qt_bindings(self):
  641. """Include Qt bindings, i.e. PyQt4 or PySide"""
  642. try:
  643. imp.find_module('PyQt4')
  644. self.add_modules('PyQt4')
  645. except ImportError:
  646. self.add_modules('PySide')
  647. def add_matplotlib(self):
  648. """Include module Matplotlib to the distribution"""
  649. if 'matplotlib' in self.excludes:
  650. self.excludes.pop(
  651. self.excludes.index('matplotlib')
  652. )
  653. try:
  654. import matplotlib.numerix # analysis:ignore
  655. self.includes += [
  656. 'matplotlib.numerix.ma',
  657. 'matplotlib.numerix.fft',
  658. 'matplotlib.numerix.linear_algebra',
  659. 'matplotlib.numerix.mlab',
  660. 'matplotlib.numerix.random_array',
  661. ]
  662. except ImportError:
  663. pass
  664. self.add_module_data_files(
  665. 'matplotlib',
  666. ('mpl-data',),
  667. (
  668. '.conf',
  669. '.glade',
  670. '',
  671. '.png',
  672. '.svg',
  673. '.xpm',
  674. '.ppm',
  675. '.npy',
  676. '.afm',
  677. '.ttf',
  678. ),
  679. )
  680. def add_modules(self, *module_names):
  681. """Include module *module_name*"""
  682. for module_name in module_names:
  683. print("Configuring module '%s'" % module_name)
  684. if module_name == 'PyQt4':
  685. self.add_pyqt4()
  686. elif module_name == 'PySide':
  687. self.add_pyside()
  688. elif module_name == 'scipy.io':
  689. self.includes += ['scipy.io.matlab.streams']
  690. elif module_name == 'matplotlib':
  691. self.add_matplotlib()
  692. elif module_name == 'h5py':
  693. import h5py
  694. for attr in [
  695. '_stub',
  696. '_sync',
  697. 'utils',
  698. '_conv',
  699. '_proxy',
  700. 'defs',
  701. ]:
  702. if hasattr(h5py, attr):
  703. self.includes.append(
  704. 'h5py.%s' % attr
  705. )
  706. if (
  707. self.bin_path_excludes is not None
  708. and os.name == 'nt'
  709. ):
  710. # Specific to cx_Freeze on Windows: avoid including a zlib dll
  711. # built with another version of Microsoft Visual Studio
  712. self.bin_path_excludes += [
  713. r'C:\Program Files',
  714. r'C:\Program Files (x86)',
  715. ]
  716. self.data_files.append( # necessary for cx_Freeze only
  717. (
  718. '',
  719. (
  720. osp.join(
  721. get_module_path('h5py'),
  722. 'zlib1.dll',
  723. ),
  724. ),
  725. )
  726. )
  727. elif module_name in (
  728. 'docutils',
  729. 'rst2pdf',
  730. 'sphinx',
  731. ):
  732. self.includes += [
  733. 'docutils.writers.null',
  734. 'docutils.languages.en',
  735. 'docutils.languages.fr',
  736. ]
  737. if module_name == 'rst2pdf':
  738. self.add_module_data_files(
  739. "rst2pdf",
  740. ("styles",),
  741. ('.json', '.style'),
  742. copy_to_root=True,
  743. )
  744. if module_name == 'sphinx':
  745. import sphinx.ext
  746. for fname in os.listdir(
  747. osp.dirname(sphinx.ext.__file__)
  748. ):
  749. if osp.splitext(fname)[1] == '.py':
  750. modname = (
  751. 'sphinx.ext.%s'
  752. % osp.splitext(fname)[0]
  753. )
  754. self.includes.append(modname)
  755. elif module_name == 'pygments':
  756. self.includes += [
  757. 'pygments',
  758. 'pygments.formatters',
  759. 'pygments.lexers',
  760. 'pygments.lexers.agile',
  761. ]
  762. elif module_name == 'zmq':
  763. # FIXME: this is not working, yet... (missing DLL)
  764. self.includes += [
  765. 'zmq',
  766. 'zmq.core._poll',
  767. 'zmq.core._version',
  768. 'zmq.core.constants',
  769. 'zmq.core.context',
  770. 'zmq.core.device',
  771. 'zmq.core.error',
  772. 'zmq.core.message',
  773. 'zmq.core.socket',
  774. 'zmq.core.stopwatch',
  775. ]
  776. if os.name == 'nt':
  777. self.bin_includes += ['libzmq.dll']
  778. elif module_name == 'guidata':
  779. self.add_module_data_files(
  780. 'guidata',
  781. ("images",),
  782. ('.png', '.svg'),
  783. copy_to_root=False,
  784. )
  785. try:
  786. imp.find_module('PyQt4')
  787. self.add_pyqt4()
  788. except ImportError:
  789. self.add_pyside()
  790. elif module_name == 'guiqwt':
  791. self.add_module_data_files(
  792. 'guiqwt',
  793. ("images",),
  794. ('.png', '.svg'),
  795. copy_to_root=False,
  796. )
  797. if os.name == 'nt':
  798. # Specific to cx_Freeze: including manually MinGW DLLs
  799. self.bin_includes += [
  800. 'libgcc_s_dw2-1.dll',
  801. 'libstdc++-6.dll',
  802. ]
  803. else:
  804. try:
  805. # Modules based on the same scheme as guidata and guiqwt
  806. self.add_module_data_files(
  807. module_name,
  808. ("images",),
  809. ('.png', '.svg'),
  810. copy_to_root=False,
  811. )
  812. except IOError:
  813. raise RuntimeError(
  814. "Module not supported: %s"
  815. % module_name
  816. )
  817. def add_module_data_dir(
  818. self,
  819. module_name,
  820. data_dir_name,
  821. extensions,
  822. copy_to_root=True,
  823. verbose=False,
  824. exclude_dirs=[],
  825. ):
  826. """
  827. Collect data files in *data_dir_name* for module *module_name*
  828. and add them to *data_files*
  829. *extensions*: list of file extensions, e.g. ('.png', '.svg')
  830. """
  831. module_dir = get_module_path(module_name)
  832. nstrip = len(module_dir) + len(osp.sep)
  833. data_dir = osp.join(module_dir, data_dir_name)
  834. if not osp.isdir(data_dir):
  835. raise IOError(
  836. "Directory not found: %s" % data_dir
  837. )
  838. for dirpath, _dirnames, filenames in os.walk(
  839. data_dir
  840. ):
  841. dirname = dirpath[nstrip:]
  842. if osp.basename(dirpath) in exclude_dirs:
  843. continue
  844. if not copy_to_root:
  845. dirname = osp.join(module_name, dirname)
  846. pathlist = [
  847. osp.join(dirpath, f)
  848. for f in filenames
  849. if osp.splitext(f)[1].lower() in extensions
  850. ]
  851. self.data_files.append((dirname, pathlist))
  852. if verbose:
  853. for name in pathlist:
  854. print(" ", name)
  855. def add_module_data_files(
  856. self,
  857. module_name,
  858. data_dir_names,
  859. extensions,
  860. copy_to_root=True,
  861. verbose=False,
  862. exclude_dirs=[],
  863. ):
  864. """
  865. Collect data files for module *module_name* and add them to *data_files*
  866. *data_dir_names*: list of dirnames, e.g. ('images', )
  867. *extensions*: list of file extensions, e.g. ('.png', '.svg')
  868. """
  869. print(
  870. "Adding module '%s' data files in %s (%s)"
  871. % (
  872. module_name,
  873. ", ".join(data_dir_names),
  874. ", ".join(extensions),
  875. )
  876. )
  877. module_dir = get_module_path(module_name)
  878. for data_dir_name in data_dir_names:
  879. self.add_module_data_dir(
  880. module_name,
  881. data_dir_name,
  882. extensions,
  883. copy_to_root,
  884. verbose,
  885. exclude_dirs,
  886. )
  887. translation_file = osp.join(
  888. module_dir,
  889. "locale",
  890. "fr",
  891. "LC_MESSAGES",
  892. "%s.mo" % module_name,
  893. )
  894. if osp.isfile(translation_file):
  895. self.data_files.append(
  896. (
  897. osp.join(
  898. module_name,
  899. "locale",
  900. "fr",
  901. "LC_MESSAGES",
  902. ),
  903. (translation_file,),
  904. )
  905. )
  906. print(
  907. "Adding module '%s' translation file: %s"
  908. % (
  909. module_name,
  910. osp.basename(translation_file),
  911. )
  912. )
  913. def build(
  914. self, library, cleanup=True, create_archive=None
  915. ):
  916. """Build executable with given library.
  917. library:
  918. * 'py2exe': deploy using the `py2exe` library
  919. * 'cx_Freeze': deploy using the `cx_Freeze` library
  920. cleanup: remove 'build/dist' directories before building distribution
  921. create_archive (requires the executable `zip`):
  922. * None or False: do nothing
  923. * 'add': add target directory to a ZIP archive
  924. * 'move': move target directory to a ZIP archive
  925. """
  926. if library == 'py2exe':
  927. self.build_py2exe(
  928. cleanup=cleanup,
  929. create_archive=create_archive,
  930. )
  931. elif library == 'cx_Freeze':
  932. self.build_cx_freeze(
  933. cleanup=cleanup,
  934. create_archive=create_archive,
  935. )
  936. else:
  937. raise RuntimeError(
  938. "Unsupported library %s" % library
  939. )
  940. def __cleanup(self):
  941. """Remove old build and dist directories"""
  942. remove_dir("build")
  943. if osp.isdir("dist"):
  944. remove_dir("dist")
  945. remove_dir(self.target_dir)
  946. def __create_archive(self, option):
  947. """Create a ZIP archive
  948. option:
  949. * 'add': add target directory to a ZIP archive
  950. * 'move': move target directory to a ZIP archive
  951. """
  952. name = self.target_dir
  953. os.system('zip "%s.zip" -r "%s"' % (name, name))
  954. if option == 'move':
  955. shutil.rmtree(name)
  956. def build_py2exe(
  957. self,
  958. cleanup=True,
  959. compressed=2,
  960. optimize=2,
  961. company_name=None,
  962. copyright=None,
  963. create_archive=None,
  964. ):
  965. """Build executable with py2exe
  966. cleanup: remove 'build/dist' directories before building distribution
  967. create_archive (requires the executable `zip`):
  968. * None or False: do nothing
  969. * 'add': add target directory to a ZIP archive
  970. * 'move': move target directory to a ZIP archive
  971. """
  972. from distutils.core import setup
  973. import py2exe # Patching distutils -- analysis:ignore
  974. self._py2exe_is_loaded = True
  975. if cleanup:
  976. self.__cleanup()
  977. sys.argv += ["py2exe"]
  978. options = dict(
  979. compressed=compressed,
  980. optimize=optimize,
  981. includes=self.includes,
  982. excludes=self.excludes,
  983. dll_excludes=self.bin_excludes,
  984. dist_dir=self.target_dir,
  985. )
  986. windows = dict(
  987. name=self.name,
  988. description=self.description,
  989. script=self.script,
  990. icon_resources=[(0, self.icon)],
  991. bitmap_resources=[],
  992. other_resources=[],
  993. dest_base=osp.splitext(self.target_name)[0],
  994. version=self.version,
  995. company_name=company_name,
  996. copyright=copyright,
  997. )
  998. setup(
  999. data_files=self.data_files,
  1000. windows=[windows],
  1001. options=dict(py2exe=options),
  1002. )
  1003. if create_archive:
  1004. self.__create_archive(create_archive)
  1005. def add_executable(
  1006. self, script, target_name, icon=None
  1007. ):
  1008. """Add executable to the cx_Freeze distribution
  1009. Not supported for py2exe"""
  1010. from cx_Freeze import Executable
  1011. base = None
  1012. if script.endswith('.pyw') and os.name == 'nt':
  1013. base = 'win32gui'
  1014. self.executables += [
  1015. Executable(
  1016. self.script,
  1017. base=base,
  1018. icon=self.icon,
  1019. targetName=self.target_name,
  1020. )
  1021. ]
  1022. def build_cx_freeze(
  1023. self, cleanup=True, create_archive=None
  1024. ):
  1025. """Build executable with cx_Freeze
  1026. cleanup: remove 'build/dist' directories before building distribution
  1027. create_archive (requires the executable `zip`):
  1028. * None or False: do nothing
  1029. * 'add': add target directory to a ZIP archive
  1030. * 'move': move target directory to a ZIP archive
  1031. """
  1032. assert (
  1033. not self._py2exe_is_loaded
  1034. ), "cx_Freeze can't be executed after py2exe"
  1035. from cx_Freeze import setup
  1036. if cleanup:
  1037. self.__cleanup()
  1038. sys.argv += ["build"]
  1039. build_exe = dict(
  1040. include_files=to_include_files(self.data_files),
  1041. includes=self.includes,
  1042. excludes=self.excludes,
  1043. bin_excludes=self.bin_excludes,
  1044. bin_includes=self.bin_includes,
  1045. bin_path_includes=self.bin_path_includes,
  1046. bin_path_excludes=self.bin_path_excludes,
  1047. build_exe=self.target_dir,
  1048. )
  1049. setup(
  1050. name=self.name,
  1051. version=self.version,
  1052. description=self.description,
  1053. executables=self.executables,
  1054. options=dict(build_exe=build_exe),
  1055. )
  1056. if create_archive:
  1057. self.__create_archive(create_archive)