Add code signing and MSI packaging for Windows

This commit is contained in:
Micah Lee 2022-01-12 19:58:40 -08:00
parent d327db58e4
commit 7879ba4a7f
4 changed files with 439 additions and 9 deletions

View file

@ -3,6 +3,8 @@ import os
import inspect
import subprocess
import shutil
import uuid
import xml.etree.ElementTree as ET
root = os.path.dirname(
@ -12,9 +14,35 @@ root = os.path.dirname(
)
def run(cmd, cwd=None):
def run(cmd, cwd=None, error_ok=False):
print(cmd)
subprocess.run(cmd, cwd=cwd, check=True)
try:
subprocess.run(cmd, cwd=cwd, check=True)
except subprocess.CalledProcessError as e:
if not error_ok:
raise subprocess.CalledProcessError(e)
def sign(filename, cwd=None):
run(
[
shutil.which("signtool"),
"sign",
"/v",
"/d",
"OnionShare",
"/sha1",
"bb1d265ab02272e8fc742f149dcf8751cac63f50",
"/fd",
"SHA256",
"/td",
"SHA256",
"/tr",
"http://timestamp.digicert.com",
filename,
],
cwd,
)
def get_size(dir):
@ -26,14 +54,121 @@ def get_size(dir):
return size
def wix_build_data(dirname, dir_prefix, id_, name):
data = {
"id": id_,
"name": name,
"files": [],
"dirs": [],
}
for basename in os.listdir(dirname):
filename = os.path.join(dirname, basename)
if os.path.isfile(filename):
data["files"].append(os.path.join(dir_prefix, basename))
elif os.path.isdir(filename):
if id_ == "INSTALLDIR":
id_prefix = "Folder"
else:
id_prefix = id_
# Skip lib/Pyside2/Examples folder
if "\\build\\exe.win32-3.9\\lib\\PySide2\\examples" in dirname:
continue
id_value = f"{id_prefix}{basename.capitalize().replace('-', '_')}"
data["dirs"].append(
wix_build_data(
os.path.join(dirname, basename),
os.path.join(dir_prefix, basename),
id_value,
basename,
)
)
if len(data["files"]) > 0:
if id_ == "INSTALLDIR":
data["component_id"] = "ApplicationFiles"
else:
data["component_id"] = "FolderComponent" + id_[len("Folder") :]
data["component_guid"] = str(uuid.uuid4())
return data
def wix_build_dir_xml(root, data):
attrs = {}
if "id" in data:
attrs["Id"] = data["id"]
if "name" in data:
attrs["Name"] = data["name"]
el = ET.SubElement(root, "Directory", attrs)
for subdata in data["dirs"]:
wix_build_dir_xml(el, subdata)
# If this is the ProgramMenuFolder, add the menu component
if "id" in data and data["id"] == "ProgramMenuFolder":
component_el = ET.SubElement(
el,
"Component",
Id="ApplicationShortcuts",
Guid="539e7de8-a124-4c09-aa55-0dd516aad7bc",
)
ET.SubElement(
component_el,
"Shortcut",
Id="ApplicationShortcut1",
Name="OnionShare",
Description="OnionShare",
Target="[INSTALLDIR]onionshare.exe",
WorkingDirectory="INSTALLDIR",
)
ET.SubElement(
component_el,
"RegistryValue",
Root="HKCU",
Key="Software\OnionShare",
Name="installed",
Type="integer",
Value="1",
KeyPath="yes",
)
def wix_build_components_xml(root, data):
component_ids = []
if "component_id" in data:
component_ids.append(data["component_id"])
for subdata in data["dirs"]:
if "component_guid" in subdata:
dir_ref_el = ET.SubElement(root, "DirectoryRef", Id=subdata["id"])
component_el = ET.SubElement(
dir_ref_el,
"Component",
Id=subdata["component_id"],
Guid=subdata["component_guid"],
)
for filename in subdata["files"]:
file_el = ET.SubElement(
component_el, "File", Source=filename, Id="file_" + uuid.uuid4().hex
)
component_ids += wix_build_components_xml(root, subdata)
return component_ids
def main():
desktop_dir = os.path.join(root, "desktop")
print("○ Clean up from last build")
if os.path.exists(os.path.join(desktop_dir, "build")):
shutil.rmtree(os.path.join(desktop_dir, "build"))
if os.path.exists(os.path.join(desktop_dir, "dist")):
shutil.rmtree(os.path.join(desktop_dir, "dist"))
print("○ Building binaires")
print("○ Building binaries")
run(
[
shutil.which("python"),
@ -288,9 +423,165 @@ def main():
after_size = get_size(os.path.join(desktop_dir, "build", "exe.win32-3.9"))
freed_bytes = before_size - after_size
freed_mb = freed_bytes / 1024 / 1024
freed_mb = int(freed_bytes / 1024 / 1024)
print(f"○ Freed {freed_mb} mb")
print(f"○ Signing onionshare.exe")
sign(os.path.join("build", "exe.win32-3.9", "onionshare.exe"), desktop_dir)
print(f"○ Signing onionshare-cli.exe")
sign(os.path.join("build", "exe.win32-3.9", "onionshare-cli.exe"), desktop_dir)
print(f"○ Build the WiX file")
version_filename = os.path.join(
root, "cli", "onionshare_cli", "resources", "version.txt"
)
with open(version_filename) as f:
version = f.read().strip()
dist_dir = os.path.join(
root,
"desktop",
"build",
"exe.win32-3.9",
)
data = {
"id": "TARGETDIR",
"name": "SourceDir",
"dirs": [
{
"id": "ProgramFilesFolder",
"dirs": [],
},
{
"id": "ProgramMenuFolder",
"dirs": [],
},
],
}
data["dirs"][0]["dirs"].append(
wix_build_data(
dist_dir,
"exe.win32-3.9",
"INSTALLDIR",
"OnionShare",
)
)
root_el = ET.Element("Wix", xmlns="http://schemas.microsoft.com/wix/2006/wi")
product_el = ET.SubElement(
root_el,
"Product",
Name="OnionShare",
Manufacturer="Micah Lee, et al.",
Id="*",
UpgradeCode="$(var.ProductUpgradeCode)",
Language="1033",
Codepage="1252",
Version="$(var.ProductVersion)",
)
ET.SubElement(
product_el,
"Package",
Id="*",
Keywords="Installer",
Description="OnionShare $(var.ProductVersion) Installer",
Manufacturer="Micah Lee, et al.",
InstallerVersion="100",
Languages="1033",
Compressed="yes",
SummaryCodepage="1252",
)
ET.SubElement(product_el, "Media", Id="1", Cabinet="product.cab", EmbedCab="yes")
ET.SubElement(
product_el,
"Icon",
Id="ProductIcon",
SourceFile="..\\onionshare\\resources\\onionshare.ico",
)
ET.SubElement(product_el, "Property", Id="ARPPRODUCTICON", Value="ProductIcon")
ET.SubElement(
product_el,
"Property",
Id="ARPHELPLINK",
Value="https://docs.onionshare.org",
)
ET.SubElement(
product_el,
"Property",
Id="ARPURLINFOABOUT",
Value="https://onionshare.org",
)
ET.SubElement(product_el, "UIRef", Id="WixUI_Minimal")
ET.SubElement(product_el, "UIRef", Id="WixUI_ErrorProgressText")
ET.SubElement(
product_el,
"WixVariable",
Id="WixUILicenseRtf",
Value="..\\package\\license.rtf",
)
# ET.SubElement(
# product_el,
# "WixVariable",
# Id="WixUIDialogBmp",
# Value="..\\package\\dialog.bmp",
# )
ET.SubElement(
product_el,
"MajorUpgrade",
AllowSameVersionUpgrades="yes",
DowngradeErrorMessage="A newer version of [ProductName] is already installed. If you are sure you want to downgrade, remove the existing installation via Programs and Features.",
)
wix_build_dir_xml(product_el, data)
component_ids = wix_build_components_xml(product_el, data)
feature_el = ET.SubElement(product_el, "Feature", Id="DefaultFeature", Level="1")
for component_id in component_ids:
ET.SubElement(feature_el, "ComponentRef", Id=component_id)
ET.SubElement(feature_el, "ComponentRef", Id="ApplicationShortcuts")
with open(os.path.join(root, "desktop", "build", "OnionShare.wxs"), "w") as f:
f.write('<?xml version="1.0" encoding="windows-1252"?>\n')
f.write(f'<?define ProductVersion = "{version}"?>\n')
f.write(
'<?define ProductUpgradeCode = "12b9695c-965b-4be0-bc33-21274e809576"?>\n'
)
ET.indent(root_el)
f.write(ET.tostring(root_el).decode())
print(f"○ Build the MSI")
run(
[shutil.which("candle.exe"), "OnionShare.wxs"],
os.path.join(desktop_dir, "build"),
)
run(
[shutil.which("light.exe"), "-ext", "WixUIExtension", "OnionShare.wixobj"],
os.path.join(desktop_dir, "build"),
)
print(f"○ Prepare OnionShare.msi for signing")
run(
[
shutil.which("insignia.exe"),
"-im",
os.path.join(desktop_dir, "build", "OnionShare.msi"),
],
error_ok=True,
)
sign(os.path.join(desktop_dir, "build", "OnionShare.msi"))
final_msi_filename = os.path.join(desktop_dir, "dist", f"OnionShare-{version}.msi")
print(f"○ Final MSI: {final_msi_filename}")
os.makedirs(os.path.join(desktop_dir, "dist"), exist_ok=True)
os.rename(
os.path.join(desktop_dir, "build", "OnionShare.msi"),
final_msi_filename,
)
if __name__ == "__main__":
main()