2022-03-31 21:34:41 -04:00
#!/usr/bin/env python3
from distutils . command . build import build
import sys
import os
import inspect
import click
import shutil
import subprocess
import uuid
import xml . etree . ElementTree as ET
2023-02-08 18:28:20 -05:00
from glob import glob
2022-03-31 21:34:41 -04:00
root = os . path . dirname (
os . path . dirname (
os . path . dirname ( os . path . abspath ( inspect . getfile ( inspect . currentframe ( ) ) ) )
)
)
desktop_dir = os . path . join ( root , " desktop " )
def get_size ( dir ) :
size = 0
for path , dirs , files in os . walk ( dir ) :
for f in files :
fp = os . path . join ( path , f )
size + = os . path . getsize ( fp )
return size
def run ( cmd , cwd = None , error_ok = False ) :
print ( cmd )
try :
subprocess . run ( cmd , cwd = cwd , check = True )
except subprocess . CalledProcessError as e :
if not error_ok :
raise subprocess . CalledProcessError ( e )
def sign ( filename ) :
2022-04-10 19:07:35 -04:00
click . echo ( f " > Signing { filename } " )
2022-03-31 21:34:41 -04:00
run (
[
2023-10-20 14:16:51 -04:00
shutil . which ( " signtool.exe " ) ,
2022-03-31 21:34:41 -04:00
" sign " ,
" /v " ,
" /d " ,
" OnionShare " ,
2023-10-20 14:16:51 -04:00
" /n " ,
" Science and Design Inc. " ,
2022-03-31 21:34:41 -04:00
" /fd " ,
2023-10-20 14:16:51 -04:00
" sha256 " ,
2022-03-31 21:34:41 -04:00
" /td " ,
2023-10-20 14:16:51 -04:00
" sha256 " ,
2022-03-31 21:34:41 -04:00
" /tr " ,
" http://timestamp.digicert.com " ,
filename ,
]
)
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 " \\ 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 " ,
2023-10-20 15:44:57 -04:00
Win64 = " yes " ,
2022-03-31 21:34:41 -04:00
)
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 " ] ,
2023-10-20 15:44:57 -04:00
Win64 = " yes " ,
2022-03-31 21:34:41 -04:00
)
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
2022-04-05 15:41:21 -04:00
def msi_package ( build_path , msi_path , product_update_code ) :
print ( f " > Build the WiX file " )
version_filename = os . path . join (
build_path , " lib " , " onionshare_cli " , " resources " , " version.txt "
)
with open ( version_filename ) as f :
version = f . read ( ) . strip ( )
2022-06-20 00:50:32 -04:00
# change a version like 2.6.dev1 to just 2.6, for cx_Freeze's sake
last_digit = version [ - 1 ]
if version . endswith ( f " .dev { last_digit } " ) :
version = version [ 0 : - 5 ]
2022-04-05 15:41:21 -04:00
data = {
" id " : " TARGETDIR " ,
" name " : " SourceDir " ,
" dirs " : [
{
2023-10-20 15:29:59 -04:00
" id " : " ProgramFiles64Folder " ,
2022-04-05 15:41:21 -04:00
" dirs " : [ ] ,
} ,
{
" id " : " ProgramMenuFolder " ,
" dirs " : [ ] ,
} ,
] ,
}
data [ " dirs " ] [ 0 ] [ " dirs " ] . append (
wix_build_data (
build_path ,
" . " ,
" 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. " ,
2023-10-20 15:44:57 -04:00
InstallerVersion = " 200 " ,
2022-04-05 15:41:21 -04:00
Languages = " 1033 " ,
Compressed = " yes " ,
SummaryCodepage = " 1252 " ,
2023-10-20 15:44:57 -04:00
Platform = " x64 " ,
2022-04-05 15:41:21 -04:00
)
ET . SubElement ( product_el , " Media " , Id = " 1 " , Cabinet = " product.cab " , EmbedCab = " yes " )
ET . SubElement (
product_el ,
" Icon " ,
Id = " ProductIcon " ,
SourceFile = os . path . join (
desktop_dir , " 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 = os . path . join ( desktop_dir , " package " , " license.rtf " ) ,
)
ET . SubElement (
product_el ,
" WixVariable " ,
Id = " WixUIDialogBmp " ,
Value = os . path . join ( desktop_dir , " 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 ( build_path , " OnionShare.wxs " ) , " w " ) as f :
f . write ( ' <?xml version= " 1.0 " encoding= " windows-1252 " ?> \n ' )
f . write ( f ' <?define ProductVersion = " { version } " ?> \n ' )
f . write ( f ' <?define ProductUpgradeCode = " { product_update_code } " ?> \n ' )
ET . indent ( root_el )
f . write ( ET . tostring ( root_el ) . decode ( ) )
print ( f " > Build the MSI " )
run (
[ shutil . which ( " candle.exe " ) , " OnionShare.wxs " ] ,
build_path ,
)
run (
[ shutil . which ( " light.exe " ) , " -ext " , " WixUIExtension " , " OnionShare.wixobj " ] ,
build_path ,
)
print ( f " > Prepare OnionShare.msi for signing " )
run (
[
shutil . which ( " insignia.exe " ) ,
" -im " ,
os . path . join ( build_path , " OnionShare.msi " ) ,
] ,
error_ok = True ,
)
sign ( os . path . join ( build_path , " OnionShare.msi " ) )
print ( f " > Final MSI: { msi_path } " )
os . makedirs ( os . path . join ( desktop_dir , " dist " ) , exist_ok = True )
os . rename (
os . path . join ( build_path , " OnionShare.msi " ) ,
msi_path ,
)
2022-03-31 21:34:41 -04:00
@click.group ( )
def main ( ) :
"""
Windows build tasks
"""
@main.command ( )
def cleanup_build ( ) :
2023-02-08 18:28:20 -05:00
""" Delete unused PySide6 stuff to save space """
2023-10-16 23:57:00 -04:00
build_path = os . path . join ( desktop_dir , " build " , " exe.win-amd64-3.11 " )
2022-03-31 21:34:41 -04:00
before_size = get_size ( build_path )
2023-05-21 19:30:45 -04:00
for dirname in [ " qml " ] :
2023-02-08 18:28:20 -05:00
shutil . rmtree ( os . path . join ( build_path , " lib " , " PySide6 " , dirname ) )
for dirname in [
" assetimporters " ,
" designer " ,
" generic " ,
" geometryloaders " ,
" platforminputcontexts " ,
" position " ,
" qmltooling " ,
" renderers " ,
" renderplugins " ,
" sceneparsers " ,
" scxmldatamodel " ,
" sensors " ,
" sqldrivers " ,
" styles " ,
2022-03-31 21:34:41 -04:00
] :
2023-02-08 18:28:20 -05:00
shutil . rmtree ( os . path . join ( build_path , " lib " , " PySide6 " , " plugins " , dirname ) )
for filename in (
glob ( os . path . join ( build_path , " lib " , " PySide6 " , " *.exe " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt3D* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt63D* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Bluetooth.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Charts* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Concurrent.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6DataVisualization* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Bus.dll " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Designer* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Help.dll " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Labs* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Multimedia* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Nfc.dll " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6OpenGL* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Qml* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Quick* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6RemoteObjects* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Scxml* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Sensors* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6SerialPort.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6ShaderTools.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Sql.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6StateMachine* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Test.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6VirtualKeyboard.* " ) )
+ glob ( os . path . join ( build_path , " lib " , " PySide6 " , " Qt6Web* " ) )
) :
os . remove ( filename )
2022-03-31 21:34:41 -04:00
after_size = get_size ( build_path )
freed_bytes = before_size - after_size
freed_mb = int ( freed_bytes / 1024 / 1024 )
print ( f " Freed { freed_mb } mb " )
@main.command ( )
2023-02-08 18:28:20 -05:00
@click.argument ( " path " )
def codesign ( path ) :
2022-03-31 21:34:41 -04:00
""" Sign Windows binaries before packaging """
2023-02-08 18:28:20 -05:00
if not os . path . isdir ( path ) :
click . echo ( " Invalid build path " )
return
sign ( os . path . join ( path , " onionshare.exe " ) )
sign ( os . path . join ( path , " onionshare-cli.exe " ) )
sign (
os . path . join (
path ,
" lib " ,
" onionshare " ,
" resources " ,
" tor " ,
" meek-client.exe " ,
2022-04-10 19:07:35 -04:00
)
2023-02-08 18:28:20 -05:00
)
sign (
os . path . join (
path ,
" lib " ,
" onionshare " ,
" resources " ,
" tor " ,
" obfs4proxy.exe " ,
2022-04-10 19:07:35 -04:00
)
2023-02-08 18:28:20 -05:00
)
sign (
os . path . join (
path ,
" lib " ,
" onionshare " ,
" resources " ,
" tor " ,
" snowflake-client.exe " ,
2022-03-31 21:34:41 -04:00
)
2023-02-08 18:28:20 -05:00
)
2022-03-31 21:34:41 -04:00
@main.command ( )
2023-02-08 18:28:20 -05:00
@click.argument ( " path " )
def package ( path ) :
2022-03-31 21:34:41 -04:00
""" Build the MSI package """
version_filename = os . path . join (
root , " cli " , " onionshare_cli " , " resources " , " version.txt "
)
with open ( version_filename ) as f :
version = f . read ( ) . strip ( )
2022-04-05 15:41:21 -04:00
msi_package (
2023-02-08 18:28:20 -05:00
path ,
2022-04-05 15:41:21 -04:00
os . path . join ( desktop_dir , " dist " , f " OnionShare-win64- { version } .msi " ) ,
" ed7f9243-3528-4b4a-b85c-9943982e75eb " ,
2022-03-31 21:34:41 -04:00
)
if __name__ == " __main__ " :
main ( )