__init__.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. import shutil
  3. from subprocess import check_output, call
  4. from urllib.request import urlretrieve
  5. from .copymodules import copy_modules
  6. pjoin = os.path.join
  7. _PKGDIR = os.path.dirname(__file__)
  8. DEFAULT_PY_VERSION = '3.3.2'
  9. DEFAULT_BUILD_DIR = pjoin('build', 'nsis')
  10. DEFAULT_ICON = pjoin(_PKGDIR, 'glossyorb.ico')
  11. DEFAULT_INSTALLER_NAME = 'pynsis_installer.exe'
  12. def fetch_python(version=DEFAULT_PY_VERSION, destination=DEFAULT_BUILD_DIR):
  13. """Fetch the MSI for the specified version of Python.
  14. It will be placed in the destination directory, and validated using GPG
  15. if possible.
  16. """
  17. url = 'http://python.org/ftp/python/{0}/python-{0}.msi'.format(version)
  18. target = pjoin(destination, 'python-{0}.msi'.format(version))
  19. if os.path.isfile(target):
  20. return
  21. urlretrieve(url, target)
  22. urlretrieve(url+'.asc', target+'.asc')
  23. try:
  24. keys_file = os.path.join(_PKGDIR, 'python-pubkeys.txt')
  25. check_output(['gpg', '--import', keys_file])
  26. check_output(['gpg', '--verify', target+'.asc'])
  27. except FileNotFoundError:
  28. print("GPG not available - could not check signature of {0}".format(target))
  29. def write_nsis_file(nsi_file, definitions):
  30. with open(nsi_file, 'w') as f:
  31. for name, value in definitions.items():
  32. f.write('!define {} "{}"\n'.format(name, value))
  33. with open(pjoin(_PKGDIR, 'template.nsi')) as f2:
  34. f.write(f2.read())
  35. def run_nsis(nsi_file):
  36. call(['makensis', nsi_file])
  37. def all_steps(appname, version, script, packages=None, icon=DEFAULT_ICON,
  38. py_version=DEFAULT_PY_VERSION, build_dir=DEFAULT_BUILD_DIR,
  39. installer_name=DEFAULT_INSTALLER_NAME):
  40. os.makedirs(build_dir, exist_ok=True)
  41. fetch_python(destination=build_dir)
  42. shutil.copy2(script, build_dir)
  43. shutil.copy2(icon, build_dir)
  44. build_pkg_dir = pjoin(build_dir, 'pkgs')
  45. if os.path.isdir('pynsis_pkgs'):
  46. shutil.copytree('pynsis_pkgs', build_pkg_dir)
  47. else:
  48. os.mkdir(build_pkg_dir)
  49. copy_modules(packages or [], build_pkg_dir)
  50. nsi_file = pjoin(build_dir, 'installer.nsi')
  51. definitions = {'PRODUCT_NAME': appname,
  52. 'PRODUCT_VERSION': version,
  53. 'PY_VERSION': py_version,
  54. 'SCRIPT': os.path.basename(script),
  55. 'PRODUCT_ICON': os.path.basename(icon),
  56. 'INSTALLER_NAME': installer_name,
  57. }
  58. write_nsis_file(nsi_file, definitions)
  59. run_nsis(nsi_file)