-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
215 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
Bastion.Utils | ||
I mostly contain "helper" functions that are agnostic to the vault protocol, etc. | ||
""" | ||
import datetime | ||
import pathlib | ||
import tarfile | ||
import logging | ||
|
||
import Bastion.Model | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class ifFileChanged: | ||
""" | ||
A class that can create callable instances to check | ||
tarinfo metadata blocks for file modifications since a given data. | ||
this is a filter for use with the python tarfile module and follows | ||
the filter convention used where the filter modifies in-place the | ||
given tarinfo object, or returns None to signal that the file should | ||
be skipped. | ||
e.g. | ||
filter = ifChangedSince( datetime.datetime(2024,1,1) ) | ||
if filter(tarinfo): | ||
#-- add to tar file | ||
else: | ||
#-- return None | ||
""" | ||
def __init__(self, when): | ||
self.whence = when | ||
|
||
def __call__(self, tarinfo): | ||
if tarinfo.type not in (tarfile.REGTYPE, tarfile.AREGTYPE): | ||
return tarinfo | ||
|
||
then = datetime.datetime.fromtimestamp(tarinfo.mtime) | ||
logger.debug("comparing file {} mod'd at {} to change limit at {}".format(tarinfo.name, then.isoformat(), self.whence.isoformat())) | ||
if then > self.whence: | ||
return tarinfo | ||
else: | ||
#-- returning None is rejecting this file for inclusion | ||
#-- in the accumulating tar file. | ||
return None | ||
|
||
def pax(tarp, asset, **kwargs): | ||
""" | ||
pax uses the python tarfile module to create a tar file using the | ||
extended POSIX.1-2001 (aka "PAX") format. | ||
gnutar (as of the date of writing, 2024-09-04) can read PAX format, but still defaults to creating archives using its own modified USTAR format. | ||
{tarp} is the halo (path) to the local file where the tar will be built. | ||
{asset} can be a file path or an instance of Bastion.Model.isAsset (e.g. Bastion.Site.Asset) | ||
differential backups can be done by supplying the "since" keyword set the datetime which is the earliest allowed modification time for files to be admitted into the tar. | ||
""" | ||
if isinstance(asset, Bastion.Model.isAsset): | ||
src = asset.halo | ||
else: | ||
src = pathlib.Path(asset) | ||
|
||
with tarfile.open(tarp, "w", format = tarfile.PAX_FORMAT) as tar: | ||
if 'since' in kwargs: | ||
when = kwargs['since'] | ||
tar.add(src, filter = ifFileChanged(when)) | ||
else: | ||
tar.add(src) | ||
|
||
#-- if no exceptions were generated during the tar construction, | ||
#-- then we get here and we can return a happy True! | ||
return True |
Oops, something went wrong.