/tools/win32build/prepare_bootstrap.py

https://github.com/wizzk42/numpy · Python · 109 lines · 85 code · 18 blank · 6 comment · 17 complexity · 5972f67a2321baf73ad4684900d3a4bb MD5 · raw file

  1. import os
  2. import subprocess
  3. import shutil
  4. from os.path import join as pjoin, split as psplit, dirname
  5. from zipfile import ZipFile
  6. import re
  7. def get_sdist_tarball():
  8. """Return the name of the installer built by wininst command."""
  9. # Yeah, the name logic is harcoded in distutils. We have to reproduce it
  10. # here
  11. name = "numpy-%s.zip" % get_numpy_version()
  12. return name
  13. def build_sdist():
  14. cwd = os.getcwd()
  15. try:
  16. os.chdir('../..')
  17. cmd = ["python", "setup.py", "sdist", "--format=zip"]
  18. subprocess.call(cmd)
  19. except Exception, e:
  20. raise RuntimeError("Error while executing cmd (%s)" % e)
  21. finally:
  22. os.chdir(cwd)
  23. def prepare_numpy_sources(bootstrap = 'bootstrap'):
  24. zid = ZipFile(pjoin('..', '..', 'dist', get_sdist_tarball()))
  25. root = 'numpy-%s' % get_numpy_version()
  26. # From the sdist-built tarball, extract all files into bootstrap directory,
  27. # but removing the numpy-VERSION head path
  28. for name in zid.namelist():
  29. cnt = zid.read(name)
  30. if name.startswith(root):
  31. # XXX: even on windows, the path sep in zip is '/' ?
  32. name = name.split('/', 1)[1]
  33. newname = pjoin(bootstrap, name)
  34. if not os.path.exists(dirname(newname)):
  35. os.makedirs(dirname(newname))
  36. fid = open(newname, 'wb')
  37. fid.write(cnt)
  38. def prepare_nsis_script(bootstrap, pyver, numver):
  39. tpl = os.path.join('nsis_scripts', 'numpy-superinstaller.nsi.in')
  40. source = open(tpl, 'r')
  41. target = open(pjoin(bootstrap, 'numpy-superinstaller.nsi'), 'w')
  42. installer_name = 'numpy-%s-win32-superpack-python%s.exe' % (numver, pyver)
  43. cnt = "".join(source.readlines())
  44. cnt = cnt.replace('@NUMPY_INSTALLER_NAME@', installer_name)
  45. for arch in ['nosse', 'sse2', 'sse3']:
  46. cnt = cnt.replace('@%s_BINARY@' % arch.upper(),
  47. get_binary_name(arch))
  48. target.write(cnt)
  49. def prepare_bootstrap(pyver):
  50. bootstrap = "bootstrap-%s" % pyver
  51. if os.path.exists(bootstrap):
  52. shutil.rmtree(bootstrap)
  53. os.makedirs(bootstrap)
  54. build_sdist()
  55. prepare_numpy_sources(bootstrap)
  56. shutil.copy('build.py', bootstrap)
  57. prepare_nsis_script(bootstrap, pyver, get_numpy_version())
  58. def get_binary_name(arch):
  59. return "numpy-%s-%s.exe" % (get_numpy_version(), arch)
  60. def get_numpy_version(chdir = pjoin('..', '..')):
  61. cwd = os.getcwd()
  62. try:
  63. if not chdir:
  64. chdir = cwd
  65. os.chdir(chdir)
  66. version = subprocess.Popen(['python', '-c', 'import __builtin__; __builtin__.__NUMPY_SETUP__ = True; from numpy.version import version;print version'], stdout = subprocess.PIPE).communicate()[0]
  67. version = version.strip()
  68. if 'dev' in version:
  69. out = subprocess.Popen(['svn', 'info'], stdout = subprocess.PIPE).communicate()[0]
  70. r = re.compile('Revision: ([0-9]+)')
  71. svnver = None
  72. for line in out.split('\n'):
  73. m = r.match(line)
  74. if m:
  75. svnver = m.group(1)
  76. if not svnver:
  77. raise ValueError("Error while parsing svn version ?")
  78. version += svnver
  79. finally:
  80. os.chdir(cwd)
  81. return version
  82. if __name__ == '__main__':
  83. from optparse import OptionParser
  84. parser = OptionParser()
  85. parser.add_option("-p", "--pyver", dest="pyver",
  86. help = "Python version (2.4, 2.5, etc...)")
  87. opts, args = parser.parse_args()
  88. pyver = opts.pyver
  89. if not pyver:
  90. pyver = "2.5"
  91. prepare_bootstrap(pyver)