File: generate_configure_cmake_script.py

package info (click to toggle)
mumble 1.5.735-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 90,008 kB
  • sloc: cpp: 556,921; ansic: 81,662; python: 3,606; sh: 659; makefile: 506; asm: 371; cs: 306; sql: 228; javascript: 143; perl: 80; xml: 13
file content (79 lines) | stat: -rwxr-xr-x 2,751 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
#
# Copyright 2022-2023 The Mumble Developers. All rights reserved.
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file at the root of the
# Mumble source tree or at <https://www.mumble.info/LICENSE>.

from typing import Tuple
from typing import List
from typing import Dict

import argparse
import sys


def parse_key_value_pair(pair: str) -> Tuple[str, str]:
    assert "=" in pair

    idx = pair.find("=")

    key = pair[: idx].strip()
    value = pair[idx + 1:].strip()

    if value[0] == value[-1] and value[0] in ["\"", "'"]:
        # Value is enclosed in quotes -> Remove those
        value = value[1: -1]

    return (key, value)


def parse_key_value_pairs(pairs: List[str]) -> Dict[str, str]:
    key_value_pairs = {}

    for currentPair in pairs:
        key, value = parse_key_value_pair(currentPair)

        key_value_pairs[key] = value

    return key_value_pairs


def main():
    parser = argparse.ArgumentParser(description="This script generates a cmake script whose sole purpose is to invoke configure_file" +
                                     " on a given set of files.")
    parser.add_argument(
        "--variables", help="Specify a number of variables and their content as key-value-pairs", metavar="NAME=VALUE", nargs="+")
    parser.add_argument("--files", help="Specify the to-be-configured files and their output path as key-value-pairs", metavar="IN-FILE=OUT-FILE",
                        nargs="+", required=True)
    parser.add_argument("--output", help="The path to the file to which to write the generated content. If not given, the content is" +
                        " written to stdout")
    parser.add_argument("--at-only", help="Passes the @ONLY option to configure_file", action="store_true", default=False)

    args = parser.parse_args()

    variables = parse_key_value_pairs(args.variables) if args.variables else {}
    files = parse_key_value_pairs(args.files) if args.files else {}

    cmake_content = "# Defining variables\n"
    for currentVar in variables.keys():
        if " " in currentVar:
            print("[ERROR]: Variable name with whitespace \"%s\"" % currentVar)
            sys.exit(1)

        cmake_content += "set(%s \"%s\")\n" % (currentVar, variables[currentVar])

    cmake_content += "\n"
    cmake_content += "# Configuring files\n"
    for currentInFile in files.keys():
        cmake_content += "configure_file(\"%s\" \"%s\" %s)\n" % (currentInFile, files[currentInFile], "@ONLY" if args.at_only else "")

    if args.output:
        with open(args.output, "w") as outFile:
            outFile.write(cmake_content)
    else:
        print(cmake_content)


if __name__ == "__main__":
    main()