Skip to content

Commit

Permalink
Don't try to instantiate a Bag instance on non-directories in is_bag.
Browse files Browse the repository at this point in the history
Don't preserve `mtime` for payload files in idempotent mode. There are arguments for/against this but there is ultimately more broad utility in eliminating it. At some point there must be a tradeoff; if this information is important to preserve, idempotent mode should not be used.
Similarly, when extracting a tar-based archive, if `mtime` is set to zero (i.e. epoch), use the tarfile extraction filter to set `mtime` to None which will cause tarfile to suppress preserving the mtime in the extracted file. This should work in every version that has the back-ported extraction filter support. Python versions less than 3.8 won't be able to do this but those are deprecated and are going to be unsupported by bdbag soon.
Add Python 3.12 into GH Actions tests.
Remove travis.yml.
  • Loading branch information
mikedarcy committed Mar 5, 2024
1 parent d3e7d78 commit 15f22fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bdbag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
Expand Down
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

23 changes: 14 additions & 9 deletions bdbag/bdbag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ def prune_bag_manifests(bag):
def is_bag(bag_path):
bag = None
try:
bag = bdbagit.BDBag(bag_path)
if os.path.isdir(bag_path):
bag = bdbagit.BDBag(bag_path)
except (bdbagit.BagError, bdbagit.BagValidationError) as e: # pragma: no cover
logger.warning("Exception while checking if %s is a bag: %s" % (bag_path, e))
logger.warning("Exception while checking if directory %s is a bag: %s" % (bag_path, e))
return True if bag else False


Expand Down Expand Up @@ -382,10 +383,8 @@ def archive_bag(bag_path, bag_archiver, config_file=None, idempotent=None):
def tar_bag_dir(bag_path, tar_file_path, tarmode, idempotent=False):

def filter_mtime(tarinfo):
# a fixed mtime is a core requirement for a reproducible archive, but we should preserve payload file mtimes
if not (tarinfo.name.startswith(os.path.basename(bag_path) + "/data/") and
os.path.isfile(os.path.dirname(bag_path) + "/" + tarinfo.name)):
tarinfo.mtime = 0
# a fixed mtime is a core requirement for a reproducible archive
tarinfo.mtime = 0
return tarinfo

is_idempotent_tgz = False
Expand Down Expand Up @@ -434,8 +433,8 @@ def zip_bag_dir(bag_path, zip_file_path, idempotent=False):
for e in entries:
filepath = os.path.join(os.path.dirname(bag_path), e)
payload_dir = os.path.join(os.path.basename(bag_path), "data", "")
# a fixed mtime is a core requirement for a reproducible archive, but we should preserve payload file mtimes
if idempotent and not (e.startswith(payload_dir) and os.path.isfile(filepath)):
# a fixed mtime is a core requirement for a reproducible archive
if idempotent:
date_time = (1980, 1, 1, 0, 0, 0)
else:
st = os.stat(filepath)
Expand Down Expand Up @@ -506,7 +505,13 @@ def extract_bag(bag_path, output_path=None, temp=False, config_file=None):
try:
if isinstance(archive, tarfile.TarFile):
if hasattr(tarfile, 'data_filter'):
archive.extractall(base_path, filter='data')
# customize tarfile 'data' filter: if we encounter a tarinfo entry with a mtime of 0 (epoch), then
# set mtime to None which will cause tarfile to suppress preserving the mtime for the extracted file
def tar_data_filter(entry, path):
if entry.mtime == 0:
entry.mtime = None
return tarfile.data_filter(entry, path)
archive.extractall(base_path, filter=tar_data_filter)
else:
if isinstance(archive, tarfile.TarFile):
logger.warning('SECURITY WARNING: TAR extraction may be unsafe; consider updating Python to a '
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
'Programming Language :: Python',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11'
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12'

]
)
Expand Down

0 comments on commit 15f22fb

Please sign in to comment.