Example #1
0
def checkConf(fpath, ftemplate, remove=[], keep=[], update=False,notify=False, compare=False):
    """Check that all the variables in the template are in the config file too"""
    # Remove from the checks the sections in "remove", and if "keep"
    # is used only check those sections.

    # Read the config file fpath and the template ftemplate
    cf = ConfigParser()
    cf.optionxform = str  # keep case (stackoverflow.com/questions/1611799)
    assert cf.read(fpath) != [], 'Missing file %s' % fpath
    ct = ConfigParser()
    ct.optionxform = str
    assert ct.read(ftemplate) != [], 'Missing file %s' % ftemplate

    # Keep only the sections we want to compare from the files.
    for section in set(remove) - set(keep):
        ct.remove_section(section)
        cf.remove_section(section)
    if keep:
        for section in set(ct.sections()) - set(keep):
            ct.remove_section(section)
            cf.remove_section(section)

    df = dict([(s, set(cf.options(s))) for s in cf.sections()])
    dt = dict([(s, set(ct.options(s))) for s in ct.sections()])
    # That funny syntax to create the dictionaries works with old pythons.

    if compare:
        compareConfig(cf, ct, fpath, ftemplate)
        return

    confChanged = False

    if df == dt:
        print(green("All the expected sections and options found in " + fpath))
    else:
        print("Found differences between the configuration file\n  %s\n"
              "and the template file\n  %s" % (fpath, ftemplate))
        sf = set(df.keys())
        st = set(dt.keys())
        for s in sf - st:
            print("Section %s exists in the configuration file but "
                  "not in the template." % red(s))

        for s in st - sf:
            print("Section %s exists in the template but not in the configuration file. Use %s parameter to update  "
                  "local config files." % (yellow(s), UPDATE_PARAM))

            if update:
                # Update config file with missing section
                cf.add_section(s)
                # add it to the keys
                sf.add(s)
                df[s] = set()
                print("Section %s added to your config file."
                      % green(s))
                confChanged = True

        for s in st & sf:
            for o in df[s] - dt[s]:
                print("In section %s, option %s exists in the configuration "
                      "file but not in the template." % (red(s), red(o)))
            for o in dt[s] - df[s]:
                print("In section %s, option %s exists in the template but not in the configuration file. Use %s "
                      "parameter to update local config files." % (yellow(s), yellow(o), UPDATE_PARAM))

                if update:
                    if s=='VARIABLES' and o=='SCIPION_NOTIFY':
                        checkNotify(ct, notify=notify)
                    # Update config file with missing variable
                    value = ct.get(s, o)
                    cf.set(s, o, value)
                    confChanged = True
                    print("Variable %s -> %s added and set to %s in your config file."
                          % (s, green(o), value))

    if update:
        if confChanged:
            print("Changes detected: writing changes into %s. Please check values." % (fpath))
        else:
            print("Update requested no changes detected for %s." % (fpath))

        if 'PACKAGES' in cf._sections:
            # Order the content of packages section alphabetically
            print("Sorting packages section for %s." %(fpath))
            cf._sections['PACKAGES'] = collections.OrderedDict(
                sorted(cf._sections['PACKAGES'].items(), key=lambda t: t[0]))

        try:
            with open(fpath, 'wb') as f:
                cf.write(f)
        except Exception as e:
            print("Could not update the config: ", e)