__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if os.name == 'nt' and sys.maxsize == (2**63)-1:
  13. DEFAULT_BITNESS = 64
  14. else:
  15. DEFAULT_BITNESS = 32
  16. def fetch_python(version=DEFAULT_PY_VERSION, bitness=DEFAULT_BITNESS,
  17. destination=DEFAULT_BUILD_DIR):
  18. """Fetch the MSI for the specified version of Python.
  19. It will be placed in the destination directory, and validated using GPG
  20. if possible.
  21. """
  22. arch_tag = '.amd64' if (bitness==64) else ''
  23. url = 'http://python.org/ftp/python/{0}/python-{0}{1}.msi'.format(version, arch_tag)
  24. target = pjoin(destination, 'python-{0}{1}.msi'.format(version, arch_tag))
  25. if os.path.isfile(target):
  26. return
  27. urlretrieve(url, target)
  28. urlretrieve(url+'.asc', target+'.asc')
  29. try:
  30. keys_file = os.path.join(_PKGDIR, 'python-pubkeys.txt')
  31. check_output(['gpg', '--import', keys_file])
  32. check_output(['gpg', '--verify', target+'.asc'])
  33. except FileNotFoundError:
  34. print("GPG not available - could not check signature of {0}".format(target))
  35. def write_nsis_file(nsi_file, definitions):
  36. with open(nsi_file, 'w') as f:
  37. for name, value in definitions.items():
  38. f.write('!define {} "{}"\n'.format(name, value))
  39. with open(pjoin(_PKGDIR, 'template.nsi')) as f2:
  40. f.write(f2.read())
  41. def run_nsis(nsi_file):
  42. call(['makensis', nsi_file])
  43. def all_steps(appname, version, script, packages=None, icon=DEFAULT_ICON,
  44. py_version=DEFAULT_PY_VERSION, py_bitness=DEFAULT_BITNESS,
  45. build_dir=DEFAULT_BUILD_DIR,
  46. installer_name=DEFAULT_INSTALLER_NAME):
  47. os.makedirs(build_dir, exist_ok=True)
  48. fetch_python(version=py_version, bitness=py_bitness, destination=build_dir)
  49. shutil.copy2(script, build_dir)
  50. shutil.copy2(icon, build_dir)
  51. # Packages
  52. build_pkg_dir = pjoin(build_dir, 'pkgs')
  53. if os.path.isdir(build_pkg_dir):
  54. shutil.rmtree(build_pkg_dir)
  55. if os.path.isdir('pynsis_pkgs'):
  56. shutil.copytree('pynsis_pkgs', build_pkg_dir)
  57. else:
  58. os.mkdir(build_pkg_dir)
  59. copy_modules(packages or [], build_pkg_dir)
  60. nsi_file = pjoin(build_dir, 'installer.nsi')
  61. definitions = {'PRODUCT_NAME': appname,
  62. 'PRODUCT_VERSION': version,
  63. 'PY_VERSION': py_version,
  64. 'SCRIPT': os.path.basename(script),
  65. 'PRODUCT_ICON': os.path.basename(icon),
  66. 'INSTALLER_NAME': installer_name,
  67. 'ARCH_TAG': '.amd64' if (py_bitness==64) else ''
  68. }
  69. write_nsis_file(nsi_file, definitions)
  70. run_nsis(nsi_file)
  71. def main(argv=None):
  72. import argparse
  73. argp = argparse.ArgumentParser(prog='pynsis')
  74. argp.add_argument('config_file')
  75. options = argp.parse_args(argv)
  76. import configparser
  77. cfg = configparser.ConfigParser()
  78. cfg.read(options.config_file)
  79. appcfg = cfg['Application']
  80. all_steps(
  81. appname = appcfg['name'],
  82. version = appcfg['version'],
  83. script = appcfg['script'],
  84. icon = appcfg.get('icon', DEFAULT_ICON),
  85. packages = cfg.get('Include', 'packages', fallback='').splitlines(),
  86. py_version = cfg.get('Python', 'version', fallback=DEFAULT_PY_VERSION),
  87. py_bitness = cfg.getint('Python', 'bitness', fallback=DEFAULT_BITNESS),
  88. build_dir = cfg.get('Build', 'directory', fallback=DEFAULT_BUILD_DIR),
  89. installer_name = cfg.get('Build', 'installer_name', fallback=DEFAULT_INSTALLER_NAME),
  90. )