tools/tryselect/preset.py
author Ashley Hauck <khyperia@mozilla.com>
Sat, 16 Feb 2019 00:20:22 +0000
changeset 517510 94a9c4cabe376cc22c9cd43199ee672bd32f0eae
parent 514594 205bf106e08444305d188c45b573e62cfc8b1cdc
child 518846 1b5a4da48de351e8f1145e3c3e49c4cd86cd9ad0
permissions -rw-r--r--
bug 1526031 - remove initOrStmt field from NameNode. r=jorendorff Differential Revision: https://phabricator.services.mozilla.com/D19054

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function, unicode_literals

import ConfigParser
import os
import subprocess

from mozboot.util import get_state_dir


CONFIG_PATH = os.path.join(get_state_dir(), "autotry.ini")


def list_presets(section=None):
    config = ConfigParser.RawConfigParser()

    data = []
    if config.read([CONFIG_PATH]):
        sections = [section] if section else config.sections()
        for s in sections:
            try:
                data.extend(config.items(s))
            except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
                pass

    if not data:
        print("No presets found")

    for name, value in data:
        print("%s: %s" % (name, value))


def edit_presets(section=None):
    if 'EDITOR' not in os.environ:
        print("error: must set the $EDITOR environment variable to use --edit-presets")
        return
    subprocess.call([os.environ['EDITOR'], CONFIG_PATH])


def load(name, section=None):
    config = ConfigParser.RawConfigParser()
    if not config.read([CONFIG_PATH]):
        return

    sections = [section] if section else config.sections()
    for s in sections:
        try:
            return config.get(s, name), s
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass
    return None, None


def save(section, name, data):
    config = ConfigParser.RawConfigParser()
    config.read([CONFIG_PATH])

    if not config.has_section(section):
        config.add_section(section)

    config.set(section, name, data)

    with open(CONFIG_PATH, "w") as f:
        config.write(f)

    print('preset saved, run with: --preset={}'.format(name))