Exemplo n.º 1
0
def _load_config_yaml(filepath):
    with open(filepath) as f:
        content = f.read()
    try:
        doc = yaml.load(content, Loader=yaml.FullLoader) or {}
    except YAMLError as e:
        raise ConfigurationError("Error loading configuration from %s: %s"
                                 % (filepath, str(e)))

    if not isinstance(doc, dict):
        raise ConfigurationError("Error loading configuration from %s: Expected "
                                 "dict, got %s" % (filepath, type(doc).__name__))
    return doc
Exemplo n.º 2
0
def _load_config_py(filepath):
    reserved = dict(
        # Standard Python module variables
        # Made available from within the module,
        # and later excluded from the `Config` class
        __name__=os.path.splitext(os.path.basename(filepath))[0],
        __file__=filepath,
        rez_version=__version__,
        ModifyList=ModifyList,
        DelayLoad=DelayLoad)

    g = reserved.copy()
    result = {}

    with open(filepath) as f:
        try:
            code = compile(f.read(), filepath, 'exec')
            exec(code, g)
        except Exception as e:
            raise ConfigurationError(
                "Error loading configuration from %s: %s" % (filepath, str(e)))

    for k, v in g.items():
        if k != '__builtins__' \
                and not ismodule(v) \
                and k not in reserved:
            result[k] = v

    return result
Exemplo n.º 3
0
    def __getattr__(self, attr):
        if attr in self.__dict__:
            return self.__dict__[attr]

        data = self.__dict__['_data']
        from rez.plugin_managers import plugin_manager
        if attr in plugin_manager.get_plugin_types():
            # get plugin config data, and apply overrides
            plugin_type = attr
            config_data = plugin_manager.get_plugin_config_data(plugin_type)
            d = copy.deepcopy(config_data)
            deep_update(d, data.get(plugin_type, {}))

            # validate
            schema = plugin_manager.get_plugin_config_schema(plugin_type)
            try:
                d = schema.validate(d)
            except SchemaError as e:
                raise ConfigurationError(
                    "Error in Rez configuration under plugins.%s: %s" %
                    (plugin_type, str(e)))
        elif attr in data:
            d = data[attr]
        else:
            raise AttributeError(
                "No such configuration setting: 'plugins.%s'" % attr)
        d_ = convert_dicts(d, RO_AttrDictWrapper)
        self.__dict__[attr] = d_
        return d_
Exemplo n.º 4
0
 def _parse_env_var(self, value):
     items = value.split(",")
     try:
         return dict([item.split(":") for item in items])
     except ValueError:
         raise ConfigurationError(
             "expected dict string in form 'k1:v1,k2:v2,...kN:vN': %s" %
             value)
Exemplo n.º 5
0
 def validate(self, data):
     try:
         data = self._validate(data)
         data = self.schema.validate(data)
         data = expand_system_vars(data)
     except SchemaError as e:
         raise ConfigurationError("Misconfigured setting '%s': %s" %
                                  (self.key, str(e)))
     return data
Exemplo n.º 6
0
 def _parse_env_var(self, value):
     value = value.lower()
     if value in self.true_words:
         return True
     elif value in self.false_words:
         return False
     else:
         raise ConfigurationError(
             "Expected $%s to be one of: %s" %
             (self._env_var_name, ", ".join(self.all_words)))
Exemplo n.º 7
0
    def parse_rule(cls, txt):
        """Parse a rule from a string.

        See rezconfig.package_filter for an overview of valid strings.

        Args:
            txt (str): String to parse.

        Returns:
            `Rule` instance.
        """
        types = {
            "glob": GlobRule,
            "regex": RegexRule,
            "range": RangeRule,
            "before": TimestampRule,
            "after": TimestampRule
        }

        # parse form 'x(y)' into x, y
        label, txt = Rule._parse_label(txt)
        if label is None:
            if '*' in txt:
                label = "glob"
            else:
                label = "range"
        elif label not in types:
            raise ConfigurationError(
                "'%s' is not a valid package filter type" % label)

        rule_cls = types[label]
        txt_ = "%s(%s)" % (label, txt)

        try:
            rule = rule_cls._parse(txt_)
        except Exception as e:
            raise ConfigurationError(
                "Error parsing package filter '%s': %s: %s" %
                (txt_, e.__class__.__name__, str(e)))
        return rule
