Skip to content

Avoid nested bags by default #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion bagit.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,27 @@ def find_locale_dir():
UNICODE_BYTE_ORDER_MARK = "\ufeff"


def is_bag(bag_dir):
"""
Return a boolean whether the given directory is already a bag.
"""
try:
Bag(bag_dir)
return True
except BagError:
return False


def make_bag(
bag_dir, bag_info=None, processes=1, checksums=None, checksum=None, encoding="utf-8"
bag_dir, bag_info=None, processes=1, checksums=None, checksum=None, encoding="utf-8", allow_nested_bag=False
):
"""
Convert a given directory into a bag. You can pass in arbitrary
key/value pairs to put into the bag-info.txt metadata file as
the bag_info dictionary.

By default creating a bag of directory that is already a bag will raise an error.
Set allow_nested_bag to allow creation of nested bags.
"""

if checksum is not None:
Expand All @@ -167,6 +181,13 @@ def make_bag(
_("Bagging a parent of the current directory is not supported")
)

if not allow_nested_bag:
if is_bag(bag_dir):
raise RuntimeError(
_(f"The directory '{bag_dir}' is already a bag. "
"Use allow_nested_bag=True to allow creation of a nested bag.")
)

LOGGER.info(_("Creating bag for directory %s"), bag_dir)

if not os.path.isdir(bag_dir):
Expand Down
25 changes: 25 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ def test_make_bag_md5_sha1_sha256_manifest(self):
# check valid with three manifests
self.assertTrue(self.validate(bag, fast=True))

def test_is_bag_false_initially(self):
self.assertFalse(bagit.is_bag(self.tmpdir))

def test_is_bag_true_after_make_bag(self):
bagit.make_bag(self.tmpdir)
self.assertTrue(bagit.is_bag(self.tmpdir))

def test_make_nested_bag_without_flag(self):
bagit.make_bag(self.tmpdir)

with self.assertRaises(RuntimeError) as ctx:
tmpdir = self.tmpdir
bagit.make_bag(tmpdir, allow_nested_bag=False)

expected_msg = (f"The directory '{tmpdir}' is already a bag. "
"Use allow_nested_bag=True to allow creation of a nested bag.")
self.assertEqual(str(ctx.exception), expected_msg)

def test_make_nested_bag_with_flag(self):
bagit.make_bag(self.tmpdir)

bag = bagit.make_bag(self.tmpdir, allow_nested_bag=True)

self.assertIsInstance(bag, bagit.Bag)

def test_validate_flipped_bit(self):
bag = bagit.make_bag(self.tmpdir)
readme = j(self.tmpdir, "data", "README")
Expand Down