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