Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pynsist installer #15

Merged
merged 6 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/win-installer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Windows Installer

on:
push:
pull_request:

jobs:
build:
# To prevent this job from running, have "[skip ci]" or "[ci skip]" in the commit message
if: contains(toJson(github.event.commits), '[ci skip]') == false && contains(toJson(github.event.commits), '[skip ci]') == false

env:
EXE_NAME: 'iris-win-installer-x64.exe'

runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-latest]
python-version: [3.9]

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- uses: actions/cache@v2
if: startsWith(runner.os, 'Windows')
with:
path: ~\AppData\Local\pip\Cache
key: ${{ runner.os }}-py${{ matrix.python-version }}-pip-${{ hashFiles('**/*requirements.txt') }}

- name: Build installer
run: |
python installer/build.py ${{ env.EXE_NAME }}

- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
path: dist/${{ env.EXE_NAME }}
name: ${{ env.EXE_NAME }}
2 changes: 0 additions & 2 deletions installer/build-exe.ps1

This file was deleted.

5 changes: 0 additions & 5 deletions installer/build-setup.ps1

This file was deleted.

150 changes: 150 additions & 0 deletions installer/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
"""
This script creates a fresh Python environment and create an installer.

Inspired from the Spyder installer script:
https://github.com/spyder-ide/spyder/tree/master/installers/Windows
"""
from pathlib import Path
import sys
from subprocess import run, check_output
import tempfile
import importlib.util as iutil
import shutil
import argparse

REPO_ROOT = Path(__file__).parent.parent
DESTINATION = REPO_ROOT / "dist"
INSTALLER_REQUIREMENTS_FILE = Path(__file__).parent / "inst-requirements.txt"
PYNSIST_CFG_TEMPLATE = """
[Application]
name=iris
version={version}
entry_point=iris.gui:run
icon={icon_file}
publisher={publisher}
license_file={license_file}

[Python]
version={python_version}
bitness=64
format=bundled

[Include]
pypi_wheels= PyQt5==5.15.2
PyQt5-sip==12.8.1
packages=
{packages}
[Build]
installer_name={installer_name}
directory=build/nsis/
"""

parser = argparse.ArgumentParser(
prog="build.py", description="Iris Windows installer build script."
)
parser.add_argument(
"exe_name",
metavar="TARGET",
help="Name of the resulting installer executable (e.g. 'iris-installer.exe')",
type=str,
default=None,
)


def importable_name(pkg):
"""
Translate package name to importable name, e.g. "scikit-image" -> "skimage".
"""
# TODO: find a way to determine this list programatically
translations = {
"scikit-image": "skimage",
"scikit-ued": "skued",
"iris-ued": "iris",
"pyqt5-sip": "sip",
"pillow": "PIL",
"pycifrw": "CifFile",
"pywavelets": "pywt",
"python-dateutil": "dateutil",
"pyyaml": "yaml",
"qdarkstyle": "qdarkstyle", # lowercase important
"qtpy": "qtpy", # lowercase important
}
return translations.get(pkg.lower(), pkg)


def generate_pynsist_config(python_exe, filename, exe_name):
"""
Create a pynsist configuration file. Note that all required packages need
to be installed before calling this function.

Parameters
----------
python_exe : path-like
Full path to the Python executable used to generate the installer.
filename : path-like
Full path to the generated config file.
"""
package_name = lambda t: t.partition("==")[0].split("@")[0].strip()

freeze = check_output(f"{python_exe} -m pip freeze --all").decode("latin1")
# PyQt5/PyQt5-sip requirements are baked in the template string
requirements = [
line
for line in freeze.splitlines()
if package_name(line) not in {"iris-ued", "PyQt5", "PyQt5-sip"}
]
packages = [package_name(p) for p in requirements]

python_version = (
check_output(f"{env_python} --version").decode("latin1").split(" ")[-1].strip()
)

iris_version = check_output(
f'{python_exe} -c "import iris; print(iris.__version__)"'
).decode("latin1")
iris_authors = check_output(
f'{python_exe} -c "import iris; print(iris.__author__)"'
).decode("latin1")

pynsist_cfg_payload = PYNSIST_CFG_TEMPLATE.format(
version=iris_version,
icon_file=Path(__file__).parent / "iris.ico",
license_file=REPO_ROOT / "LICENSE.txt",
python_version=python_version,
publisher=iris_authors,
packages="\n ".join([importable_name(p) for p in packages]),
installer_name=exe_name,
)
with open(filename, mode="wt", encoding="latin1") as f:
f.write(pynsist_cfg_payload)


if __name__ == "__main__":
args = parser.parse_args()
exe_name = args.exe_name

with tempfile.TemporaryDirectory(prefix="installer-iris-ued-") as work_dir:
work_dir = Path(work_dir)
run(f"python -m venv {work_dir} --upgrade --upgrade-deps --copies")

env_python = work_dir / "Scripts" / "python.exe"
run(f"{env_python} -m pip install --upgrade wheel --no-warn-script-location")
run(f"{env_python} -m pip install {REPO_ROOT} --no-warn-script-location")

# Generating configuration file BEFORE installing pynsist ensures that
# we bundle the requirements for iris
cfg_path = work_dir / "pynsist.cfg"
generate_pynsist_config(
python_exe=env_python, filename=cfg_path, exe_name=exe_name
)
print(open(cfg_path, "rt").read())

run(
f"{env_python} -m pip install -r {INSTALLER_REQUIREMENTS_FILE} --no-warn-script-location"
)

run(f"{env_python} -m nsist {cfg_path}")

DESTINATION.mkdir(exist_ok=True)
shutil.copy(work_dir / "build" / "nsis" / exe_name, DESTINATION)
9 changes: 0 additions & 9 deletions installer/hook-dask.py

This file was deleted.

57 changes: 0 additions & 57 deletions installer/hook-pywt.py

This file was deleted.

9 changes: 0 additions & 9 deletions installer/hook-skued.py

This file was deleted.

1 change: 1 addition & 0 deletions installer/inst-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pynsist >= 2, <3
50 changes: 0 additions & 50 deletions installer/iris-onedir.spec

This file was deleted.

49 changes: 0 additions & 49 deletions installer/iris-setup.iss

This file was deleted.

Loading