Skip to content

Commit

Permalink
Added "run condition file" feature to QtIfwExternalOp.RunProgram.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuvinJ committed Jan 6, 2021
1 parent 85a3233 commit 8ca4c36
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
68 changes: 59 additions & 9 deletions distbuilder/qt_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5157,12 +5157,15 @@ def RemoveDir( event, dirPath, isElevated=True ): # TODO: Test in NIX/MAC
# TODO: Test in NIX/MAC
@staticmethod
def RunProgram( event, path, arguments=None, isAutoQuote=True,
isHidden=False, isSynchronous=True, isElevated=True,
isHidden=False, isSynchronous=True, isElevated=True,
runConditionFileName=None, isRunConditionNegated=False,
isAutoBitContext=True ): # Windows Only
return QtIfwExternalOp.__genScriptOp( event,
script=QtIfwExternalOp.RunProgramScript(
path, arguments, isAutoQuote,
isHidden, isSynchronous, isAutoBitContext ),
isHidden, isSynchronous,
runConditionFileName, isRunConditionNegated,
isAutoBitContext ),
isElevated=isElevated )

@staticmethod
Expand Down Expand Up @@ -5407,18 +5410,23 @@ def RemoveDirScript( dirPath ): # TODO: Test in NIX/MAC
@staticmethod
def RunProgramScript( path, arguments=None, isAutoQuote=True,
isHidden=False, isSynchronous=True,
runConditionFileName=None, isRunConditionNegated=False,
isAutoBitContext=True,
replacements=None ):
if arguments is None: arguments =[]
if isHidden:
if IS_WINDOWS :
waitSwitch = " -Wait" if isSynchronous else ""
hiddenStyle = " -WindowStyle Hidden"
argList = [('"%s"' % (a,) if (" " in a or "@" in a) else a)
for a in arguments]
argList =( (" -ArgumentList " +
",".join( ['"%s"' % (a,) for a in arguments]) )
if len(arguments) > 0 else "" )
",".join( ["'%s'" % (a,) for a in argList]) )
if len(arguments) > 0 else "" )
return ExecutableScript( QtIfwExternalOp.__scriptRootName(
"runHiddenProgram" ), extension="ps1", script=([
QtIfwExternalOp.__psExitIfFileMissing(
runConditionFileName, isRunConditionNegated ),
QtIfwExternalOp.__psSetBitContext( isAutoBitContext ),
'Start-Process -FilePath "%s" %s%s%s'
% (path, waitSwitch, hiddenStyle, argList),
Expand All @@ -5430,22 +5438,51 @@ def RunProgramScript( path, arguments=None, isAutoQuote=True,
tokens = [path] + arguments
if isAutoQuote:
tokens = [('"%s"' % (t,) if (" " in t or "@" in t) else t)
for t in tokens]

runCmd = " ".join( tokens )
for t in tokens]
runCmd = " ".join( tokens )
exitSnippet =(
QtIfwExternalOp.__batExitIfFileMissing(
runConditionFileName, isRunConditionNegated )
if IS_WINDOWS else
QtIfwExternalOp.__shExitIfFileMissing(
runConditionFileName, isRunConditionNegated )
)
if not isSynchronous:
if IS_WINDOWS: runCmd = 'start "" ' + runCmd
else: runCmd += " &"
if IS_WINDOWS and not isAutoBitContext:
script=([
exitSnippet,
'set "RUN_CMD=%s"' % (runCmd,),
'if %PROCESSOR_ARCHITECTURE%==x86 ( "%windir%\sysnative\cmd" /c "%RUN_CMD%" ) else ( %RUN_CMD% )',
runCmd])
else: script=runCmd
else: script=[exitSnippet, runCmd ]
return ExecutableScript( QtIfwExternalOp.__scriptRootName(
"runProgram" ), script=script )

if IS_WINDOWS:

@staticmethod
def __batExitIfFileMissing( fileName, isNegated=False, errorCode=0 ):
return str( ExecutableScript( "", script=([
'if {negate}exist "{filePath}" exit /b {errorCode}'
]), replacements={
'negate' : ('' if isNegated else 'not ')
, 'filePath' : qtIfwTempDataFilePath( fileName )
, 'errorCode': errorCode
}) )

@staticmethod
def __psExitIfFileMissing( fileName, isNegated=False, errorCode=0 ):
return str( ExecutableScript( "", script=([
'if( {negate}(Test-Path "{filePath}" -PathType Leaf) ) {'
, ' [Environment]::Exit( {errorCode} )'
, '}'
]), replacements={
'negate' : ('' if isNegated else '! ')
, 'filePath' : qtIfwTempDataFilePath( fileName )
, 'errorCode': errorCode
}) )

__PS_32_TO_64_BIT_CONTEXT_HEADER=(
"""
Expand All @@ -5463,7 +5500,7 @@ def RunProgramScript( path, arguments=None, isAutoQuote=True,
def __psSetBitContext( isAutoBitContext ):
return( "" if isAutoBitContext else
QtIfwExternalOp.__PS_32_TO_64_BIT_CONTEXT_HEADER )

@staticmethod
def __psFindWindowsAppUninstallCmd( appName, isAutoBitContext,
isExitOnNotFound=False ):
Expand Down Expand Up @@ -5868,6 +5905,19 @@ def ReplacePrimaryIconInExeScript( exePath, iconDirPath,
"iconDir": iconDirPath, "setIconName": setIconName,
"removeIconDir": removeIconDir } )

if IS_MACOS or IS_LINUX:

@staticmethod
def __shExitIfFileMissing( fileName, isNegated=False, errorCode=0 ):
if not fileName: return ""
return str( ExecutableScript( "", script=([
'[ {negate}-f "{filePath}" ] && exit {errorCode}'
]), replacements={
'negate' : ('' if isNegated else '! ')
, 'filePath' : qtIfwTempDataFilePath( fileName )
, 'errorCode': errorCode
}) )

# QtIfwExternalOp
def __init__( self, # [0]
script=None, exePath=None, args=None, successRetCodes=None,
Expand Down
37 changes: 28 additions & 9 deletions examples/hello_qtifw_ops/cascading_scripts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from distbuilder import PyToBinInstallerProcess, ConfigFactory, \
QtIfwExternalOp, QtIfwPackageScript, ExecutableScript, \
joinPath, qtIfwDynamicValue, qtIfwTempDataFilePath, \
IS_WINDOWS, QT_IFW_DESKTOP_DIR, QT_IFW_HOME_DIR
QT_IFW_DESKTOP_DIR, QT_IFW_HOME_DIR, \
IS_WINDOWS, QT_IFW_APPS_X86_DIR

ON_INSTALL = QtIfwExternalOp.ON_INSTALL

Expand Down Expand Up @@ -87,11 +88,21 @@ def getVaribaleCascadeOp( fileName, destFilePath ):
return QtIfwExternalOp( script=createFileScript,
uninstScript=QtIfwExternalOp.RemoveFileScript( destFilePath ) )

# Assorted "convenience" operations in the library have built-in
# options to pivot on the state of a given "run condition file".
# I.e. the "boolean" temp file concept (previously shown in an explicit
# manner) maybe applied via these abstractions.
if IS_WINDOWS:
def setAppFoundFileOp( appName, is32Bit, fileName ):
return QtIfwExternalOp.CreateWindowsAppFoundTempFile(
QtIfwExternalOp.ON_BOTH,
appName, fileName, isAutoBitContext=(not is32Bit) )
def setAppFoundFileOp( event, appName, is32BitRegistration, fileName ):
return QtIfwExternalOp.CreateWindowsAppFoundTempFile( event,
appName, fileName, isAutoBitContext=is32BitRegistration )

def launchAppIfFound( event, exePath, appFoundFileName ):
return QtIfwExternalOp.RunProgram( event,
exePath, arguments=["Launched By Cascading Scripts"],
isHidden=False, isSynchronous=False,
runConditionFileName=appFoundFileName,
isRunConditionNegated=False )

BOOL_FILE_NAME = "boolData"
BOOL_EVAL_FILE_NAME = "cascadingBool.txt"
Expand Down Expand Up @@ -120,11 +131,19 @@ def setAppFoundFileOp( appName, is32Bit, fileName ):
]

if IS_WINDOWS:
APP_NAME = "Hello World Tk Example"
IS_32BIT_APP = True
APP_FOUND_FILENAME = "HelloWorldTkInstalled"
APP_NAME = "Hello Installer Conveniences Example"
COMPANY_NAME = "Some Company"
EXE_NAME = "RunConditionsTest.exe"
IS_32BIT_REG = False
APP_FOUND_FILENAME = "ConveniencesAppInstalled"
EXE_PATH = joinPath( QT_IFW_APPS_X86_DIR, COMPANY_NAME, APP_NAME,
EXE_NAME )
pkg.pkgScript.externalOps += [
setAppFoundFileOp( APP_NAME, IS_32BIT_APP, APP_FOUND_FILENAME )
setAppFoundFileOp( QtIfwExternalOp.ON_INSTALL,
APP_NAME, IS_32BIT_REG, APP_FOUND_FILENAME ),
launchAppIfFound( QtIfwExternalOp.ON_BOTH, EXE_PATH, APP_FOUND_FILENAME ),
setAppFoundFileOp( QtIfwExternalOp.ON_UNINSTALL,
APP_NAME, IS_32BIT_REG, APP_FOUND_FILENAME ),
]

p = BuildProcess( configFactory, isDesktopTarget=True )
Expand Down

0 comments on commit 8ca4c36

Please sign in to comment.