| #!/usr/bin/python -S |
| |
| """Python implementation of the experiment framework.""" |
| |
| import logging |
| import os |
| import subprocess |
| |
| |
| EXPERIMENTS_TMP_DIR = '/tmp/experiments' |
| EXPERIMENTS_DIR = '/config/experiments' |
| |
| _experiment_warned = set() |
| _experiment_enabled = set() |
| |
| |
| def register(name): |
| try: |
| rv = subprocess.call(['register_experiment', name]) |
| except OSError as e: |
| logging.info('register_experiment: %s', e) |
| else: |
| if rv: |
| logging.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) |
| logging.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) |
| logging.info('Notice: using experiment %r.', name) |
| elif not is_enabled and name in _experiment_enabled: |
| _experiment_enabled.remove(name) |
| logging.info('Notice: stopping experiment %r.', name) |
| return is_enabled |