1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#!/usr/bin/env python
# Copyright (C) 2017-2020 CS GROUP - France. All Rights Reserved.
# Author: Yoann Vandoorselaere <yoannv@gmail.com>
#
# This file is part of the Prewikka program.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import absolute_import, division, print_function, unicode_literals
import gevent.monkey
gevent.monkey.patch_all()
import argparse
import base64
import fcntl
import locale
import os
import signal
import sys
from prewikka import crontab, localization, main, siteconfig, version
from prewikka import FakeRequest
_LOCKFD = None
_LOCKFILE = None
def _get_lock_filename(options):
return "/tmp/prewikka-crontab-%s.lock" % base64.urlsafe_b64encode(options.config)
def _handle_signal(signum, frame):
env.log.info("received signal %d: terminating" % (signum))
if _LOCKFILE:
os.unlink(_LOCKFILE)
sys.exit(0)
def _daemonize(options):
global _LOCKFD, _LOCKFILE
_LOCKFILE = _get_lock_filename(options)
_LOCKFD = open(_LOCKFILE, 'w')
try:
fcntl.flock(_LOCKFD, fcntl.LOCK_EX | fcntl.LOCK_NB)
except Exception:
env.log.error("%s is already locked, is prewikka-crontab already running ?" % _LOCKFILE)
raise
ret = os.fork()
if ret != 0:
sys.exit(0)
os.setsid()
nd = open(os.devnull, 'rw')
for i in ("stdin", "stdout", "stderr"):
getattr(sys, i).close()
setattr(sys, i, nd)
os.umask(0o27)
os.chdir('/')
_LOCKFD.write('%s\n' % (os.getpid()))
def set_locale(lang):
if lang[0] not in localization.get_languages():
lang = "en_GB.utf8"
else:
lang = ".".join(lang)
localization.translation.set_locale(lang)
if __name__ == "__main__":
set_locale(locale.getdefaultlocale())
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-c", "--config", default="%s/prewikka.conf" % siteconfig.conf_dir, help=_("configuration file to use (default: %(default)s)"))
parser.add_argument("-d", "--daemon", action="store_true", help=_("run as a system daemon"))
parser.add_argument("-h", "--help", action="help", help=_("show this help message and exit"))
parser.add_argument("-v", "--version", action="version", version=version.__version__, help=_("show program's version number and exit"))
options = parser.parse_args()
if options.daemon:
_daemonize(options)
signal.signal(signal.SIGINT, _handle_signal)
signal.signal(signal.SIGTERM, _handle_signal)
# Setup the environment
core = main.Core.from_config(options.config)
env.request = FakeRequest()
crontab.run(core)
|