Want to save snapshots of work in progress
Create a simple [%g version_control_system “version control system” %]
And show how to test it using mock objects ([%x protocols %])
Wasteful to store the same file repeatedly
So if the file’s hash is abcd1234, save it as
abcd1234.bck
Then create a [%g manifest “manifest” %] to show what unique blocks of bytes had what names when
[% figure slug=“archive-storage” img=“storage.svg” alt=“Backup file storage” caption=“Organization of backup file storage.” %]
[%inc hash_all.py mark=func %]
[%inc sample_dir.out %] [%inc hash_all.sh %] [%inc hash_all.out %]
Obvious approach is to create lots of files and directories
But we want to test what happens when they change, which makes things complicated to maintain
Use a [%g mock_object “mock object” %] ([%x protocols %]) instead of the real filesystem
open with
ones that behave the same way but act on “files” stored in memory[% figure slug=“archive-mock-fs” img=“mock_fs.svg” alt=“Mock filesystem” caption=“Using a mock filesystem to simplify testing.” %]
import pyfakefs automatically creates a fixture called
fs[%inc test_mock_fs.py %]
[%inc test_mock_tree.py %]
[%inc test_hash_all.py omit=change %]
Store backups and manifests in a directory selected by the user
Real system would support remote storage as well
Which suggests we need to design with multiple back ends in mind
Backed-up files are abcd1234.bck
Manifests are ssssssssss.csv, where
ssssssssss is the [%g utc “UTC” %] [%g timestamp
“timestamp” %]
Manifest naming scheme fails if we try to create two backups in less than one second
A [%g toctou “time of check/time of use” %] [%g race_condition “race condition” %]
May seem unlikely, but many bugs and security holes seemed unlikely to their creators
[%inc backup.py mark=backup %]
Create the backup directory if it doesn’t already exist
Then save CSV
[%inc backup.py mark=write %]
[%inc backup.py mark=copy %]
[%inc test_backup.py mark=setup %]
[%inc test_backup.py mark=test %]
Trust that the hash is correct
Should look inside the manifest and check that it lists files correctly
[%inc backup_oop.py mark=base %]
Derive a [%g child_class “child class” %] to do local archiving
Convert functions we have built so far into methods
[%inc backup_oop.py mark=create %]
[%inc backup_oop.py mark=use %]
[% figure slug=“archive-concept-map” img=“concept_map.svg” alt=“Concept map of build manager” caption=“Concept map.” %]