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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
#
# Copyright (C) 2000, 2001, 2013 Gregory Trubetskoy
# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You
# may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
#
# Config maker, a la HTMLGen. This could grow into something useful.
#
from __future__ import print_function
import sys
import os
import shutil
# this is so that it could be referred to in a Container only_if
import mod_python
class Directive:
def __init__(self, name, val, flipslash=1):
self.name = name
self.val = val
self.indent = 0
self.flipslash = flipslash
def __repr__(self):
i = " " * self.indent
s = i + '%s(%s)' % (self.name, repr(self.val))
if self.flipslash:
s = s.replace("\\", "/")
return s
def __str__(self):
i = " " * self.indent
s = i + '%s %s\n' % (self.name, self.val)
if self.flipslash:
s = s.replace("\\", "/")
return s
class Container:
def __init__(self, *args, **kwargs):
self.args = list(args)
self.indent = 0
self.only_if = kwargs.get('only_if')
def append(self, value):
if not (isinstance(value, Directive) or
isinstance(value, Container) or
isinstance(value, ContainerTag) or
isinstance(value, Comment)):
raise TypeError("appended value must be an instance of Directive, Container, ContainerTag or Comment")
self.args.append(value)
def __repr__(self):
i = " " * self.indent
s = i + 'Container('
for arg in self.args:
arg.indent = self.indent + 4
s += "\n%s," % repr(arg)
s += "\n" + i + (self.only_if and (' only_if=%s)' % repr(self.only_if)) or ')')
return s
def __str__(self):
if self.only_if and not eval(self.only_if):
return ""
s = ""
for arg in self.args:
arg.indent = self.indent
s += "%s" % str(arg)
return s
class ContainerTag:
def __init__(self, tag, attr, args, flipslash=1):
self.tag = tag
self.attr = attr
self.args = args
self.indent = 0
self.flipslash = flipslash
def __repr__(self):
i = " " * self.indent
s = i + "%s(%s," % (self.tag, repr(self.attr))
if self.flipslash:
s = s.replace("\\", "/")
for arg in self.args:
arg.indent = self.indent + 4
s += "\n%s," % repr(arg)
s += "\n" + i + ")"
return s
def __str__(self):
i = " " * self.indent
s = i + "<%s %s>\n" % (self.tag, self.attr)
if self.flipslash:
s = s.replace("\\", "/")
for arg in self.args:
arg.indent = self.indent + 2
s += "%s" % str(arg)
s += i + "</%s>\n" % self.tag
return s
class Comment:
def __init__(self, comment):
self.comment = comment
self.indent = 0
def __repr__(self):
i = " " * self.indent
lines = self.comment.splitlines()
s = i + "Comment(%s" % repr(lines[0]+"\n")
for line in lines[1:]:
s += "\n " + i + repr(line+"\n")
s += ")"
return s
def __str__(self):
i = " " * self.indent
s = ""
for line in self.comment.splitlines():
s += i + '# %s\n' % line
return s
## directives
class AddHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class AddOutputFilter(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class AddType(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class AuthBasicAuthoritative(Directive):
# New in Apache 2.2
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class AuthBasicProvider(Directive):
# New in Apache 2.2
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class AuthType(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class AuthName(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class CustomLog(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Directory(ContainerTag):
def __init__(self, dir, *args):
ContainerTag.__init__(self, self.__class__.__name__, dir, args)
class DirectoryIndex(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class DocumentRoot(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class ErrorLog(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Files(ContainerTag):
def __init__(self, dir, *args):
ContainerTag.__init__(self, self.__class__.__name__, dir, args)
class IfModule(ContainerTag):
def __init__(self, dir, *args):
ContainerTag.__init__(self, self.__class__.__name__, dir, args)
class KeepAliveTimeout(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Listen(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class LoadModule(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Location(ContainerTag):
def __init__(self, dir, *args):
ContainerTag.__init__(self, self.__class__.__name__, dir, args)
class LogLevel(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class LogFormat(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val, flipslash=0)
class LockFile(Directive):
def __init__(self, val):
import sys
if sys.platform!='win32':
Directive.__init__(self, self.__class__.__name__, val)
else:
Directive.__init__(self, '#'+self.__class__.__name__, val)
class MaxConnectionsPerChild(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class MaxClients(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class MaxRequestsPerChild(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class MaxSpareServers(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class MaxSpareThreads(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class MaxThreadsPerChild(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class MinSpareThreads(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Mutex(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class NameVirtualHost(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class NumServers(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Options(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PidFile(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonAuthenHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonAuthzHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonCleanupHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonConnectionHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonDebug(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonAccessHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonPostReadRequestHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonTransHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonFixupHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonImport(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonPath(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val, flipslash=0)
class PythonOutputFilter(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonOption(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Require(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class SetHandler(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class ServerAdmin(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class ServerName(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class ServerPath(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class ServerRoot(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class StartServers(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class StartThreads(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class ThreadsPerChild(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class Timeout(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class TypesConfig(Directive):
def __init__(self, val):
Directive.__init__(self, self.__class__.__name__, val)
class PythonInterpPerDirectory(Directive):
def __init__(self, val='Off'):
Directive.__init__(self, self.__class__.__name__, val)
class PythonInterpPerDirective(Directive):
def __init__(self, val='Off'):
Directive.__init__(self, self.__class__.__name__, val)
class VirtualHost(ContainerTag):
def __init__(self, addr, *args):
ContainerTag.__init__(self, self.__class__.__name__, addr, args)
## utility functions
def quote_if_space(s):
# Windows doesn't like quotes when there are
# no spaces, but needs them otherwise,
# TODO: Is this still true?
if s.find(" ") != -1:
s = '"%s"' % s
return s
def write_basic_config(server_root, listen='0.0.0.0:8888', conf="conf", logs="logs",
htdocs="public", pythonhandler="mod_python.publisher",
pythonpath=[], pythonoptions=[], mp_comments=[],
conf_name='httpd_conf.py', createdirs=True, replace_config=False):
"""This generates a sensible Apache configuration"""
conf_path = os.path.join(server_root, conf, conf_name)
if os.path.exists(conf_path) and not replace_config:
print('Error: %s already exists, aborting.' % repr(conf_path), file=sys.stderr)
return
if createdirs:
for dirname in [server_root,
os.path.join(server_root, htdocs),
os.path.join(server_root, conf),
os.path.join(server_root, logs)]:
if os.path.isdir(dirname):
print("Warning: directory %s already exists, continuing." % repr(dirname), file=sys.stderr)
else:
print("Creating directory %s." % repr(dirname), file=sys.stderr)
os.mkdir(dirname)
# try to find mime.types
mime_types_dest = os.path.join(server_root, conf, 'mime.types')
if os.path.isfile(mime_types_dest):
print("Warning: file %s already exists, continuing." % repr(mime_types_dest), file=sys.stderr)
else:
for mime_types_dir in [mod_python.version.SYSCONFDIR, '/etc']:
mime_types_src = os.path.join(mime_types_dir, 'mime.types')
if os.path.isfile(mime_types_src):
print("Copying %s to %s" % (repr(mime_types_src), repr(mime_types_dest)), file=sys.stderr)
shutil.copy(mime_types_src, mime_types_dest)
break
mime_types = os.path.join(conf, "mime.types")
if not os.path.exists(os.path.join(server_root, mime_types)):
print("Warning: file %s does not exist." % repr(os.path.join(server_root, mime_types)), file=sys.stderr)
if not os.path.isdir(os.path.join(server_root, htdocs)):
print("Warning: %s does not exist or not a directory." % repr(os.path.join(server_root, htdocs)), file=sys.stderr)
modpath = mod_python.version.LIBEXECDIR
modules = Container(Comment("\nLoad the necessary modules (this is the default httpd set):\n\n"))
for module in [
['authn_file_module', 'mod_authn_file.so'],
['authn_core_module', 'mod_authn_core.so'],
['authz_host_module', 'mod_authz_host.so'],
['authz_groupfile_module', 'mod_authz_groupfile.so'],
['authz_user_module', 'mod_authz_user.so'],
['authz_core_module', 'mod_authz_core.so'],
['access_compat_module', 'mod_access_compat.so'],
['auth_basic_module', 'mod_auth_basic.so'],
['reqtimeout_module', 'mod_reqtimeout.so'],
['include_module', 'mod_include.so'],
['filter_module', 'mod_filter.so'],
['mime_module', 'mod_mime.so'],
['log_config_module', 'mod_log_config.so'],
['env_module', 'mod_env.so'],
['headers_module', 'mod_headers.so'],
['setenvif_module', 'mod_setenvif.so'],
['version_module', 'mod_version.so'],
['unixd_module', 'mod_unixd.so'],
['status_module', 'mod_status.so'],
['autoindex_module', 'mod_autoindex.so'],
['dir_module', 'mod_dir.so'],
['alias_module', 'mod_alias.so'],
]:
modules.append(
LoadModule("%s %s" % (module[0], quote_if_space(os.path.join(modpath, module[1]))))
)
main = Container(Comment("\nMain configuration options:\n\n"),
ServerRoot(server_root),
Container(MaxConnectionsPerChild('65536'),
only_if="mod_python.version.HTTPD_VERSION[0:3] == '2.4'"),
Container(MaxRequestsPerChild('65536'),
only_if="mod_python.version.HTTPD_VERSION[0:3] < '2.4'"),
LogFormat(r'"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined'),
CustomLog("%s combined" % quote_if_space(os.path.join(logs, "access_log"))),
ErrorLog(quote_if_space(os.path.join(logs, "error_log"))),
LogLevel("warn"),
PidFile(quote_if_space(os.path.join(logs, "httpd.pid"))),
TypesConfig(quote_if_space(mime_types)),
# The only reason we need a ServerName is so that Apache does not
# generate a warning about being unable to determine its name.
ServerName("127.0.0.1"),
Listen(listen),
DocumentRoot(quote_if_space(os.path.join(server_root, htdocs))),
Container(LockFile(quote_if_space(os.path.join(logs, "accept.lock"))),
only_if="mod_python.version.HTTPD_VERSION[0:3] == '2.2'"),
)
mp = Container(Comment("\nmod_python-specific options:\n\n"),
LoadModule("python_module %s" % quote_if_space(quote_if_space(os.path.join(modpath, 'mod_python.so')))),
SetHandler("mod_python"),
Comment("PythonDebug On"),
PythonHandler(pythonhandler),
)
if pythonpath:
pp = "sys.path+["
for p in pythonpath:
pp += repr(p)+","
pp += "]"
mp.append(PythonPath('"%s"' % pp))
for po in pythonoptions:
mp.append(PythonOption(po))
for c in mp_comments:
mp.append(Comment(c))
config = Container()
config.append(Comment(
"\n"
"This config was auto-generated, do not edit!\n"
"\n"
))
config.append(modules)
config.append(main)
config.append(mp)
s = """#!%s
#
# This config was auto-generated, but you can edit it!
# It can be used to generate an Apache config by simply
# running it. We recommend you run it like this:
# $ mod_python genconfig <this filename> > <new apache confg>
#\n
""" % mod_python.version.PYTHON_BIN
s += "from mod_python.httpdconf import *\n\n"
s += "config = " + repr(config)
s += "\n\nprint(config)\n"
print("Writing %s." % repr(conf_path), file=sys.stderr)
open(conf_path, 'w').write(s)
return conf_path
|