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