#!/usr/bin/python -O # # /usr/sbin/webapp-config # Python script for managing the deployment of web-based # applications # # Originally written for the Gentoo Linux distribution # # Copyright (c) 1999-2007 Authors # Released under v2 of the GNU GPL # # Author(s) Stuart Herbert # Renat Lumpau # Gunnar Wrobel # # ======================================================================== # ======================================================================== # Dependencies # ------------------------------------------------------------------------ import sys, os, os.path, re, socket, time try: # Python 3 import configparser if sys.version_info >= (3, 2): from configparser import ConfigParser as configparser_ConfigParser from configparser import ExtendedInterpolation else: from configparser import SafeConfigParser as configparser_ConfigParser except ImportError: # Python 2 import ConfigParser as configparser from ConfigParser import SafeConfigParser as configparser_ConfigParser import WebappConfig.server import WebappConfig.permissions as Perm import WebappConfig.wrapper as wrapper from argparse import ArgumentParser from WebappConfig.debug import OUT from WebappConfig.version import WCVERSION from WebappConfig.permissions import PermissionMap # ======================================================================== # BashParser class # ------------------------------------------------------------------------ class BashConfigParser(configparser_ConfigParser): _interpvar_match = re.compile(r"(%\(([^)]+)\)s|\$\{([^}]+)\})").match def __init__(self, defaults=None): self.error_action = 1 if sys.hexversion >= 0x3000000: configparser_ConfigParser.__init__(self, defaults, interpolation=ExtendedInterpolation()) else: configparser_ConfigParser.__init__(self, defaults) def on_error(self, action = 0): self.error_action = action def get(self, section, option, *args, **kwargs): try: return configparser_ConfigParser.get(self, section, option, *args, **kwargs) except Exception as e: error = '\nThere is a problem with your configuration file or' \ ' an environment variable.\n' \ 'webapp-config tried to read the variable "' + str(option) \ + '"\nand received the following error:\n\n' + str(e) + \ '\nPlease note that webapp-config is not written in bash ' \ 'anymore\nand that you cannot use the bash scripting feat' \ 'ures.' if self.error_action == 0: OUT.die(error) elif self.error_action == 1: OUT.warn(error) return '' def _interpolate_some(self, option, accum, rest, section, map, depth): if depth > configparser.MAX_INTERPOLATION_DEPTH: raise configparser.InterpolationDepthError(option, section, rest) while rest: p = rest.find("%") if p < 0: p = rest.find("$") if p < 0: accum.append(rest) return if p > 0: accum.append(rest[:p]) rest = rest[p:] # p is no longer used c = rest[1:2] OUT.debug('Parsing', 7) if c == "%" or c == "$": accum.append(c) rest = rest[2:] elif c == "(" or c == "{": m = self._interpvar_match(rest) if m is None: raise configparser.InterpolationSyntaxError(option, section, "bad interpolation variable reference %r" % rest) var = m.group(2) if not var: var = m.group(3) var = self.optionxform(var) rest = rest[m.end():] try: v = map[var] except KeyError: raise configparser.InterpolationMissingOptionError( option, section, rest, var) if "%" in v or "$" in v: self._interpolate_some(option, accum, v, section, map, depth + 1) else: accum.append(v) else: raise configparser.InterpolationSyntaxError( option, section, "'" + c + "' must be followed by '" + c + "', '{', or '(', found: %r" % (rest,)) OPTCRE = re.compile( r'(?P