Dear win32 experts

I am trying to write a win32 service for a paste/pylons application
which works well from the command line, however if I try to make it
into a service the application can no longer find "eggs" that it needs.
 I'm including the command line program (which works) and the winservice
(which noes not work) hopping that someone can point me the right
direction.  One that that I'm doing to complicate things is the eggs it
needs to find are not installed in the usual place, which for the
command line program simply means manipulating sys.path, however doing
the same thing for the windows service does not have the same result.

Any and all help is appreciated

Jose
#!C:\Python25\python.exe
import os
import sys


try:
    here = __file__
except NameError:
    # Python 2.2
    here = sys.argv[0]

location = os.path.join(os.path.dirname(here),'testApp', 'webapps')
apps = [os.path.join(location, x) for x in os.listdir(location)]

for a in apps:
    if a not in sys.path:
        sys.path.insert(0, a)

myargs = [
    'serve',
    '--reload',
    'development.ini'
]
for arg in myargs:
    if arg not in sys.argv:
        sys.argv.append(arg)


relative_paste = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(here))), 'paste')

if os.path.exists(relative_paste):
    sys.path.insert(0, os.path.dirname(relative_paste))

from paste.script import command

def run():
    command.run()

if __name__ == '__main__':
    run()
"""
The most basic (working) Windows service possible.
Requires Mark Hammond's pywin32 package.
Most of the code was taken from a  CherryPy 2.2 example of how to set up a 
service
"""
import pkg_resources
import win32serviceutil
from paste.script.serve import ServeCommand as Server
from paste.script import command

import os, sys, site
import ConfigParser

import win32service
import win32event


class DefaultSettings(object):
    def __init__(self):
        os.chdir(os.path.dirname(__file__))
        self.ini = r'development.ini'
        # create a config parser opject and populate it with the ini file
        c = ConfigParser.SafeConfigParser()
        c.read(self.ini)
        self.c = c

    def getDefaults(self):
        '''
        Check for and get the default settings
        '''
        if (
            (not self.c.has_section('winservice')) or
            (not self.c.has_option('winservice', 'service_name')) or
            (not self.c.has_option('winservice', 'service_display_name')) or
            (not self.c.has_option('winservice', 'service_description'))
            ):
            print 'setting defaults'
            self.setDefaults()
        service_name = self.c.get('winservice', 'service_name')
        service_display_name = self.c.get('winservice', 'service_display_name')
        service_description = self.c.get('winservice', 'service_description')
        iniFile = self.ini
        return service_name, service_display_name, service_description, iniFile

    def setDefaults(self):
        '''
        set and add the default setting to the ini file
        '''
        if not self.c.has_section('winservice'):
            self.c.add_section('winservice')
        self.c.set('winservice', 'service_name', 'WSCGIService')
        self.c.set('winservice', 'service_display_name', 'WSCGI windows 
service')
        self.c.set('winservice', 'service_description', 'WSCGI windows service')
        cfg = file(self.ini, 'wr')
        self.c.write(cfg)
        cfg.close()
        print '''
you must set the winservice section service_name, service_display_name,
and service_description options to define the service
in the %s file
''' % self.ini
        sys.exit()


class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""

    d = DefaultSettings()
    service_name, service_display_name, service_description, iniFile = 
d.getDefaults()

    _svc_name_ = service_name
    _svc_display_name_ = service_display_name
    _svc_description_ = service_description

    def __init__(self, args):
        location = os.path.join(os.path.dirname(__file__), 'testApp','webapps')
        apps = [os.path.join(location, x) for x in os.listdir(location) if 
os.path.isdir(os.path.join(location, x))]
        for a in apps:
            if a not in sys.path:
                sys.path.insert(0, a)
##        raise str(sys.path)
        win32serviceutil.ServiceFramework.__init__(self, args)
        # create an event that SvcDoRun can wait on and SvcStop
        # can set.
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        os.chdir(os.path.dirname(__file__))
        ## create a paste service and start it running
        self.s = Server(None)
        self.s.run([self.iniFile])
        win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # just kill the service
        del self.s
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(MyService)
_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to