Creating and managing a Windows service (part 4)

The final step is to "compile" everything into Windows executables using py2exe. For this I have the following setup.py file:

from distutils.core import setup
import py2exe
import sys

# If run without args, build executables, in quiet mode.
if len(sys.argv) == 1:
sys.argv.append("py2exe")
sys.argv.append("-q")

class Target:
def __init__(self, **kw):
self.__dict__.update(kw)
# for the versioninfo resources
self.version = "0.1.0"
self.company_name = "Pixelblaster Romania"
self.copyright = "Copyright 2006 Pixelblaster Romania"
self.name = "MyService service"

################################################################
# THE SERVICE
myservice_service = Target(
description = "MyService daemon",
version = "0.1.0",
modules = ["myservice"]
)


################################################################
# THE CONFIGURATOR
manifest_template = '''
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="5.0.0.0"
processorArchitecture="x86"
name="%(prog)s"
type="win32"
/>
<description>%(prog)s Program</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
'''

RT_MANIFEST = 24

service_configurator = Target(
description = "The Service configurator",
script = "service_configurator.py",
other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="configurator"))],
## icon_resources = [(1, "icon.ico")],
dest_base = "service_configurator")

#==============================
#CREATE SERVICE
create_service = Target(
description = "Creates the service",
script = "create_service.py",
dest_base = "create_service")

#==============================
#REMOVE SERVICE
remove_service = Target(
description = "Removes the service",
script = "remove_service.py",
dest_base = "remove_service")

################################################################
# THE SERVICE CONTROLER
################################################################

service_controler = Target(
description = "The configurator",
script = "service_controler.py",
other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="configurator"))],
## icon_resources = [(1, "icon.ico")],
dest_base = "service_controler")

excludes = ["pywin", "pywin.debugger", "pywin.debugger.dbgcon",
"pywin.dialogs", "pywin.dialogs.list"]

setup(
options = {"py2exe": {
"compressed": 1,
"optimize": 2,
"excludes": excludes}},
zipfile = "lib/shared.zip",
service = [service],
windows = [configurator, service_controler],
console = [create_service, remove_service]
)

I think that's it. All that is left is to wrap everything to a nice installer using InnoTools and just deploy it to the customer. :-)

Comments