Module: Versioned Backups
Credit: Mitch Chapman
Before overwriting an existing file, it
is often desirable to make a backup. Example 4-1
emulates the behavior of Emacs by saving versioned backups.
It’s also compatible with the
marshal module, so you can use versioned output
files for output in marshal format. If you find other file-writing
modules that, like marshal, type-test rather than
using file-like objects polymorphically, the class supplied here will
stand you in good stead.
When Emacs saves a file foo.txt, it first checks
to see if foo.txt already exists. If it does,
the current file contents are backed up. Emacs can be configured to
use versioned backup files, so, for example,
foo.txt might be backed up to
foo.txt.~1~. If other versioned backups of the
file already exist, Emacs saves to the next available version. For
example, if the largest existing version number is 19, Emacs will
save the new version to foo.txt.~20~. Emacs can
also prompt you to delete old versions of your files. For example, if
you save a file that has six backups, Emacs can be configured to
delete all but the three newest backups.
Example 4-1 emulates the versioning backup behavior
of Emacs. It saves backups with version numbers (e.g., backing up
foo.txt to foo.txt.~n~ when
the largest existing backup number is n-1. It
also lets you specify how many old versions of a file to save. A
value that is less than zero means not to delete any old versions.
The marshal module lets you marshal an object to a ...