0ee124
#!/usr/bin/python3
0ee124
# -*- coding:utf-8 -*-
0ee124
#
0ee124
# Copyright (C) 2015 Daniel Vrátil <dvratil@redhat.com>
0ee124
# Copyright (C) 2017 Daniel Vrátil <dvratil@fedoraproject.org>
0ee124
#
0ee124
#
0ee124
# This program is free software; you can redistribute it and/or modify
0ee124
# it under the terms of the GNU Library General Public License as
0ee124
# published by the Free Software Foundation; either version 2 of the
0ee124
# License, or (at your option) any later version.
0ee124
#
0ee124
# This program is distributed in the hope that it will be useful,
0ee124
# but WITHOUT ANY WARRANTY; without even the implied warranty of
0ee124
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0ee124
# GNU General Public License for more details.
0ee124
#
0ee124
# You should have received a copy of the GNU Library General Public
0ee124
# License along with this program; if not, write to the
0ee124
# Free Software Foundation, Inc.,
0ee124
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0ee124
#
0ee124
0ee124
import sys
0ee124
import re
0ee124
import glob
0ee124
0ee124
class CMakeParser:
0ee124
    def __init__(self, filelist = None):
0ee124
        if filelist == None:
0ee124
            filelist = sys.stdin
0ee124
0ee124
        paths = map(lambda x: x.rstrip(), filelist.readlines())
0ee124
        for path in paths:
0ee124
            modulePath, cmakeModule, lowercase = self.parseCmakeModuleConfig(path)
0ee124
            if modulePath and cmakeModule:
0ee124
                version = self.resolveCMakeModuleVersion(modulePath, cmakeModule, lowercase)
0ee124
0ee124
                if version:
0ee124
                    string = "cmake(" + cmakeModule + ") = " + version
0ee124
                else:
0ee124
                    string = "cmake(" + cmakeModule + ")"
0ee124
                if string == string.lower():
0ee124
                    print(string)
0ee124
                else:
0ee124
                    # Temporarily print both variants to satisfy requires
0ee124
                    # by the old version of this generator which made mistakes
0ee124
                    print(string)
0ee124
                    print(string.lower())
0ee124
0ee124
0ee124
    def parseCmakeModuleConfig(self, configFile):
0ee124
        paths = configFile.rsplit("/", 3)
0ee124
0ee124
        modulePath = "%s/cmake/%s" % (paths[0], paths[2])
0ee124
        cfgFile = paths[3]
0ee124
        if cfgFile.endswith("Config.cmake"):
0ee124
           return (modulePath, cfgFile[0:-len("Config.cmake")], False)
0ee124
        elif cfgFile.endswith("-config.cmake"):
0ee124
           return (modulePath, cfgFile[0:-len("-config.cmake")], True)
0ee124
        else:
0ee124
            return (None, None, False)
0ee124
0ee124
    def resolveCMakeModuleVersion(self, modulePath, cmakeModule, lowercase):
0ee124
        versionFile = ("%s/%s-config-version.cmake" if lowercase else "%s/%sConfigVersion.cmake") % (modulePath, cmakeModule)
0ee124
        try:
0ee124
            f = open(versionFile, 'r')
0ee124
        except:
0ee124
            return None
0ee124
0ee124
        for line in f:
0ee124
            line = line.strip()
0ee124
0ee124
            # set(PACKAGE_VERSION <version>)
0ee124
            version = re.match(r"^set[\ ]*\([\ ]*PACKAGE_VERSION[\ ]+[\"]*([0-9\.]+)[\"]*[\ ]*[.]*\)", line)
0ee124
            if version:
0ee124
                return version.groups(1)[0]
0ee124
0ee124
        return None
0ee124
0ee124
if __name__ == "__main__":
0ee124
    parser = CMakeParser()