ngompa / rpms / cmake

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