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