Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions circus/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import signal
import warnings

from fnmatch import fnmatch
try:
import resource
Expand Down Expand Up @@ -97,19 +98,17 @@ def rlimit_value(val):


def read_config(config_path):
cfg = DefaultConfigParser()
with open(config_path) as f:
if hasattr(cfg, 'read_file'):
cfg.read_file(f)
else:
cfg.readfp(f)

current_dir = os.path.dirname(config_path)

# load included config files
includes = []
def _init_config(config_path_, parser=DefaultConfigParser):
config = parser()
with open(config_path_) as f:
if hasattr(config, 'read_file'):
config.read_file(f)
else:
config.readfp(f)
return config

def _scan(filename, includes):
def _scan(filename, includes_):
if os.path.abspath(filename) != filename:
filename = os.path.join(current_dir, filename)

Expand All @@ -118,24 +117,47 @@ def _scan(filename, includes):
logger.warn('%r does not lead to any config. Make sure '
'include paths are relative to the main config '
'file' % filename)
includes += paths
includes_ += paths

for include_file in cfg.dget('circus', 'include', '').split():
_scan(include_file, includes)
def _get_includes(config, section_):
incl = []

for include_dir in cfg.dget('circus', 'include_dir', '').split():
_scan(os.path.join(include_dir, '*.ini'), includes)
for include_file in config.dget(section_, 'include', '').split():
_scan(include_file, incl)

for include_dir in config.dget(section_, 'include_dir', '').split():
_scan(os.path.join(include_dir, '*.ini'), incl)

return incl

current_dir = os.path.dirname(config_path)
cfg = _init_config(config_path)

# load included config files in circus section
includes = _get_includes(cfg, 'circus')

logger.debug('Reading config files: %s' % includes)
return cfg, [config_path] + cfg.read(includes)
cfg.read(includes)

# load included config files in watcher sections
for section in cfg.sections():
if not section.startswith('watcher:'):
continue
watcher_includes = _get_includes(cfg, section)
for include_path in watcher_includes:
included_cfg = _init_config(include_path)
for name, value in included_cfg.items('included'):
cfg.set(section, name, value)

return cfg


def get_config(config_file):
if not os.path.exists(config_file):
raise IOError("the configuration file %r does not exist\n" %
config_file)

cfg, cfg_files_read = read_config(config_file)
cfg = read_config(config_file)
dget = cfg.dget
config = {}

Expand Down
7 changes: 7 additions & 0 deletions circus/tests/config/issue1119.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[circus]
pidfile = pidfile

[watcher:server]
cmd = echo
include = issue1119_included.ini issue1119_included/*/*.ini
include_dir = issue1119_included
10 changes: 10 additions & 0 deletions circus/tests/config/issue1119_included.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[included]
numprocesses = 5
stdout_stream.class = FileStream
stdout_stream.filename = /var/logs/$(circus.wid).log
stdout_stream.max_bytes = 10000000
stdout_stream.backup_count = 10
stderr_stream.class = FileStream
stderr_stream.filename = /var/logs/$(circus.wid).log
stderr_stream.max_bytes = 1000000
stderr_stream.backup_count = 10
2 changes: 2 additions & 0 deletions circus/tests/config/issue1119_included/bar/baz.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[included]
stderr_stream.backup_count = 13
2 changes: 2 additions & 0 deletions circus/tests/config/issue1119_included/foo.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[included]
stderr_stream.max_bytes = 1000001
19 changes: 18 additions & 1 deletion circus/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
'issue680': os.path.join(CONFIG_DIR, 'issue680.ini'),
'virtualenv': os.path.join(CONFIG_DIR, 'virtualenv.ini'),
'empty_section': os.path.join(CONFIG_DIR, 'empty_section.ini'),
'issue1088': os.path.join(CONFIG_DIR, 'issue1088.ini')
'issue1088': os.path.join(CONFIG_DIR, 'issue1088.ini'),
'issue1119': os.path.join(CONFIG_DIR, 'issue1119.ini')
}


Expand Down Expand Up @@ -396,5 +397,21 @@ def test_issue1088(self):
watcher = Watcher.load_from_config(conf['watchers'][0])
watcher.stop()

def test_ssue_1119_include(self):
# #1119 - add support for include directive in watcher section
conf = get_config(_CONF['issue1119'])
watchers = conf['watchers']
# numprocesses is overriden in issue1119_included.ini
self.assertEqual(watchers[0]['numprocesses'], 5)
# this is to make sure wid is properly populated
self.assertEqual(
watchers[0]['stdout_stream']['filename'],
'/var/logs/$(circus.wid).log',
)
# this option is in the included directory
self.assertEqual(watchers[0]['stderr_stream']['max_bytes'], '1000001')
# this option is in the included sub-directory
self.assertEqual(watchers[0]['stderr_stream']['backup_count'], '13')


test_suite = EasyTestSuite(__name__)