This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifndefy.py
executable file
·80 lines (72 loc) · 2.84 KB
/
ifndefy.py
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
#!/usr/bin/env python3
#
# Copyright (C) 2021 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
# About:
# When provided with an interface file, this script outputs a new interface
# file, where all original interfaces are enclosed in an ifndef statement
# dependent on their name. All indentation inside the interface file should
# be converted to tabs before running this script.
#
# Known issues:
# - interface already enclosed in an ifndef statement is not ignored
# - "`" and "'" inside comments are not ignored
# - \t is always used for indentation - may result in mixture of spaces and tabs
import sys
import os
import re
if len(sys.argv) < 1:
print(("Usage: {} <policy>.if > <output>.if").format(sys.argv[0]), file=sys.stderr)
exit(os.EX_USAGE)
# ending index of interface
end = 0
with open(sys.argv[1], 'r') as f:
interfaces = f.read()
file_len = len(interfaces)
while end < file_len:
start = interfaces.find("interface(`", end)
if start < 0:
#no more interfaces
print(interfaces[end:], end ="")
break
name_end = interfaces.find("'", start+11)
name = interfaces[start+11:name_end]
# locate the end of the interface - i.e. ending apostrophe
reg = re.compile(r"[`']")
# skip the ' after interface name
i = name_end+1
# counter for the depth (` opens a new block, while ' closes it)
graves = 0
while i < file_len:
match = reg.search(interfaces, i)
if not match:
print("Malformed interface: {}".format(name), file=sys.stderr)
exit(1)
i = match.end()
if match.group(0) == '`':
graves += 1
else:
graves -= 1
if graves == 0:
break
# keep everything between the end of previous interface and start of a new one
print(interfaces[end:start], end ="")
# interface ends in "')" -- add 1 for the edning bracket
end = i+1
# print the whole interface enclosed in "ifndef"
print("ifndef(`{}',`".format(name))
print('\n'.join([('\t' + x) if x else x for x in interfaces[start:end].split('\n')]))
print("')", end ="")