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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
# Deb-o-Matic
#
# Copyright (C) 2007-2014 Luca Falavigna
#
# Author: Luca Falavigna <dktrkranz@debian.org>
#
# 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; version 3 of the License.
#
# 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
import os
from configparser import ConfigParser
from argparse import ArgumentParser
from logging import basicConfig as log, debug, error, getLogger, warning
from logging import ERROR, WARNING, INFO, DEBUG
from time import sleep
from .build import FullBuild
from .commands import Command
from .modules import Module
from .process import Process, ThreadPool
class Debomatic(Process):
def __init__(self):
self.daemonize = True
self.setlog('%(levelname)s: %(message)s')
self.conffile = None
self.configvers = '013a'
self.opts = ConfigParser()
self.rtopts = ConfigParser()
parser = ArgumentParser()
parser.add_argument('-c', '--configfile', metavar='file', type=str,
nargs=1, help='configuration file for Deb-o-Matic')
parser.add_argument('-n', '--no-daemon', action='store_true',
help='do not launch Deb-o-Matic in daemon mode')
parser.add_argument('-q', '--quit-process', action='store_true',
help='terminate Deb-o-Matic processes')
args = parser.parse_args()
if os.getuid():
error(_('You must run Deb-o-Matic as root'))
exit(1)
if args.configfile:
self.conffile = args.configfile[0]
if args.no_daemon:
self.daemonize = False
self.default_options()
self.packagedir = self.opts.get('default', 'packagedir')
if not os.path.isdir(self.packagedir):
error(_('Unable to access %s directory') % self.packagedir)
exit(1)
self.pool = ThreadPool(self.opts.getint('default', 'maxbuilds'))
self.logfile = self.opts.get('default', 'logfile')
if args.quit_process:
self.shutdown()
exit()
self.setlog('%(levelname)s: %(message)s',
self.opts.get('default', 'loglevel'))
self.mod_sys = Module((self.opts, self.rtopts, self.conffile))
debug(_('Startup hooks launched'))
self.mod_sys.execute_hook('on_start', {})
debug(_('Startup hooks finished'))
try:
self.startup()
except RuntimeError:
error(_('Another instance is running, aborting'))
exit(1)
def default_options(self):
defaultoptions = ('builder', 'debootstrap', 'packagedir', 'configdir',
'architecture', 'maxbuilds', 'pbuilderhooks',
'inotify', 'sleep', 'logfile', 'loglevel')
if not self.conffile:
error(_('Configuration file has not been specified'))
exit(1)
if not os.path.exists(self.conffile):
error(_('Configuration file %s does not exist') % self.conffile)
exit(1)
self.opts.read(self.conffile)
if (not self.opts.has_option('internals', 'configversion') or
not self.opts.get('internals', 'configversion') ==
self.configvers):
error(_('Configuration file is not at version %s') %
self.configvers)
exit(1)
for opt in defaultoptions:
if (not self.opts.has_option('default', opt) or
not self.opts.get('default', opt)):
error(_('Set "%(opt)s" in %(conffile)s') %
{'opt': opt, 'conffile': self.conffile})
exit(1)
def launcher(self):
self.queue_files()
if self.opts.getint('default', 'inotify'):
try:
self.launcher_inotify()
except ImportError:
self.launcher_timer()
else:
self.launcher_timer()
def launcher_inotify(self):
import pyinotify
class PE(pyinotify.ProcessEvent, Debomatic):
def __init__(self, parent):
self.parent = parent
def process_IN_CLOSE_WRITE(self, event):
if (event.name.endswith('.changes') or
event.name.endswith('commands')):
self.parent.queue_files([event.name])
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, PE(self))
wm.add_watch(self.packagedir, pyinotify.IN_CLOSE_WRITE)
debug(_('Inotify loop started'))
notifier.loop()
def launcher_timer(self):
debug(_('Timer loop started'))
while True:
sleep(self.opts.getint('default', 'sleep'))
self.queue_files()
def queue_files(self, filelist=None):
if not filelist:
try:
filelist = os.listdir(self.packagedir)
except OSError:
error(_('Unable to access %s directory') % self.packagedir)
exit(1)
for filename in filelist:
if filename.endswith('.changes'):
b = FullBuild((self.opts, self.rtopts, self.conffile),
package=filename)
self.pool.schedule(b.run)
debug(_('Thread for %s scheduled') % filename)
elif filename.endswith('.commands'):
c = Command((self.opts, self.rtopts, self.conffile),
self.pool, filename)
c.process_command()
def setlog(self, fmt, level='info'):
loglevels = {'error': ERROR,
'warning': WARNING,
'info': INFO,
'debug': DEBUG}
level = level.lower()
if level not in loglevels:
warning(_('Log level not valid, defaulting to "info"'))
level = 'info'
self.loglevel = loglevels[level]
old_log = getLogger()
if old_log.handlers:
for handler in old_log.handlers:
old_log.removeHandler(handler)
log(level=self.loglevel, format=fmt)
|