summaryrefslogtreecommitdiff
path: root/packaging/cmake.prov
blob: 3a45753f7c118e695fb3e188591e6ed4aeae7e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
# -*- coding:utf-8 -*-
#
# Copyright (C) 2015 Daniel Vrátil <dvratil@redhat.com>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

import sys
import re
import glob

class CMakeParser:
    def __init__(self, filelist = None):
        if filelist == None:
            filelist = sys.stdin

        paths = map(lambda x: x.rstrip(), filelist.readlines())
        for path in paths:
            for (modulePath, cmakeModule, lowercase) in self.parseCmakeModuleConfig(path):
                version = self.resolveCMakeModuleVersion(modulePath, cmakeModule, lowercase)

                if version:
                   print("cmake(%s) = %s" % (cmakeModule, version))
                else:
                   print("cmake(%s)" % cmakeModule)

    def parseCmakeModuleConfig(self, configFile):
        paths = configFile.rsplit("/", 3)

        modulePath = "%s/cmake/%s" % (paths[0], paths[2])

        result = []
        for configFile in glob.glob("%s/*Config.cmake" % modulePath):
            moduleName = configFile[len(modulePath) + 1:-len("Config.cmake")]
            result.append( (modulePath, moduleName, False) )

        for configFile in glob.glob("%s/*-config.cmake" % modulePath):
            moduleName = configFile[len(modulePath) + 1:-len("-config.cmake")]
            if (modulePath, moduleName, False) not in result:
                result.append( (modulePath, moduleName, True) )

        return result

    def resolveCMakeModuleVersion(self, modulePath, cmakeModule, lowercase):
        versionFile = ("%s/%s-config-version.cmake" if lowercase else "%s/%sConfigVersion.cmake") % (modulePath, cmakeModule)
        try:
            f = open(versionFile, 'r')
        except:
            return None

        for line in f:
            line = line.strip()

            # set(PACKAGE_VERSION <version>)
            version = re.match(r"^set[\ ]*\([\ ]*PACKAGE_VERSION[\ ]+[\"]*([0-9\.]+)[\"]*[\ ]*[.]*\)", line)
            if version:
                return version.groups(1)[0]

        return None

if __name__ == "__main__":
    parser = CMakeParser()