Skip to content

Instantly share code, notes, and snippets.

@sakurai-youhei
Created June 28, 2018 08:55
Show Gist options
  • Save sakurai-youhei/8d99512cd9c960d7f110ac9a66218ab2 to your computer and use it in GitHub Desktop.
Save sakurai-youhei/8d99512cd9c960d7f110ac9a66218ab2 to your computer and use it in GitHub Desktop.
WIP: Build MSI package deploying Windows service written in Python
from io import BytesIO
from msilib import add_tables
from msilib import init_database
from msilib import schema
from msilib import sequence
from os.path import abspath
from os.path import dirname
from os.path import normpath
from shutil import rmtree
from subprocess import check_call
from subprocess import PIPE
from subprocess import Popen
from urllib.request import urlopen
from uuid import uuid4
from zipfile import ZipFile
pyemb = "https://www.python.org/ftp/python/3.7.0/python-3.7.0-embed-amd64.zip"
getpip = "https://bootstrap.pypa.io/get-pip.py"
bdir = dirname(__file__) + "/build"
py = bdir + "/python.exe"
pth = bdir + "/python37._pth"
pip = (py, "-m", "pip", "install")
msi = normpath(abspath(dirname(__file__) + "/winsvc-py37.msi"))
ProductName = "test"
ProductCode = ("{%s}" % uuid4()).upper()
ProductVersion = "0.0.0.1"
Manufacturer = "me"
rmtree(bdir, ignore_errors=True)
with urlopen(pyemb) as res:
with ZipFile(BytesIO(res.read())) as zfp:
zfp.extractall(bdir)
with urlopen(getpip) as res:
p = Popen(py, stdin=PIPE)
p.communicate(res.read())
with open(pth, "a") as fp:
print("Lib\site-packages", file=fp)
check_call(pip + ("pypiwin32",))
db = init_database(msi, schema,
ProductName, ProductCode, ProductVersion, Manufacturer)
add_tables(db, sequence)
db.Commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment