348aeb
#!/usr/bin/python3
2576bb
# -*- coding:utf-8 -*-
2576bb
#
2576bb
# Copyright (C) 2017 Björn Esser <besser82@fedoraproject.org>
2576bb
#
2576bb
# based on cmake.prov, which is
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 subprocess
2576bb
2576bb
class CMakeParser:
2576bb
    def __init__(self, filelist = None):
2576bb
        if filelist == None:
2576bb
            filelist = sys.stdin
2576bb
2576bb
        has_module = False
2576bb
        is_arched = False
2576bb
2576bb
        isa_suf = subprocess.check_output(["/usr/bin/rpm", "-E %{?_isa}"]).decode().strip()
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
                has_module = True
2576bb
                if re.match(".*/usr/lib(64)?/cmake/.*", modulePath):
2576bb
                    is_arched = True
2576bb
2576bb
        if has_module:
2576bb
            if is_arched:
2576bb
                print("cmake-filesystem%s" % isa_suf)
2576bb
            else:
2576bb
                print("cmake-filesystem")
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
2576bb
if __name__ == "__main__":
2576bb
    parser = CMakeParser()