Skip to content

Commit 0fed35d

Browse files
Update pretty printer install script
Make it: * able to detect when it has already been run * able to add script to existing file without breaking existing printers * add new script before existing printers that might clash with it (such as the C++ ones) * executable
1 parent 11a407f commit 0fed35d

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

scripts/pretty-printers/gdb/install.py

100644100755
+33-35
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,61 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
import os
44

5-
# This is the code that should be copied if you're applying the changes by hand.
6-
# Replace {0} with the path to this folder.
7-
file_contents = """
8-
python
9-
import sys
10-
import os
11-
12-
pretty_printer_folder = '{0}'
13-
if os.path.exists(pretty_printer_folder):
14-
sys.path.insert(1, pretty_printer_folder)
15-
from pretty_printers import load_cbmc_printers
16-
load_cbmc_printers()
17-
end
18-
"""
19-
205

216
def create_gdbinit_file():
227
"""
23-
Add or append to a .gdbinit file the python code to set-up cbmc pretty-printers.
8+
Create and insert into a .gdbinit file the python code to set-up cbmc pretty-printers.
249
"""
2510

2611
print("Attempting to enable cbmc-specific pretty-printers.")
2712

2813
home_folder = os.path.expanduser("~")
2914
if not home_folder:
30-
print(home_folder + " is an invalid home folder, please manually create a .gdbinit file and apply the code.")
15+
print(home_folder + " is an invalid home folder, can't auto-configure .gdbinit.")
3116
return
3217

18+
# This is the code that should be copied if you're applying the changes by hand.
19+
gdb_directory = os.path.dirname(os.path.abspath(__file__))
20+
code_block_start = "cbmc_printers_folder = "
21+
code_block = \
22+
[
23+
"{0}'{1}'".format(code_block_start, gdb_directory),
24+
"if os.path.exists(cbmc_printers_folder):",
25+
" sys.path.insert(1, cbmc_printers_folder)",
26+
" from pretty_printers import load_cbmc_printers",
27+
" load_cbmc_printers()",
28+
]
29+
3330
gdbinit_file = os.path.join(home_folder, ".gdbinit")
34-
file_write_mode = 'w'
31+
lines = []
3532
if os.path.exists(gdbinit_file):
36-
print(".gdbinit file exists at " + gdbinit_file + "."
37-
" Please type 'y' if you want to append the pretty-printer commands to it. Press any other key to exit.")
38-
while True:
39-
choice = input().lower()
40-
if choice == 'y':
41-
file_write_mode = 'a'
42-
break
43-
else:
44-
print("Not appending to file. Exiting.")
33+
with open(gdbinit_file, 'r') as file:
34+
lines = [ line.rstrip() for line in file ]
35+
line_no = 0
36+
imports = { "os", "sys" }
37+
while line_no < len(lines):
38+
if lines[line_no].startswith('import '):
39+
imports.add(lines[line_no][len("import "):].strip())
40+
lines.pop(line_no)
41+
else:
42+
if lines[line_no].startswith(code_block_start):
43+
print(".gdbinit already contains our pretty printers, not changing it")
4544
return
45+
line_no += 1
46+
while len(lines) != 0 and (lines[0] == "" or lines[0] == "python"):
47+
lines.pop(0)
4648

47-
if file_write_mode == 'w':
48-
print("Creating .gdbinit file.")
49+
lines = [ "python" ] + list(map("import {}".format, sorted(imports))) + [ "", "" ] + code_block + [ "", "" ] + lines + [ "" ]
4950

5051
print("Adding pretty-print commands to {0}.".format(gdbinit_file))
51-
parent_directory = os.path.dirname(os.path.abspath(__file__))
5252
try:
53-
file = open(gdbinit_file, file_write_mode)
54-
file.write(file_contents.format(parent_directory))
55-
file.close()
53+
with open(gdbinit_file, 'w+') as file:
54+
file.write('\n'.join(lines))
5655
print("Commands added.")
5756
except:
5857
print("Exception occured writing to file. Please apply changes manually.")
5958

6059

6160
if __name__ == "__main__":
6261
create_gdbinit_file()
63-

0 commit comments

Comments
 (0)