forked from DentonGentry/gfiber-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.py
53 lines (41 loc) · 1.45 KB
/
experiment.py
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
#!/usr/bin/python -S
"""Python implementation of the experiment framework."""
import logging
import os
import subprocess
logger = logging.getLogger(__name__)
EXPERIMENTS_TMP_DIR = '/tmp/experiments'
EXPERIMENTS_DIR = '/fiber/config/experiments'
_experiment_warned = set()
_experiment_enabled = set()
def register(name):
try:
rv = subprocess.call(['register_experiment', name])
except OSError as e:
logger.info('register_experiment: %s', e)
else:
if rv:
logger.error('Failed to register experiment %s.', name)
def enabled(name):
"""Check whether an experiment is enabled.
Copy/pasted from waveguide/helpers.py.
Args:
name: The name of the experiment to check.
Returns:
Whether the experiment is enabled.
"""
if not os.path.exists(os.path.join(EXPERIMENTS_TMP_DIR,
name + '.available')):
if name not in _experiment_warned:
_experiment_warned.add(name)
logger.warning('Warning: experiment %r not registered.', name)
else:
is_enabled = os.path.exists(os.path.join(EXPERIMENTS_DIR,
name + '.active'))
if is_enabled and name not in _experiment_enabled:
_experiment_enabled.add(name)
logger.info('Notice: using experiment %r.', name)
elif not is_enabled and name in _experiment_enabled:
_experiment_enabled.remove(name)
logger.info('Notice: stopping experiment %r.', name)
return is_enabled