1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import
6import sys
7import os
8
9
10def path_join(*args):
11    path = os.path.join(*args)
12    return os.path.abspath(path)
13
14
15mozproxy_src_dir = os.path.dirname(os.path.realpath(__file__))
16mozproxy_dir = path_join(mozproxy_src_dir, "..")
17mozbase_dir = path_join(mozproxy_dir, "..")
18
19# needed so unit tests can find their imports
20if os.environ.get("SCRIPTSPATH", None) is not None:
21    # in production it is env SCRIPTS_PATH
22    mozharness_dir = os.environ["SCRIPTSPATH"]
23else:
24    # locally it's in source tree
25    mozharness_dir = path_join(mozbase_dir, "..", "mozharness")
26
27
28def get_playback(config):
29    """Returns an instance of the right Playback class"""
30    sys.path.insert(0, mozharness_dir)
31    sys.path.insert(0, mozproxy_dir)
32    sys.path.insert(0, mozproxy_src_dir)
33
34    from .server import get_backend
35    from .utils import LOG
36
37    tool_name = config.get("playback_tool", None)
38    if tool_name is None:
39        LOG.critical("playback_tool name not found in config")
40        return None
41    try:
42        return get_backend(tool_name, config)
43    except KeyError:
44        LOG.critical("specified playback tool is unsupported: %s" % tool_name)
45        return None
46