#!/usr/bin/env python
"""

Script to generate the JSON file with the list of versions

"""
import logging
import sys
from LbSoftConfDb2Clients.GenericClient import LbSoftConfDbBaseClient, \
    WRITE_ALLOWED_MODE


class LbSdbGenerateVersionJSON(LbSoftConfDbBaseClient):
    """ Update information about a project / version to say
    whether it was built with CMake or CMT"""

    def __init__(self, *args, **kwargs):
        LbSoftConfDbBaseClient.__init__(
            self, server_mode=WRITE_ALLOWED_MODE, *args, **kwargs)

    def main(self):
        """ Main method for bootstrap and parsing the options.
        It invokes the appropriate method and  """
        self.log = logging.getLogger()

        opts = self.options
        args = self.args
        if opts.debug:
            self.log.setLevel(logging.DEBUG)
        else:
            self.log.setLevel(logging.WARNING)

        known_tags = ["prod"]

        allvers = {}
        for tag in known_tags:
            tmplist = []
            for p in sorted(self.mConfDB.listTag(tag.upper())):
                tmplist.append(p)
            allvers[tag] = tmplist

        import json
        print(json.dumps(allvers, indent=2))


def main():
    sUsage = """%prog [-r] project version tag
    Sets the flag indicating which buildtool is used for the build"""
    s = LbSdbGenerateVersionJSON(usage=sUsage)
    sys.exit(s.run())


if __name__ == '__main__':
    main()
