From 8580f023028de7ecd63439a288effd9c6d5fa6d0 Mon Sep 17 00:00:00 2001 From: "James E.H. Turner" Date: Wed, 19 Oct 2016 16:29:54 -0300 Subject: Avoid blank lines at the start of the file when adding/removing the 1st entry. --- scripts/ac_update_extern_pkg | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/scripts/ac_update_extern_pkg b/scripts/ac_update_extern_pkg index 90d1c65..b44155d 100644 --- a/scripts/ac_update_extern_pkg +++ b/scripts/ac_update_extern_pkg @@ -121,8 +121,9 @@ match = re.search(keep_re, buffer, flags=re.M) if match: buffer = buffer[:match.start()] -# Make sure that what goes before the "keep" ends with a new line: -buffer = re.compile('\n?\Z', flags=re.M).sub('\n', buffer) +# Make sure what goes before the "keep" ends with a new line (unless empty): +sep = '\n' if buffer.strip() else '' +buffer = re.compile('\n?\Z', flags=re.M).sub(sep, buffer) # Find the last entry in extern.pkg (if any) that matches the template: match = None @@ -150,8 +151,12 @@ if match: # template-based definition but then continue as if there had been no # match, so a new definition will get placed at the end of the file below: if later_match or args_remove: - buffer = '%s\n\n%s' % (buffer[:match.start()].rstrip(), - buffer[match.end():].lstrip()) + + # Replace the entry by 2 separator lines (unless it's the first one): + buf_before = buffer[:match.start()].rstrip() + buf_after = buffer[match.end():].lstrip() + buffer = buf_before + ('\n\n' if buf_before else '') + buf_after + del buf_before, buf_after match = None if not args_remove: -- cgit