Skip to content

Commit

Permalink
Added isDesktopTarget and isHomeDirTarget features to PyToBinPackageP…
Browse files Browse the repository at this point in the history
…rocess class. Refactored buildTrustCertInstaller function into a more client customizable design of using trustCertInstallerConfigFactory function paired with TrustInstallerBuilderProcess class.
  • Loading branch information
BuvinJ committed Feb 12, 2021
1 parent b86b7b0 commit 6fa2a2c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 58 deletions.
3 changes: 2 additions & 1 deletion distbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@
from distbuilder.code_sign import \
SelfSignedCertConfig \
, SignToolConfig \
, TrustInstallerBuilderProcess \
, trustCertInstallerConfigFactory \
, generateTrustCerts \
, buildTrustCertInstaller \
, signExe \
, SIGNTOOL_PATH_ENV_VAR

Expand Down
90 changes: 47 additions & 43 deletions distbuilder/code_sign.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from distbuilder.master import ConfigFactory, PyToBinPackageProcess
from distbuilder import util # @UnusedImport
from distbuilder.util import * # @UnusedWildImport

Expand Down Expand Up @@ -398,21 +399,10 @@ def generateTrustCerts( certConfig, keyPassword=None, isOverwrite=False ):
#TODO: SUPPORT OTHER PLATFORMS! USING OPENSSL?
util._onPlatformErr()

def buildTrustCertInstaller(
companyTradeName, caCertPath, keyFilePath, keyPassword=None,
companyLegalName=None, version=(1,0,0,0), iconFilePath=None,
isDesktopTarget=False, isHomeDirTarget=False,
isSilent=False, isTest=False ):
"""
Returns path to installer
"""
print( "Building Trust Certificate Installer..." )
#TODO: SUPPORT OTHER PLATFORMS!!!
if not IS_WINDOWS: util._onPlatformErr()

from distbuilder.master import ConfigFactory, PyToBinPackageProcess

