Subversion Repositories Kolibri OS

Rev

Rev 9377 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9367 Boppan 1
# Copyright Magomed Kostoev
2
# Published under MIT license
3
 
9377 Boppan 4
block_beginner2finisher = {
5
    "(": ")",
6
    "{": "}",
7
    "\"": "\"",
8
}
9367 Boppan 9
 
9377 Boppan 10
def get_to(src, ptr, c, backwards = False):
11
    while src[ptr] != c:
12
        if not backwards:
13
            ptr += 1
14
        else:
15
            ptr -= 1
16
    return ptr
9367 Boppan 17
 
9377 Boppan 18
# Assuming we are at beginning of some block (string,
19
# parenthesed expression, {}-like thing), get to end
20
# of the block (also handles the blocks openned in the
21
# way to the block ending character. So it's not just
22
# stupid searching for block closing character.
23
def get_to_block_finisher(src, ptr):
24
    assert(src[ptr] in block_beginner2finisher)
25
 
26
    block_beginner = src[ptr]
27
    ptr += 1
28
    while src[ptr] != block_beginner2finisher[block_beginner]:
29
        # If any block starts here - get to its end and then continue
30
        if src[ptr] in block_beginner2finisher:
31
            ptr = get_to_block_finisher(src, ptr)
9367 Boppan 32
        ptr += 1
33
    return ptr
34
 
9377 Boppan 35
def get_strnig(src, ptr):
36
    # Strings starts with "\""
37
    assert(src[ptr] == "\"")
9367 Boppan 38
 
9377 Boppan 39
    result = ""
40
    # Skip first "\"" of the string
9367 Boppan 41
    ptr += 1
42
    while src[ptr] != "\"":
9377 Boppan 43
        result += src[ptr]
9367 Boppan 44
        ptr += 1
45
    return result
46
 
9377 Boppan 47
def parse_rule_output(src, ptr):
9367 Boppan 48
    # Get straight to the first argument
9377 Boppan 49
    ptr += len("tup.rule")
50
    # Get to parenthese
51
    ptr = get_to(src, ptr, "(")
52
    # Get to the closing parenthese
53
    ptr = get_to_block_finisher(src, ptr)
54
    # We are at closing parenthese of argument list
55
    # And the last argument is always output file
56
    # Let's get to closing "\"" of the output file name
57
    ptr = get_to(src, ptr, "\"", backwards = True)
58
    # Get into the string
59
    ptr -= 1
60
    # Then get to the beginning of the string
61
    ptr = get_to(src, ptr, "\"", backwards = True)
62
    # Now we can read the string
63
    return get_strnig(src, ptr)
9367 Boppan 64
 
9411 Boppan 65
def parse_required_compiler(src, ptr):
66
    # Get straignt to the first argument
67
    ptr += len("tup.getconfig")
68
    # Get to parenthese
69
    ptr = get_to(src, ptr, "(")
70
    # Get to start of the requirement string
71
    ptr = get_to(src, ptr, "\"")
72
    # Read the requirement string (like NO_FASM)
73
    requirement_string = get_strnig(src, ptr)
74
    if requirement_string.startswith("NO_"):
75
        return requirement_string[len("NO_"):].lower().replace("_", "-")
76
    else:
77
        return None
78
 
9377 Boppan 79
def parse_tupfile_outputs(file_name):
80
    outputs = []
9367 Boppan 81
    with open(file_name) as f:
82
        tupfile = f.read()
83
    rule_begin_index = tupfile.find("tup.rule(")
84
    while (rule_begin_index != -1):
9377 Boppan 85
        outputs.append(parse_rule_output(tupfile, rule_begin_index))
9367 Boppan 86
        # Find the next tup.rule call
87
        rule_begin_index = tupfile.find("tup.rule(", rule_begin_index + len("tup.rule("))
9377 Boppan 88
    return outputs
9411 Boppan 89
 
90
def parse_required_compilers(file_name):
91
    compilers = []
92
    with open(file_name) as f:
93
        tupfile = f.read()
94
    rule_begin_index = tupfile.find("tup.getconfig(")
95
    while (rule_begin_index != -1):
96
        required_compiler = parse_required_compiler(tupfile, rule_begin_index)
97
        if required_compiler is not None:
98
            compilers.append(required_compiler)
99
        # Find the next tup.getconfig call
100
        rule_begin_index = tupfile.find("tup.getconfig(", rule_begin_index + len("tup.getconfig"))
101
    return compilers
102