Skip to content

Commit

Permalink
Added self destruct script snippet functions to QtIfwExternalOp.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuvinJ committed Jan 10, 2021
1 parent 2742f6c commit 6d43a07
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
27 changes: 26 additions & 1 deletion distbuilder/qt_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3310,7 +3310,6 @@ def __genFinishedClickedCallbackBody( self ):
TAB = _QtIfwScript.TAB
SBLK = _QtIfwScript.START_BLOCK
EBLK = _QtIfwScript.END_BLOCK
ELSE = _QtIfwScript.ELSE

prepend = _QtIfwScript.log( "finish clicked" )

Expand Down Expand Up @@ -5429,6 +5428,32 @@ def WrapperScript2Exe( scriptPath, exePath, targetPath,
QtIfwExternalResource.RESOURCE_HACKER ) ] )
]

# Script Builders
# -----------------
@staticmethod
def batchSelfDestructSnippet():
return '(goto) 2>nul & del "%~f0"'

@staticmethod
def powerShellSelfDestructSnippet():
return 'Remove-Item $script:MyInvocation.MyCommand.Path -Force'

@staticmethod
def vbScriptSelfDestructSnippet():
return ExecutableScript.linesToStr([
'Set oFSO = CreateObject("Scripting.FileSystemObject")'
,'oFSO.DeleteFile WScript.ScriptFullName'
,'Set oFSO = Nothing'
])

@staticmethod
def shellScriptSelfDestructSnippet():
return "" #TODO: Fill in NIX/MAC

@staticmethod
def appleScriptSelfDestructSnippet():
return "" #TODO: Fill in NIX/MAC

@staticmethod
def __scriptRootName( prefix ):
QtIfwExternalOp.__AUTO_SCRIPT_COUNT+=1
Expand Down
13 changes: 10 additions & 3 deletions distbuilder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,14 @@ class ExecutableScript(): # Roughly mirrors PlasticFile, but would override all
__SHEBANG_TEMPLATE = "%s\n%s"

__PLACEHOLDER_TMPLT = "{%s}"

@staticmethod
def strToLines( s ): return s.split( _NEWLINE ) if s else []

@staticmethod
def linesToStr( lines ): return _NEWLINE.join( lines )

# ExecutableScript
def __init__( self, rootName,
extension=True, # True==auto assign, str==what to use, or None
shebang=True, # True==auto assign, str==what to use, or None
Expand Down Expand Up @@ -1016,10 +1023,10 @@ def read( self, dirPath ):
filePath = joinPath( dirPath, self.fileName() )
with open( filePath, 'r' ) as f : self.script = f.read()

def toLines( self ):
return self.script.split( _NEWLINE ) if self.script else []
def toLines( self ): return ExecutableScript.strToLines( self.script )

def fromLines( self, lines ): self.script = _NEWLINE.join( lines )
def fromLines( self, lines ):
self.script = ExecutableScript.linesToStr( lines )

def injectLine( self, injection, lineNo ):
lines = self.toLines()
Expand Down
22 changes: 21 additions & 1 deletion docs/ConfigClasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,27 @@ use of SysWow64 nodes.
targetPath, brandingInfo, iconName="0.ico" )

### QtIfwExternalOp Convenience Scripts


#### Self-destruct Script Snippets