script = ExecutableScript( "__installTrustCert", extension="py", script=[
def _trustCertInstallerScript( companyTradeName, caCertPath,
iconFilePath=None, isSilent=False ):

return ExecutableScript( "__installTrustCert", extension="py", script=[
'import sys, os, traceback'
,'from subprocess import( check_call, check_output,'
,' STARTUPINFO, STARTF_USESHOWWINDOW )'
Expand Down Expand Up @@ -470,45 +460,59 @@ def buildTrustCertInstaller(
, "iconFileName" : baseFileName( iconFilePath )
, "commonName" : companyTradeName }
)
script.write( THIS_DIR )

compressedCompName = companyTradeName.replace(" ","").replace(".","")

def trustCertInstallerConfigFactory( companyTradeName,
caCertPath, keyFilePath, keyPassword=None,
companyLegalName=None, version=(1,0,0,0), iconFilePath=None,
isSilent=False, script=None ):

#TODO: SUPPORT OTHER PLATFORMS!!!
if not IS_WINDOWS: util._onPlatformErr()

f = configFactory = ConfigFactory()
if script is None :
script = _trustCertInstallerScript( companyTradeName, caCertPath,
iconFilePath, isSilent )

f = ConfigFactory()
f.productName = "Trust %s" % (companyTradeName,)
f.description = "Trust Certificate Installer"
f.binaryName = "Trust%s" % (compressedCompName,)
f.binaryName = "Trust%s" % (
companyTradeName.replace(" ","").replace(".",""),)
f.companyTradeName = companyTradeName
f.companyLegalName = companyLegalName
f.isGui = not isSilent
f.iconFilePath = iconFilePath
f.version = version
f.isOneFile = True
f.entryPointPy = script.fileName()
f.entryPointPy = script.fileName()

class TrustInstallerBuilderProcess( PyToBinPackageProcess ):
def onPyInstConfig( self, cfg ):
cfg.isAutoElevated = True
cfg.dataFilePaths = [ caCertPath ]

def onFinalize( self ):
script.remove()

# TODO: Support other platforms!
# sign the installer itself
signConfig = SignToolConfig( pfxFilePath=absPath( keyFilePath ),
keyPassword=keyPassword )
signExe( self.binPath, signConfig )

p = TrustInstallerBuilderProcess( configFactory )
p.run()
# Adding custom attributes on the fly!
f._trustCertScript = script
f._caCertPath = caCertPath
f._keyFilePath = keyFilePath
f._keyPassword = keyPassword

return f

if isDesktopTarget: installerPath = moveToDesktop( p.binPath )
elif isHomeDirTarget: installerPath = moveToHomeDir( p.binPath )
else : installerPath = moveToDir( p.binPath, THIS_DIR )
removeDir( p.binDir )
if isTest: run( installerPath, isElevated=True, isDebug=True )
return installerPath
class TrustInstallerBuilderProcess( PyToBinPackageProcess ):
def onInitialize( self ):
#TODO: SUPPORT OTHER PLATFORMS!!!
if not IS_WINDOWS: util._onPlatformErr()
self.configFactory._trustCertScript.write()

def onPyInstConfig( self, cfg ):
cfg.isAutoElevated = True
cfg.dataFilePaths = [ self.configFactory._caCertPath ]
if self.configFactory.iconFilePath:
cfg.dataFilePaths.append( self.configFactory.iconFilePath )

def onFinalize( self ):
self.configFactory._trustCertScript.remove()
# sign the installer itself
signConfig = SignToolConfig(
pfxFilePath=absPath( self.configFactory._keyFilePath ),
keyPassword=self.configFactory._keyPassword )
signExe( self.binPath, signConfig )

def signExe( exePath, signToolConfig ):
exePath = normBinaryName( exePath, isPathPreserved=True )
Expand Down
41 changes: 34 additions & 7 deletions distbuilder/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,18 +450,21 @@ class PyToBinPackageProcess( _DistBuildProcessBase ):

def __init__( self, configFactory,
name="Python to Binary Package Process",
isZipped=False ) :
isZipped=False, isDesktopTarget=False, isHomeDirTarget=False ) :
_DistBuildProcessBase.__init__( self, configFactory, name )
self.isZipped = isZipped

self.isDesktopTarget = isDesktopTarget
self.isHomeDirTarget = isHomeDirTarget

self.isWarningSuppression = True
self.isUnBufferedStdIo = False
self.isPyInstDupDataPatched = None

self.isObfuscationTest = False
self.isExeTest = False
self.isElevatedTest = False
self.exeTestArgs = []

self._pyInstConfig = None

# Results
Expand Down Expand Up @@ -503,12 +506,36 @@ def _body( self ):
self.binDir, self.binPath = (
buildExecutable( pyInstConfig=self._pyInstConfig,
opyConfig=opyConfig ) )
if self.isExeTest :
run( self.binPath, self.exeTestArgs,
isElevated=self.isElevatedTest, isDebug=True )

destDirPath =( util._userDesktopDirPath() if self.isDesktopTarget else
util._userHomeDirPath() if self.isHomeDirTarget else
None )
if self.isZipped :
toZipFile( self.binDir, zipDest=None, removeScr=True )
# test exe first!
if self.isExeTest : self.__testExe()
# bin path is actually the zip now...
self.binPath = toZipFile( self.binDir, zipDest=None, removeScr=True )
if destDirPath and self.binDir != destDirPath:
self.binPath = moveToDir( self.binPath, destDirPath )
self.binDir = destDirPath
else:
if destDirPath and self.binDir != destDirPath:
binName = baseFileName( self.binPath )
isOtherContent = len( [item for item in listdir( self.binDir )
if item != binName ] ) > 0
if isOtherContent:
self.binDir = moveToDir( self.binDir, destDirPath )
self.binPath = joinPath( self.binDir, binName )
else:
originDirPath = self.binDir
self.binPath = moveToDir( self.binPath, destDirPath )
self.binDir = destDirPath
removeDir( originDirPath )
if self.isExeTest : self.__testExe()

def __testExe( self ):
run( self.binPath, self.exeTestArgs,
isElevated=self.isElevatedTest, isDebug=True )

# Override these to further customize the build process once the
# ConfigFactory has produced each initial config object
Expand Down
19 changes: 12 additions & 7 deletions examples/hello_code_signing/certs/generateTrustCerts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from distbuilder import( SelfSignedCertConfig,
getPassword, generateTrustCerts, buildTrustCertInstaller )
from distbuilder import( SelfSignedCertConfig, TrustInstallerBuilderProcess,
getPassword, generateTrustCerts, trustCertInstallerConfigFactory )

companyTradeName = "Some Company"
companyLegalName = "Some Company Inc."
Expand All @@ -17,8 +17,13 @@
caCertPath, keyFilePath = generateTrustCerts(
certConfig, keyPassword=password, isOverwrite=True )

# build an installer to distribute to users
buildTrustCertInstaller(
companyTradeName, caCertPath, keyFilePath, keyPassword=password,
companyLegalName=companyLegalName, iconFilePath=iconFilePath,
isSilent=False, isDesktopTarget=True, isTest=True )
configFactory = trustCertInstallerConfigFactory(
companyTradeName, caCertPath, keyFilePath, keyPassword=password,
companyLegalName=companyLegalName, iconFilePath=iconFilePath,
isSilent=False )

p = TrustInstallerBuilderProcess( configFactory, isDesktopTarget=True )
p.isExeTest = True
p.isElevatedTest = True
p.run()

0 comments on commit 6fa2a2c

Please sign in to comment.