Exemplo n.º 8
0
def _load_config_py(filepath):
    from rez.vendor.six.six import exec_

    globs = dict(rez_version=__version__)
    result = {}

    with open(filepath) as f:
        try:
            code = compile(f.read(), filepath, 'exec')
            exec_(code, _globs_=globs)
        except Exception, e:
            raise ConfigurationError(
                "Error loading configuration from %s: %s" % (filepath, str(e)))
Exemplo n.º 9
0
    def file_lock_dir(self):
        dirname = self.settings.file_lock_dir
        if not dirname:
            return None

        path = os.path.join(self.location, dirname)
        path_ = os.path.dirname(path)
        if os.path.realpath(path_) != os.path.realpath(self.location):
            raise ConfigurationError(
                "filesystem package repository setting 'file_lock_dir' must be "
                "a single relative directory such as '.lock'")

        return os.path.basename(path)
Exemplo n.º 10
0
    def file_lock_dir(self):
        dirname = _settings.file_lock_dir
        if not dirname:
            return None

        # sanity check
        if os.path.isabs(dirname) or os.path.basename(dirname) != dirname:
            raise ConfigurationError(
                "filesystem package repository setting 'file_lock_dir' must be "
                "a single relative directory such as '.lock'")

        # fall back to location path if lock dir doesn't exist.
        path = os.path.join(self.location, dirname)
        if not os.path.exists(path):
            return None

        return dirname
Exemplo n.º 11
0
def _load_config_py(filepath):
    from rez.vendor.six.six import exec_

    reserved = dict(
        # Standard Python module variables
        # Made available from within the module,
        # and later excluded from the `Config` class
        __name__=os.path.splitext(os.path.basename(filepath))[0],
        __file__=filepath,
        rez_version=__version__,
        ModifyList=ModifyList)

    globs = reserved.copy()
    result = {}

    with open(filepath) as f:
        try:
            code = compile(f.read(), filepath, 'exec')
            exec_(code, _globs_=globs)
        except Exception, e:
            raise ConfigurationError(
                "Error loading configuration from %s: %s" % (filepath, str(e)))
Exemplo n.º 12
0
    def _parse_env_var(self, value):
        items = value.split(",")
        result = {}

        for item in items:
            if ':' not in item:
                raise ConfigurationError(
                    "Expected dict string in form 'k1:v1,k2:v2,...kN:vN': %s" %
                    value)

            k, v = item.split(':', 1)

            try:
                v = int(v)
            except ValueError:
                try:
                    v = float(v)
                except ValueError:
                    pass

            result[k] = v

        return result
Exemplo n.º 13
0
    def _validate(self, data):
        # overridden settings take precedence. Note that `data` has already
        # taken override into account at this point
        if self.key in self.config.overrides:
            return data

        if not self.config.locked:

            # next, env-var
            value = os.getenv(self._env_var_name)
            if value is not None:
                return self._parse_env_var(value)

            # next, JSON-encoded env-var
            varname = self._env_var_name + "_JSON"
            value = os.getenv(varname)
            if value is not None:
                from rez.utils import json

                try:
                    return json.loads(value)
                except ValueError:
                    raise ConfigurationError(
                        "Expected $%s to be JSON-encoded string." % varname
                    )

        # next, data unchanged
        if data is not None:
            return data

        # some settings have a programmatic default
        attr = "_get_%s" % self.key
        if hasattr(self.config, attr):
            return getattr(self.config, attr)()

        # setting is None
        return None
Exemplo n.º 14
0
 def _parse_env_var(self, value):
     try:
         return float(value)
     except ValueError:
         raise ConfigurationError("Expected %s to be a float" %
                                  self._env_var_name)
Exemplo n.º 15
0
 def _parse_env_var(self, value):
     try:
         return int(value)
     except ValueError:
         raise ConfigurationError("expected %s to be an integer" %
                                  self._env_var_name)
Exemplo n.º 16
0
"""
Abstraction for PyQt/PySide import.
"""
import sys
from rez.config import config
from rez.exceptions import RezGuiQTImportError
from rez.utils.lint_helper import used

USE_PYSIDE = None
if config.use_pyside:
    if config.use_pyqt:
        from rez.exceptions import ConfigurationError
        raise ConfigurationError(
            "'use_pyside' and 'use_pyqt' are both enabled")
    USE_PYSIDE = True
elif config.use_pyqt:
    USE_PYSIDE = False

if USE_PYSIDE is None:
    if 'PyQt4' in sys.modules:
        USE_PYSIDE = False
    elif 'PySide' in sys.modules:
        USE_PYSIDE = True
    else:
        try:
            import PyQt4
            used(PyQt4)
            USE_PYSIDE = False
        except ImportError:
            try:
                import PySide