This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addpromise.py
executable file
·55 lines (44 loc) · 1.61 KB
/
addpromise.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
#!/usr/bin/env python
import os
import re
import sys
DEFAULT_PROMISE = ["wpath", "cpath", "rpath", "flock"]
def main():
if len(sys.argv) == 2:
filename = sys.argv[1]
promise = DEFAULT_PROMISE
elif len(sys.argv) == 3:
filename = sys.argv[1]
promise = sys.argv[2].split(" ")
else:
sys.exit("ERROR: Please specify a source file and promise. Default: [\"wpath\", \"cpath\", \"rpath\", \"flock\"].")
if not os.path.exists(filename):
sys.exit('ERROR: %s was not found.' % sys.argv[1])
if not os.path.isfile(filename):
sys.exit('ERROR: %s is not a file.' % sys.argv[1])
buf = ""
# Example: pledge("stdio rpath proc exec", NULL)
pledged = False
with open(filename, 'r') as source:
for line in source:
line = line.rstrip()
pledge_match = re.findall(".*pledge\(\"(.*)\",.*", line)
pledge_num = len(pledge_match)
if pledge_num > 0:
print(filename, ":", pledge_match)
pledged = True
for p in promise:
promise_match = re.findall(
".*pledge\(\"(.*%s.*)\",.*\)" % p, line)
promise_num = len(promise_match)
if promise_num < pledge_num:
line = line.replace(
pledge_match[0], "%s %s" % (pledge_match[0], p))
buf = "{}{}\n".format(buf, line)
if not pledged:
print(filename, ":", "None")
source = open(filename, 'w')
source.write(buf)
source.close()
if __name__ == "__main__":
exit(main())