summaryrefslogtreecommitdiff
path: root/bin/test_dep_rewrite
blob: b8efb722a5cfc8e0b1af4ed827380899e583de0f (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
#!/usr/bin/python
import sys
import re


if __name__ == "__main__":
    args = sys.argv[1:]
    if not args or len(args) < 3:
        sys.stderr.write(
            "%s <target dependency> <rewrite rule> "
            "<replace 1> [<replace 2> ...\n" % (sys.argv[0],))
        raise SystemExit(1)

    target, pattern, replaces = args[0], args[1], args[2:]
    sys.stdout.write("Target:   %s\n" % (target,))
    sys.stdout.write("Pattern:  %s\n" % (pattern,))
    sys.stdout.write("Rewrites: %s\n" % (", ".join(replaces),))

    if pattern.startswith("++"):
        sys.stderr.write("You are just asking to add dep, meh!\n")
        raise SystemExit(1)

    sys.stdout.write("\n")

    compiled_pattern = re.compile(pattern)
    if not compiled_pattern.match(target):
        sys.stderr.write("Error: Pattern does not match target dep\n")
        raise SystemExit(1)

    exit_st = 0
    for replace in replaces:
        new_target, number_of_subs = compiled_pattern.subn(
            replace, target)
        if number_of_subs:
            sys.stdout.write("%s -> %s\n" % (target, new_target))
        else:
            sys.stderr.write("Error, not replaced: %s -> %s\n" % (
                    target, replace,))
            exit_st = 1

    raise SystemExit(exit_st)