Skip to content

Commit b393954

Browse files
committed
Take into account no existing .gdbinit file
Threw exception when no .gdbinit file existed due to attempting to copy from a non-existent location.
1 parent 91ccdfb commit b393954

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

scripts/pretty-printers/gdb/install.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,33 @@ def create_gdbinit_file():
3030

3131
gdbinit_file = os.path.join(home_folder, ".gdbinit")
3232
lines = []
33+
imports = { "os", "sys" }
3334
if os.path.exists(gdbinit_file):
3435
with open(gdbinit_file, 'r') as file:
3536
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")
37+
line_no = 0
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+
return
46+
line_no += 1
47+
while len(lines) != 0 and (lines[0] == "" or lines[0] == "python"):
48+
lines.pop(0)
49+
50+
backup_file = os.path.join(home_folder, "backup.gdbinit")
51+
if os.path.exists(backup_file):
52+
print("backup.gdbinit file already exists. Type 'y' if you would like to overwrite it or any other key to exit.")
53+
choice = input().lower()
54+
if choice != 'y':
4555
return
46-
line_no += 1
47-
while len(lines) != 0 and (lines[0] == "" or lines[0] == "python"):
48-
lines.pop(0)
56+
print("Backing up {0}".format(gdbinit_file))
57+
copyfile(gdbinit_file, backup_file)
4958

5059
lines = [ "python" ] + list(map("import {}".format, sorted(imports))) + [ "", "" ] + code_block + [ "", "" ] + lines + [ "" ]
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)
6060
print("Adding pretty-print commands to {0}.".format(gdbinit_file))
6161
try:
6262
with open(gdbinit_file, 'w+') as file:

0 commit comments

Comments
 (0)