Fichier d'inclusion "common" pour le plugin Python

C'est le fichier qui est inclu avant l'éxécution de n'importe quel script du
plugin Python.

darcs-hash:20071215224051-af139-e7d738d689273af7ba0abfa7caa4c0a38275d995.gz
This commit is contained in:
Jeremie Dimino 2007-12-15 23:40:51 +01:00
parent c2ac799061
commit e46fcd8d9a

71
etc/python/common.py Normal file
View file

@ -0,0 +1,71 @@
# -*- mode: utf-8 -*-
#
# common.py
# ---------
#
# Copyright (C) 2007 Jeremie Dimino <jeremie@dimino.org>
#
# This file 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 of the License, or
# (at your option) any later version.
#
# This file 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., 59 Temple Street #330, Boston, MA 02111-1307, USA.
'''Fichier inclu par tous les scripts du plugin Python de bcfg2'''
import sys
sys.path.append('/usr/scripts/gestion')
def comment(line):
'''Écrit un commentaire'''
out("# " + line + "\n")
def header(desc, mode="conf-unix"):
'''Écrit une en-tête de fichier avec un commentaire'''
comment("-*- mode: %s; coding: utf-8 -*-" % mode)
@#
@# Fichier gere par BCfg2
@#
lines = desc.split('\n')
if lines[-1] == "":
lines.pop()
for line in lines:
comment(line)
out("\n")
def var(name, value, doc=None):
'''Écrit la définition d'une variable'''
if doc: comment(doc)
out("%s = %s\n" % (name, value))
# Le nom d'hote court, la partie avant le premier '.'
hostname = metadata.hostname.split('.',1)[0]
# Le nom d'hote sur le vlan public
pubhostname = hostname + ".crans.org"
# Le nom d'hote sur le vlan
admhostname = hostname + ".adm.crans.org"
def has(group):
return group in metadata.groups
def yesno(b):
if b:
return "yes"
else:
return "no"
def onoff(b):
if b:
return "on"
else:
return "off"