7676# - realign comments
7777# - optionally do much more thorough reformatting, a la C indent
7878
79+ from __future__ import print_function
80+
7981# Defaults
8082STEPSIZE = 8
8183TABSIZE = 8
@@ -370,17 +372,31 @@ def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
370372 return output .getvalue ()
371373# end def reformat_string
372374
375+ def make_backup (filename ):
376+ import os , os .path
377+ backup = filename + '~'
378+ if os .path .lexists (backup ):
379+ try :
380+ os .remove (backup )
381+ except os .error :
382+ print ("Can't remove backup %r" % (backup ,), file = sys .stderr )
383+ # end try
384+ # end if
385+ try :
386+ os .rename (filename , backup )
387+ except os .error :
388+ print ("Can't rename %r to %r" % (filename , backup ), file = sys .stderr )
389+ # end try
390+ # end def make_backup
391+
373392def complete_file (filename , stepsize = STEPSIZE , tabsize = TABSIZE , expandtabs = EXPANDTABS ):
374393 with open (filename , 'r' ) as f :
375394 source = f .read ()
376395 # end with
377396 result = complete_string (source , stepsize , tabsize , expandtabs )
378397 if source == result : return 0
379398 # end if
380- import os
381- try : os .rename (filename , filename + '~' )
382- except os .error : pass
383- # end try
399+ make_backup (filename )
384400 with open (filename , 'w' ) as f :
385401 f .write (result )
386402 # end with
@@ -394,10 +410,7 @@ def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = E
394410 result = delete_string (source , stepsize , tabsize , expandtabs )
395411 if source == result : return 0
396412 # end if
397- import os
398- try : os .rename (filename , filename + '~' )
399- except os .error : pass
400- # end try
413+ make_backup (filename )
401414 with open (filename , 'w' ) as f :
402415 f .write (result )
403416 # end with
@@ -411,10 +424,7 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
411424 result = reformat_string (source , stepsize , tabsize , expandtabs )
412425 if source == result : return 0
413426 # end if
414- import os
415- try : os .rename (filename , filename + '~' )
416- except os .error : pass
417- # end try
427+ make_backup (filename )
418428 with open (filename , 'w' ) as f :
419429 f .write (result )
420430 # end with
0 commit comments