Changeset 4099

Show
Ignore:
Timestamp:
02/17/09 21:48:49 (4 years ago)
Author:
gpolo
Message:

Adjusted umit_scheduler to run as a Windows service under py2exe

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/install_scripts/windows/setup.py

    r4090 r4099  
    167167      zipfile = None, 
    168168      cmdclass = {"py2exe": umit_py2exe}, 
    169       service = ['umit_scheduler'], 
     169      service = [{'modules': ['umit_scheduler'], 'cmdline_style': 'custom'}], 
    170170      windows = [{ 
    171171          "script": "umit", 
  • trunk/umit_scheduler.py

    r4088 r4099  
    4040HOME_CONF = None 
    4141RUNNING_FILE = None 
    42  
     42if hasattr(sys, 'frozen'): 
     43    FROZEN_CFG = os.path.join(os.path.dirname(sys.path[0]), ".scheduserhome") 
     44else: 
     45    FROZEN_CFG = None 
    4346 
    4447class UMITSchedulerWinService(WindowsService): 
     
    4649    _svc_display_name_ = "%s service" % _svc_name_ 
    4750    _svc_description_ = _svc_display_name_ 
    48     _exe_args_ = None # This is defined at bottom 
     51    _exe_args_ = None # This is defined at bottom (when not using py2exe) 
    4952 
    5053    def __init__(self, args): 
     
    5356    def run(self): 
    5457        # _exe_args_ will be our sys.argv when this runs as a Windows service 
    55         Scheduler.main('start', winhndl=self.hndl_waitstop, *sys.argv[1:]) 
     58        # as long as we don't run under py2exe. 
     59        if FROZEN_CFG is not None: 
     60            cfg = open(FROZEN_CFG, 'r') 
     61            home_path = cfg.read() 
     62            cfg.close() 
     63            args = (sys.path[0], home_path) 
     64        else: 
     65            args = sys.argv[1:] 
     66        Scheduler.main('start', winhndl=self.hndl_waitstop, *args) 
    5667 
    5768 
     
    7283    Show help 
    7384    """ 
    74     try: 
    75         program_name = __file__ 
    76     except AttributeError: 
    77         if hasattr(sys, 'frozen'): 
    78             program_name = sys.executable 
    79         else: 
    80             program_name = sys.argv[0] 
    8185    print (_("Usage:") + 
    82             (" %s start|stop|cleanup|running <config_dir>" % program_name)) 
     86            (" %s start|stop|cleanup|running <config_dir>" % __file__)) 
    8387 
    8488 
     
    106110        return 1 
    107111 
    108 if __name__ == "__main__": 
     112 
     113def pre_main(): 
    109114    if len(sys.argv) < 2 or len(sys.argv) > 3: 
    110115        usage() 
    111         sys.exit(0) 
     116        return 0 
    112117 
    113118    if CONFIG_DIR: # forcing especified dir 
     
    119124            setup_homedir(os.path.join(os.path.expanduser("~"), '.umit')) 
    120125 
    121     sys.exit(main(sys.argv[1:])) 
     126    return main(sys.argv[1:]) 
     127 
     128 
     129if FROZEN_CFG is not None: 
     130    def write_frozen_cfg(): 
     131        setup_homedir(os.path.join(os.path.expanduser('~'), '.umit')) 
     132        conf = open(FROZEN_CFG, 'w') 
     133        conf.write(HOME_CONF) 
     134        conf.close() 
     135 
     136    import win32serviceutil 
     137    # HandleCommandLine is used by py2exe when defining a service with 
     138    # cmdline_style as 'custom' 
     139    def HandleCommandLine(): 
     140        # XXX I will need the user home before starting the Scheduler, 
     141        # I wish changing UMITSchedulerWinService._exe_args_ would work 
     142        # here too, but it doesn't. The workaround here is far from 
     143        # ideal. 
     144        if sys.argv[1] == 'install': 
     145            write_frozen_cfg() 
     146        elif sys.argv[1] in ('start', 'debug'): 
     147            if not os.path.isfile(FROZEN_CFG): 
     148                write_frozen_cfg() 
     149 
     150        win32serviceutil.HandleCommandLine(UMITSchedulerWinService) 
     151 
     152if __name__ == "__main__": 
     153    sys.exit(pre_main())