|
1 |
| -#!/usr/bin/env python |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import os
|
4 |
| - |
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 |
| - import auto_load |
16 |
| - auto_load.load_pretty_printers() |
17 |
| -end |
18 |
| -""" |
| 4 | +from shutil import copyfile |
19 | 5 |
|
20 | 6 |
|
21 | 7 | def create_gdbinit_file():
|
22 | 8 | """
|
23 |
| - Add or append to a .gdbinit file the python code to set-up cbmc pretty-printers. |
| 9 | + Create and insert into a .gdbinit file the python code to set-up cbmc pretty-printers. |
24 | 10 | """
|
25 | 11 |
|
26 | 12 | print("Attempting to enable cbmc-specific pretty-printers.")
|
27 | 13 |
|
28 | 14 | home_folder = os.path.expanduser("~")
|
29 | 15 | if not home_folder:
|
30 |
| - print(home_folder + " is an invalid home folder, please manually create a .gdbinit file and apply the code.") |
| 16 | + print(home_folder + " is an invalid home folder, can't auto-configure .gdbinit.") |
31 | 17 | return
|
32 | 18 |
|
| 19 | + # This is the code that should be copied if you're applying the changes by hand. |
| 20 | + gdb_directory = os.path.dirname(os.path.abspath(__file__)) |
| 21 | + code_block_start = "cbmc_printers_folder = " |
| 22 | + code_block = \ |
| 23 | + [ |
| 24 | + "{0}'{1}'".format(code_block_start, gdb_directory), |
| 25 | + "if os.path.exists(cbmc_printers_folder):", |
| 26 | + " sys.path.insert(1, cbmc_printers_folder)", |
| 27 | + " from pretty_printers import load_cbmc_printers", |
| 28 | + " load_cbmc_printers()", |
| 29 | + ] |
| 30 | + |
33 | 31 | gdbinit_file = os.path.join(home_folder, ".gdbinit")
|
34 |
| - file_write_mode = 'w' |
| 32 | + lines = [] |
35 | 33 | 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.") |
| 34 | + with open(gdbinit_file, 'r') as file: |
| 35 | + lines = [ line.rstrip() for line in file ] |
| 36 | + line_no = 0 |
| 37 | + imports = { "os", "sys" } |
| 38 | + while line_no < len(lines): |
| 39 | + if lines[line_no].startswith('import '): |
| 40 | + imports.add(lines[line_no][len("import "):].strip()) |
| 41 | + lines.pop(line_no) |
| 42 | + else: |
| 43 | + if lines[line_no].startswith(code_block_start): |
| 44 | + print(".gdbinit already contains our pretty printers, not changing it") |
45 | 45 | return
|
| 46 | + line_no += 1 |
| 47 | + while len(lines) != 0 and (lines[0] == "" or lines[0] == "python"): |
| 48 | + lines.pop(0) |
46 | 49 |
|
47 |
| - if file_write_mode == 'w': |
48 |
| - print("Creating .gdbinit file.") |
| 50 | + lines = [ "python" ] + list(map("import {}".format, sorted(imports))) + [ "", "" ] + code_block + [ "", "" ] + lines + [ "" ] |
49 | 51 |
|
| 52 | + backup_file = os.path.join(home_folder, "backup.gdbinit") |
| 53 | + if os.path.exists(backup_file): |
| 54 | + print("backup.gdbinit file already exists. Type 'y' if you would like to overwrite it or any other key to exit.") |
| 55 | + choice = input().lower() |
| 56 | + if choice != 'y': |
| 57 | + return |
| 58 | + print("Backing up {0}".format(gdbinit_file)) |
| 59 | + copyfile(gdbinit_file, backup_file) |
50 | 60 | print("Adding pretty-print commands to {0}.".format(gdbinit_file))
|
51 |
| - parent_directory = os.path.dirname(os.path.abspath(__file__)) |
52 | 61 | try:
|
53 |
| - file = open(gdbinit_file, file_write_mode) |
54 |
| - file.write(file_contents.format(parent_directory)) |
55 |
| - file.close() |
| 62 | + with open(gdbinit_file, 'w+') as file: |
| 63 | + file.write('\n'.join(lines)) |
56 | 64 | print("Commands added.")
|
57 | 65 | except:
|
58 | 66 | print("Exception occured writing to file. Please apply changes manually.")
|
59 | 67 |
|
60 | 68 |
|
61 | 69 | if __name__ == "__main__":
|
62 | 70 | create_gdbinit_file()
|
63 |
| - |
|
0 commit comments