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