Exemplo n.º 1
0
def parse_legacy_config(options):
    """Parse an old-style, shell-script configuration.

    This runs the config file (if it exists) in a shell.

    :param: A container of option names that should be read from the config.
    :return: A dict mapping variable names to values, both as unicode.
        Any options that are not set by the config are left out.  Options that
        are set to the empty string come out as empty strings.
    """
    if not os.path.exists(EPHEMERALS_LEGACY_CONFIG):
        return {}

    # Source the legacy settings file, and print the environment.  Use the nul
    # character as a separator, so we don't get confused by newlines in the
    # values.
    source_config = 'source %s >/dev/null' % quote(EPHEMERALS_LEGACY_CONFIG)
    export_vars = 'export ' + ' '.join(quote(var) for var in options)
    dump_env = 'env -0'
    shell_code = '; '.join([source_config, export_vars, dump_env])
    output = call_capture_and_check(['bash', '-c', shell_code])
    # Assume UTF-8 encoding.  If the system uses something else but the
    # variables are all ASCII, that's probably fine too.
    output = output.decode('utf-8')

    variables = dict(
        setting.split('=', 1) for setting in output.split('\0')
        if len(setting) > 0)

    return filter_dict(variables, options)
Exemplo n.º 2
0
def parse_legacy_config(options):
    """Parse an old-style, shell-script configuration.

    This runs the config file (if it exists) in a shell.

    :param: A container of option names that should be read from the config.
    :return: A dict mapping variable names to values, both as unicode.
        Any options that are not set by the config are left out.  Options that
        are set to the empty string come out as empty strings.
    """
    if not os.path.exists(EPHEMERALS_LEGACY_CONFIG):
        return {}

    # Source the legacy settings file, and print the environment.  Use the nul
    # character as a separator, so we don't get confused by newlines in the
    # values.
    source_config = 'source %s >/dev/null' % quote(EPHEMERALS_LEGACY_CONFIG)
    export_vars = 'export ' + ' '.join(quote(var) for var in options)
    dump_env = 'env -0'
    shell_code = '; '.join([source_config, export_vars, dump_env])
    output = call_capture_and_check(['bash', '-c', shell_code])
    # Assume UTF-8 encoding.  If the system uses something else but the
    # variables are all ASCII, that's probably fine too.
    output = output.decode('utf-8')

    variables = dict(
        setting.split('=', 1)
        for setting in output.split('\0')
        if len(setting) > 0)

    return filter_dict(variables, options)
Exemplo n.º 3
0
    def test_leaves_original_intact(self):
        desired_key = factory.make_name('key')
        original = {
            desired_key: factory.make_name('value'),
            factory.make_name('otherkey'): factory.make_name('othervalue'),
        }
        copy = original.copy()

        result = filter_dict(copy, {desired_key})

        self.assertEqual({desired_key: original[desired_key]}, result)
        self.assertEqual(original, copy)
Exemplo n.º 4
0
 def select(self, keys):
     """Select a subset of this mapping."""
     return filter_dict(self, frozenset(keys))
Exemplo n.º 5
0
    def test_ignores_values_from_second_dict(self):
        key = factory.make_name('key')
        items = {key: factory.make_name('value')}
        keys = {key: factory.make_name('othervalue')}

        self.assertEqual(items, filter_dict(items, keys))
Exemplo n.º 6
0
 def test_ignores_undesired_keys(self):
     items = {factory.make_name('key'): factory.make_name('value')}
     self.assertEqual({}, filter_dict(items, {factory.make_name('other')}))
Exemplo n.º 7
0
 def test_keeps_desired_keys(self):
     key = factory.make_name('key')
     value = factory.make_name('value')
     self.assertEqual({key: value}, filter_dict({key: value}, {key}))
Exemplo n.º 8
0
 def select(self, keys):
     """Select a subset of this mapping."""
     return filter_dict(self, frozenset(keys))