summaryrefslogtreecommitdiffstats
path: root/util/cmake/condition_simplifier_cache.py
blob: b405ef23f8451568c96851c73c51e23dd314af40 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
#############################################################################
##
## Copyright (C) 2018 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the plugins of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:GPL-EXCEPT$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see https://www.qt.io/terms-conditions. For further
## information use the contact form at https://www.qt.io/contact-us.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3 as published by the Free Software
## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
## included in the packaging of this file. Please review the following
## information to ensure the GNU General Public License requirements will
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
##
## $QT_END_LICENSE$
##
#############################################################################


import atexit
import hashlib
import json
import os
import sys
import time

from typing import Callable


def get_current_file_path() -> str:
    try:
        this_file = __file__
    except NameError:
        this_file = sys.argv[0]
    this_file = os.path.abspath(this_file)
    return this_file


def get_cache_location() -> str:
    this_file = get_current_file_path()
    dir_path = os.path.dirname(this_file)
    cache_path = os.path.join(dir_path, ".pro2cmake_cache", "cache.json")
    return cache_path


def get_file_checksum(file_path: str) -> str:
    try:
        with open(file_path, "r") as content_file:
            content = content_file.read()
    except IOError:
        content = time.time()
    checksum = hashlib.md5(content.encode("utf-8")).hexdigest()
    return checksum


def get_condition_simplifier_checksum() -> str:
    current_file_path = get_current_file_path()
    dir_name = os.path.dirname(current_file_path)
    condition_simplifier_path = os.path.join(dir_name, "condition_simplifier.py")
    return get_file_checksum(condition_simplifier_path)


def init_cache_dict():
    return {
        "checksum": get_condition_simplifier_checksum(),
        "schema_version": "1",
        "cache": {"conditions": {}},
    }


def simplify_condition_memoize(f: Callable[[str], str]):
    cache_path = get_cache_location()
    cache_file_content = None

    if os.path.exists(cache_path):
        try:
            with open(cache_path, "r") as cache_file:
                cache_file_content = json.load(cache_file)
        except (IOError, ValueError):
            pass

    if not cache_file_content:
        cache_file_content = init_cache_dict()

    current_checksum = get_condition_simplifier_checksum()
    if cache_file_content["checksum"] != current_checksum:
        cache_file_content = init_cache_dict()

    def update_cache_file():
        if not os.path.exists(cache_path):
            os.makedirs(os.path.dirname(cache_path), exist_ok=True)
        with open(cache_path, "w") as cache_file_write_handle:
            json.dump(cache_file_content, cache_file_write_handle, indent=4)

    atexit.register(update_cache_file)

    def helper(condition: str) -> str:
        if condition not in cache_file_content["cache"]["conditions"]:
            cache_file_content["cache"]["conditions"][condition] = f(condition)
        return cache_file_content["cache"]["conditions"][condition]

    return helper