示例#1
0
def presets(command_context):
    from tryselect.preset import MergedHandler

    # Create our handler using both local and in-tree presets. The first
    # path in this list will be treated as the 'user' file for the purposes
    # of saving and editing. All subsequent paths are 'read-only'. We check
    # an environment variable first for testing purposes.
    if os.environ.get("MACH_TRY_PRESET_PATHS"):
        preset_paths = os.environ["MACH_TRY_PRESET_PATHS"].split(os.pathsep)
    else:
        preset_paths = [
            os.path.join(get_state_dir(), "try_presets.yml"),
            os.path.join(command_context.topsrcdir, "tools", "tryselect",
                         "try_presets.yml"),
        ]

    return MergedHandler(*preset_paths)
示例#2
0
    def presets(self):
        if self._presets:
            return self._presets

        from tryselect.preset import MergedHandler

        # Create our handler using both local and in-tree presets. The first
        # path in this list will be treated as the 'user' file for the purposes
        # of saving and editing. All subsequent paths are 'read-only'. We check
        # an environment variable first for testing purposes.
        if os.environ.get('MACH_TRY_PRESET_PATHS'):
            preset_paths = os.environ['MACH_TRY_PRESET_PATHS'].split(os.pathsep)
        else:
            preset_paths = [
                os.path.join(get_state_dir(), 'try_presets.yml'),
                os.path.join(self.topsrcdir, 'tools', 'tryselect', 'try_presets.yml'),
            ]

        self._presets = MergedHandler(*preset_paths)
        return self._presets
示例#3
0
    def handle_presets(self, preset_action, save, preset, **kwargs):
        """Handle preset related arguments.

        This logic lives here so that the underlying selectors don't need
        special preset handling. They can all save and load presets the same
        way.
        """
        from tryselect.preset import MergedHandler, migrate_old_presets
        from tryselect.util.dicttools import merge

        # Create our handler using both local and in-tree presets. The first
        # path in this list will be treated as the 'user' file for the purposes
        # of saving and editing. All subsequent paths are 'read-only'. We check
        # an environment variable first for testing purposes.
        if os.environ.get('MACH_TRY_PRESET_PATHS'):
            preset_paths = os.environ['MACH_TRY_PRESET_PATHS'].split(
                os.pathsep)
        else:
            preset_paths = [
                os.path.join(get_state_dir(), 'try_presets.yml'),
                os.path.join(self.topsrcdir, 'tools', 'tryselect',
                             'try_presets.yml'),
            ]

        presets = MergedHandler(*preset_paths)
        user_presets = presets.handlers[0]

        # TODO: Remove after Jan 1, 2020.
        migrate_old_presets(user_presets)

        if preset_action == 'list':
            presets.list()
            sys.exit()

        if preset_action == 'edit':
            user_presets.edit()
            sys.exit()

        default = self.parser.get_default
        if save:
            selector = self.subcommand or self._mach_context.settings['try'][
                'default']

            # Only save non-default values for simplicity.
            kwargs = {k: v for k, v in kwargs.items() if v != default(k)}
            user_presets.save(save, selector=selector, **kwargs)
            print('preset saved, run with: --preset={}'.format(save))
            sys.exit()

        if preset:
            if preset not in presets:
                self.parser.error("preset '{}' does not exist".format(preset))

            name = preset
            preset = presets[name]
            selector = preset.pop('selector')
            preset.pop('description',
                       None)  # description isn't used by any selectors

            if not self.subcommand:
                self.subcommand = selector
            elif self.subcommand != selector:
                print("error: preset '{}' exists for a different selector "
                      "(did you mean to run 'mach try {}' instead?)".format(
                          name, selector))
                sys.exit(1)

            # Order of precedence is defaults -> presets -> cli. Configuration
            # from the right overwrites configuration from the left.
            defaults = {}
            nondefaults = {}
            for k, v in kwargs.items():
                if v == default(k):
                    defaults[k] = v
                else:
                    nondefaults[k] = v

            kwargs = merge(defaults, preset, nondefaults)

        return kwargs