root/trunk/umit_scheduler.py @ 4135

Revision 4135, 4.5 kB (checked in by gpolo, 4 years ago)

* Explicitly fail when a path has no change to succeed in
Path.force_set_umit_conf;
* No longer always using force_set_umit_conf in umit_scheduler, that
only happens when explicitly told so.

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2007 Insecure.Com LLC.
5#
6# Author: Guilherme Polo <ggpolo@gmail.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22"""
23Core Scheduler Controller
24"""
25
26# If you want to start Scheduler at system startup, please, especify here
27# umit config dir. XXX outdated comment
28# CONFIG_DIR = /home/myuser/.umit
29CONFIG_DIR = ''
30
31import os
32import sys
33import signal
34
35from umitCore.BGProcess import WindowsService
36from umitCore import Scheduler
37from umitCore.I18N import _
38from umitCore.Paths import Path
39
40HOME_CONF = None
41RUNNING_FILE = None
42if hasattr(sys, 'frozen'):
43    FROZEN_CFG = os.path.join(os.path.dirname(sys.path[0]), ".scheduserhome")
44else:
45    FROZEN_CFG = None
46
47class UMITSchedulerWinService(WindowsService):
48    _svc_name_ = 'umit-scheduler'
49    _svc_display_name_ = "%s service" % _svc_name_
50    _svc_description_ = _svc_display_name_
51    _exe_args_ = None # This is defined at bottom (when not using py2exe)
52
53    def __init__(self, args):
54        WindowsService.__init__(self, args)
55
56    def run(self):
57        # _exe_args_ will be our sys.argv when this runs as a Windows service
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)
67
68
69def setup_homedir(usethis, force=False):
70    """
71    Setting umit home directory.
72    """
73    if force:
74        Path.force_set_umit_conf(usethis)
75    else:
76        Path.set_umit_conf(usethis)
77
78    global HOME_CONF, RUNNING_FILE
79
80    HOME_CONF = os.path.split(Path.get_umit_conf())[0]
81    RUNNING_FILE = os.path.join(HOME_CONF, 'schedrunning')
82
83
84def usage():
85    """
86    Show help
87    """
88    print (_("Usage:") +
89            (" %s start|stop|cleanup|running <config_dir>" % __file__))
90
91
92def main(args, verbose=True):
93    # Quoting paths since they may contain spaces
94    UMITSchedulerWinService._exe_args_ = '"%s" "%s"' % (sys.path[0], HOME_CONF)
95
96    schedcontrol = Scheduler.SchedulerControl(RUNNING_FILE, HOME_CONF,
97            verbose, UMITSchedulerWinService)
98    cmds = {"start": schedcontrol.start,
99            "stop": schedcontrol.stop,
100            "cleanup": schedcontrol.cleanup,
101            "running": schedcontrol.running
102            }
103
104    cmd_args = ()
105    try:
106        if args[0] == 'cleanup':
107            cmd_args += (True, )
108        return cmds[args[0]](*cmd_args)
109    except KeyError, e:
110        if verbose:
111            print "Invalid command especified: %s" % e
112            usage()
113        return 1
114
115
116def pre_main():
117    if len(sys.argv) < 2 or len(sys.argv) > 3:
118        usage()
119        return 0
120
121    if CONFIG_DIR: # forcing especified dir
122        setup_homedir(CONFIG_DIR, force=True)
123    else:
124        try:
125            setup_homedir(sys.argv[2], force=True)
126        except IndexError: # no path especified
127            setup_homedir(os.path.expanduser("~"))
128
129    return main(sys.argv[1:])
130
131
132if FROZEN_CFG is not None:
133    def write_frozen_cfg():
134        setup_homedir(os.path.expanduser('~'))
135        conf = open(FROZEN_CFG, 'w')
136        conf.write(HOME_CONF)
137        conf.close()
138
139    import win32serviceutil
140    # HandleCommandLine is used by py2exe when defining a service with
141    # cmdline_style as 'custom'
142    def HandleCommandLine():
143        # XXX I will need the user home before starting the Scheduler,
144        # I wish changing UMITSchedulerWinService._exe_args_ would work
145        # here too, but it doesn't. The workaround here is far from
146        # ideal.
147        if sys.argv[1] == 'install':
148            write_frozen_cfg()
149        elif sys.argv[1] in ('start', 'debug'):
150            if not os.path.isfile(FROZEN_CFG):
151                write_frozen_cfg()
152
153        win32serviceutil.HandleCommandLine(UMITSchedulerWinService)
154
155if __name__ == "__main__":
156    sys.exit(pre_main())
Note: See TracBrowser for help on using the browser.