In some circumstances, e.g. when using scripts with
[QtIfwOnFinishedDetachedExec](#qtifwonfinisheddetachedexec) or
[QtIfwOnFinishedCheckbox](#qtifwonfinishedcheckbox), you may wish for your
scripts to "self-desctruct" (i.e. delete themselves).

The following functions return strings that you can append to / weave into
your custom scripts.

##### QtIfwExternalOp.batchSelfDestructSnippet

##### QtIfwExternalOp.powerShellSelfDestructSnippet

##### QtIfwExternalOp.vbScriptSelfDestructSnippet

##### QtIfwExternalOp.shellScriptSelfDestructSnippet

##### QtIfwExternalOp.appleScriptSelfDestructSnippet
#### QtIfwExternalOp.RunProgramScript

RunProgramScript( path, arguments=None, isAutoQuote=True,
Expand Down
5 changes: 5 additions & 0 deletions docs/LowLevel.md
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,11 @@ Functions:
fromBase64( data )

debug()

Static Functions:

strToLines( s )
linesToStr( lines )
Details:

Expand Down
20 changes: 10 additions & 10 deletions examples/hello_dynamic_finish/build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from distbuilder import( RobustInstallerProcess, ConfigFactory,
QtIfwOnFinishedCheckbox, QtIfwOnFinishedDetachedExec, \
QtIfwControlScript, ExecutableScript, \
QtIfwOnFinishedCheckbox, QtIfwOnFinishedDetachedExec,
QtIfwControlScript, QtIfwExternalOp, ExecutableScript,
findQtIfwPackage, joinPathQtIfw, joinPath,
IS_WINDOWS, IS_MACOS, QT_IFW_TARGET_DIR, QT_IFW_DESKTOP_DIR )

Expand Down Expand Up @@ -85,6 +85,8 @@ def customizeFinishedPage( cfg, tkPkg, cliPkg ):
# remain in your system temp folder until otherwise purged.
# This self destruction is a *best practice*, but it's not a
# major calamity if you forget, or it fails...
# To this end, QtIfwExternalOp provides a collection of helper
# script snippet functions

scripts = {}
if IS_WINDOWS:
Expand All @@ -95,22 +97,20 @@ def customizeFinishedPage( cfg, tkPkg, cliPkg ):
'start "" {textViewer} "@TargetDir@\{licenseName}"' )
openLicBatchScript = ExecutableScript( "openLic", script=([ # as line list
'start "" {textViewer} "@TargetDir@\{licenseName}"',
'(goto) 2>nul & del "%~f0"' # SELF DESTRUCT
QtIfwExternalOp.batchSelfDestructSnippet()
]) )
openLicPowerShellScript = ExecutableScript(
"openLic", extension="ps1", script=([ # as line list
'Start-Process -FilePath "{textViewer}" '
'-ArgumentList "@TargetDir@\{licenseName}"',
'Remove-Item $script:MyInvocation.MyCommand.Path -Force' # SELF DESTRUCT
QtIfwExternalOp.powerShellSelfDestructSnippet()
]) )
openLicVbScript = ExecutableScript(
"openLic", extension="vbs", script=([ # as line list
'Set oShell = WScript.CreateObject("WScript.Shell")',
'oShell.Run "{textViewer} ""@TargetDir@\{licenseName}"""',
'Set oShell = Nothing',
'Set oFSO = CreateObject("Scripting.FileSystemObject")', # SELF DESTRUCT
'oFSO.DeleteFile WScript.ScriptFullName', # |
'Set oFSO = Nothing' # V
QtIfwExternalOp.vbScriptSelfDestructSnippet() # V
]) )
scripts.update( { SHELL: openLicBatchScript,
POWERSHELL: openLicPowerShellScript,
Expand Down Expand Up @@ -163,7 +163,7 @@ def customizeFinishedPage( cfg, tkPkg, cliPkg ):
argList=[joinPathQtIfw( QT_IFW_TARGET_DIR, licenseName )] )
# TODO: Fix this! (It's broken on Windows at least...)
openLicViaCmdCheckbox = QtIfwOnFinishedCheckbox(
"openLicViaCommand", text="Open License w/ Command now.",
"openLicViaCommand", text="Open License w/ Command now. (broken!)",
shellCmd=openLicCommand )
openLicViaScriptCheckbox = QtIfwOnFinishedCheckbox(
"openLicViaScript", text="Open License w/ Script now.",
Expand Down Expand Up @@ -236,7 +236,7 @@ def showIfInstalled( checkbox, pkg, isChecked=True ):
"createExampleFile", QtIfwOnFinishedDetachedExec.ON_INSTALL,
script = ExecutableScript( "createFile", script=(
['echo. > "%s"' % (EXAMPLE_FILE_PATH,)
,'(goto) 2>nul & del "%~f0"']
,QtIfwExternalOp.batchSelfDestructSnippet()]
if IS_WINDOWS else
['touch "%s"' % (EXAMPLE_FILE_PATH,)
,'']) # TODO: Add self-destruction
Expand All @@ -246,7 +246,7 @@ def showIfInstalled( checkbox, pkg, isChecked=True ):
"removeExampleFile", QtIfwOnFinishedDetachedExec.ON_UNINSTALL,
script = ExecutableScript( "removeFile", script=(
['del /q "%s"' % (EXAMPLE_FILE_PATH,)
,'(goto) 2>nul & del "%~f0"']
,QtIfwExternalOp.batchSelfDestructSnippet()]
if IS_WINDOWS else
['rm "%s"' % (EXAMPLE_FILE_PATH,)
,'']) # TODO: Add self-destruction
Expand Down

0 comments on commit 6d43a07

Please sign in to comment.