Skip to content
Merged
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
13 changes: 11 additions & 2 deletions bagit.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,9 @@ def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
def _make_tagmanifest_file(alg, bag_dir):
tagmanifest_file = join(bag_dir, "tagmanifest-%s.txt" % alg)
LOGGER.info("writing %s", tagmanifest_file)
files = [f for f in listdir(bag_dir) if isfile(join(bag_dir, f))]

checksums = []
for f in files:
for f in _find_tag_files(bag_dir):
if re.match(r'^tagmanifest-.+\.txt$', f):
continue
with open(join(bag_dir, f), 'rb') as fh:
Expand All @@ -784,6 +784,15 @@ def _make_tagmanifest_file(alg, bag_dir):
for digest, filename in checksums:
tagmanifest.write('%s %s\n' % (digest, filename))

def _find_tag_files(bag_dir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Python community has increasingly discouraged the convention of using _ prefixes like this, but there's precedent in the other functions in this file. I'd be inclined to remove it.

for dir_name, _, filenames in os.walk(bag_dir):
if not re.match(r'.*data$', dir_name):
for filename in filenames:
if filename.startswith('tagmanifest-'):
continue
#remove everything up to the bag_dir directory
p = join(dir_name, filename)
yield os.path.relpath(p, bag_dir)

def _walk(data_dir):
for dirpath, dirnames, filenames in os.walk(data_dir):
Expand Down
31 changes: 31 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,37 @@ def test_validate_optional_tagfile(self):
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, self.validate, bag)

def test_validate_optional_tagfile_in_directory(self):
bag = bagit.make_bag(self.tmpdir)
tagdir = tempfile.mkdtemp(dir=self.tmpdir)

if not os.path.exists(j(tagdir, "tagfolder")):
os.makedirs(j(tagdir, "tagfolder"))

with open(j(tagdir, "tagfolder", "tagfile"), "w") as tagfile:
tagfile.write("test")
relpath = j(tagdir, "tagfolder", "tagfile").replace(self.tmpdir + os.sep, "")
relpath.replace("\\", "/")
with open(j(self.tmpdir, "tagmanifest-md5.txt"), "w") as tagman:
# Incorrect checksum.
tagman.write("8e2af7a0143c7b8f4de0b3fc90f27354 " + relpath + "\n")
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, self.validate, bag)

hasher = hashlib.new("md5")
with open(j(tagdir, "tagfolder", "tagfile"), "r") as tf:
contents = tf.read().encode('utf-8')
hasher.update(contents)
with open(j(self.tmpdir, "tagmanifest-md5.txt"), "w") as tagman:
tagman.write(hasher.hexdigest() + " " + relpath + "\n")
bag = bagit.Bag(self.tmpdir)
self.assertTrue(self.validate(bag))

# Missing tagfile.
os.remove(j(tagdir, "tagfolder", "tagfile"))
bag = bagit.Bag(self.tmpdir)
self.assertRaises(bagit.BagValidationError, self.validate, bag)

def test_sha1_tagfile(self):
info = {'Bagging-Date': '1970-01-01', 'Contact-Email': 'ehs@pobox.com'}
bag = bagit.make_bag(self.tmpdir, checksum=['sha1'], bag_info=info)
Expand Down