Skip to content

Commit

Permalink
python_service_by_wix does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
marbx committed Sep 18, 2021
1 parent 2ecb4db commit 8695d10
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions python_service_by_wix/FoobarAppl10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
29 changes: 29 additions & 0 deletions python_service_by_wix/Product.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version='1.0' encoding='UTF-8'?>
<Wix
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
<Product
Name='Python Service by WiX'
Version='0.1.0'
Manufacturer='marbx'
Language='1033'
UpgradeCode='a26bc2d3-d9a8-44cb-916c-2c8270fe178e'
Id='*'
>
<Package InstallerVersion='450' Compressed='yes' InstallScope="perMachine"/>
<MediaTemplate EmbedCab="yes"/>
<Directory Id='TARGETDIR' Name='SourceDir'>
<Directory Id='ProgramFiles64Folder' Name='PFiles'>
<Directory Id='Mare' Name='Mare'>
<Component Id='ReadMe.component'>
<File Id='ReadMe.fileID' Name='FoobarAppl10.txt' Source='FoobarAppl10.txt' KeyPath='yes'/>
</Component>
</Directory>
</Directory>
</Directory>
<Feature Id='Complete'>
<ComponentRef Id='ReadMe.component'/>
<ComponentGroupRef Id="service" /> <!-- Includes service.wxs in this directory -->
</Feature>
</Product>
</Wix>
47 changes: 47 additions & 0 deletions python_service_by_wix/Python_service_by_pywin32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket




class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "Python_service_by_pywin32"
_svc_display_name_ = "Python_service_by_pywin32"
STRFTIME = '%Y-%m-%dT%H:%M:%S'
run = 0

def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)


def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)


def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
#self.main()


def main(self):
import time
import datetime

while self.run < 900:
self.run += 1
now = datetime.datetime.now().strftime(self.STRFTIME)
print(now)
with open('/temp/Python_service_by_pywin32', 'a') as f:
f.write(f"{now}\n")
time.sleep(5)

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
10 changes: 10 additions & 0 deletions python_service_by_wix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```
pip install pywin32
python c:\git\LearnWix3\python_service_by_wix\Python_service_by_pywin32.py start
Starting service Python_service_by_pywin32
Error starting service: The service did not respond to the start or control request in a timely fashion.
```
47 changes: 47 additions & 0 deletions python_service_by_wix/Service.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
<Fragment>
<ComponentGroup Id="service">
<Component Directory="TARGETDIR" Guid="{9972ea7e-b2ab-40d5-8566-fe007374a3ad}">
<File Id="python.exe" KeyPath="yes" Source="C:\somewhere\python.exe" />
<ServiceInstall
Account="LocalSystem"
ErrorControl="normal"
Name="python-service-by-wix"
Start="auto"
Type="ownProcess"
Vital="yes"
Description="python-service-by-wix"
DisplayName="python-service-by-wix"
Id="python-service-by-wix">
<util:ServiceConfig
FirstFailureActionType="none"
SecondFailureActionType="none"
ThirdFailureActionType="none" />
</ServiceInstall>
<ServiceControl
Id="python-service-by-wix"
Name="python-service-by-wix"
Remove="uninstall"
Stop="both"
Start="install"
Wait="yes">
<ServiceArgument />
</ServiceControl>
<CreateFolder />
<util:EventSource
Log="Application"
Name="python-service-by-wix"
EventMessageFile="[#python.exe]" />
<RegistryKey Root="HKLM" Key="System\CurrentControlSet\Services\python-service-by-wix">
<RegistryKey Key="Parameters">
<RegistryValue Type="expandable" Name="AppDirectory" Value="C:\somewhere\" />
<RegistryValue Type="expandable" Name="Application" Value="C:\somewhere\python.exe" />
<RegistryValue Type="expandable" Name="AppParameters" Value="C:\somewhere\doesnotexist.py" />
</RegistryKey>
</RegistryKey>
</Component>
</ComponentGroup>
</Fragment>
</Wix>
1 change: 1 addition & 0 deletions python_service_by_wix/build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PowerShell -ExecutionPolicy RemoteSigned -File build.ps1
42 changes: 42 additions & 0 deletions python_service_by_wix/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Set-ExecutionPolicy RemoteSigned
Set-PSDebug -Strict
Set-strictmode -version latest

$msbuild = "C:\Program Files (x86)\MSBuild\14.0\" # Build tools 2015


function CheckExitCode($txt) {
if ($LastExitCode -ne 0) {
Write-Host -ForegroundColor Red "$txt failed"
exit(1)
}
}


Write-Host -ForegroundColor Yellow "Precompiling wxs to wixobj..."
& "$($ENV:WIX)bin\candle.exe" `
-nologo `
-sw1150 `
-arch x64 `
-ddist=".\" `
-ext "$($ENV:WIX)bin\WixUtilExtension.dll" `
-ext "$($ENV:WIX)bin\WixUIExtension.dll" `
-ext "$($ENV:WIX)bin\WixNetFxExtension.dll" `
Product.wxs `
Service.wxs
CheckExitCode "candle"


Write-Host -ForegroundColor Yellow "Linking wixobj to msi..."
& "$($ENV:WIX)bin\light" `
-out "$pwd\Product.msi" `
-pdbout "$pwd\Product.wixpdb" `
-cultures:null `
-ext "$($ENV:WIX)bin\WixUtilExtension.dll" `
-ext "$($ENV:WIX)bin\WixUIExtension.dll" `
-ext "$($ENV:WIX)bin\WixNetFxExtension.dll" `
-sice:ICE03 `
Service.wixobj `
Product.wixobj
CheckExitCode "light"

1 change: 1 addition & 0 deletions python_service_by_wix/clean.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PowerShell -ExecutionPolicy RemoteSigned -File clean.ps1
10 changes: 10 additions & 0 deletions python_service_by_wix/clean.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function silentdelelete ($path) {
if (Test-Path $path) {
Remove-Item -Recurse -Force $path
}
}

silentdelelete *.msi
silentdelelete *.log
silentdelelete *.wixpdb
silentdelelete *.wixobj
1 change: 1 addition & 0 deletions python_service_by_wix/install.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msiexec /i Product.msi /l*v Product-install.log
1 change: 1 addition & 0 deletions python_service_by_wix/uninstall.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msiexec /x Product.msi /qb! /l*v Product-uninstall.log

0 comments on commit 8695d10

Please sign in to comment.