Skip to content

Commit ba5860f

Browse files
committed
Merge pull request #33 from edsu/issue32
Issue32
2 parents 19de132 + 27176ee commit ba5860f

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

bagit.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
146146
bag_info['Payload-Oxum'] = Oxum
147147
_make_tag_file('bag-info.txt', bag_info)
148148

149-
_make_tagmanifest_file('tagmanifest-md5.txt', bag_dir)
149+
for c in checksum:
150+
_make_tagmanifest_file(c, bag_dir)
151+
150152

151153
except Exception as e:
152154
os.chdir(old_dir)
@@ -311,7 +313,8 @@ def save(self, processes=1, manifests=False):
311313
_make_tag_file(self.tag_file_name, self.info)
312314

313315
# Update tag-manifest for changes to manifest & bag-info files
314-
_make_tagmanifest_file('tagmanifest-md5.txt', self.path)
316+
for alg in self.algs:
317+
_make_tagmanifest_file(alg, self.path)
315318

316319
# Reload the manifests
317320
self._load_manifests()
@@ -759,14 +762,16 @@ def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
759762
return "%s.%s" % (total_bytes, num_files)
760763

761764

762-
def _make_tagmanifest_file(tagmanifest_file, bag_dir):
765+
def _make_tagmanifest_file(alg, bag_dir):
766+
tagmanifest_file = join(bag_dir, "tagmanifest-%s.txt" % alg)
767+
logging.info("writing %s", tagmanifest_file)
763768
files = [f for f in listdir(bag_dir) if isfile(join(bag_dir, f))]
764769
checksums = []
765770
for f in files:
766-
if f == tagmanifest_file:
771+
if re.match('^tagmanifest-.+\.txt$', f):
767772
continue
768773
fh = open(join(bag_dir, f), 'rb')
769-
m = hashlib.md5()
774+
m = _hasher(alg)
770775
while True:
771776
bytes = fh.read(16384)
772777
if not bytes:
@@ -830,8 +835,7 @@ def _manifest_line_sha256(filename):
830835
def _manifest_line_sha512(filename):
831836
return _manifest_line(filename, 'sha512')
832837

833-
def _manifest_line(filename, algorithm='md5'):
834-
fh = open(filename, 'rb')
838+
def _hasher(algorithm='md5'):
835839
if algorithm == 'md5':
836840
m = hashlib.md5()
837841
elif algorithm == 'sha1':
@@ -840,6 +844,11 @@ def _manifest_line(filename, algorithm='md5'):
840844
m = hashlib.sha256()
841845
elif algorithm == 'sha512':
842846
m = hashlib.sha512()
847+
return m
848+
849+
def _manifest_line(filename, algorithm='md5'):
850+
fh = open(filename, 'rb')
851+
m = _hasher(algorithm)
843852

844853
total_bytes = 0
845854
while True:

test.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ def test_validate_optional_tagfile(self):
269269
bag = bagit.Bag(self.tmpdir)
270270
self.assertRaises(bagit.BagValidationError, self.validate, bag)
271271

272+
def test_sha1_tagfile(self):
273+
bag = bagit.make_bag(self.tmpdir, checksum=['sha1'])
274+
self.assertTrue(os.path.isfile(j(self.tmpdir, 'tagmanifest-sha1.txt')))
275+
self.assertEqual(bag.entries['bag-info.txt']['sha1'], 'b537642e07abc0c22c428aee65180e97f78e61dc')
276+
272277
def test_validate_unreadable_file(self):
273278
bag = bagit.make_bag(self.tmpdir, checksum=["md5"])
274279
os.chmod(j(self.tmpdir, "data/loc/2478433644_2839c5e8b8_o_d.jpg"), 0)
@@ -493,6 +498,17 @@ def test_save_baginfo(self):
493498
self.assertEqual(b.info["x"], ["a", "b", "c"])
494499
self.assertTrue(bag.is_valid())
495500

501+
def test_save_baginfo_with_sha1(self):
502+
bag = bagit.make_bag(self.tmpdir, checksum=["sha1", "md5"])
503+
self.assertTrue(bag.is_valid())
504+
bag.save()
505+
506+
bag.info['foo'] = "bar"
507+
bag.save()
508+
509+
bag = bagit.Bag(self.tmpdir)
510+
self.assertTrue(bag.is_valid())
511+
496512
def test_save_only_baginfo(self):
497513
bag = bagit.make_bag(self.tmpdir)
498514
with open(j(self.tmpdir, 'data', 'newfile'), 'w') as nf:
@@ -504,7 +520,6 @@ def test_save_only_baginfo(self):
504520
self.assertEqual(bag.info["foo"], "bar")
505521
self.assertFalse(bag.is_valid())
506522

507-
508523
def test_make_bag_with_newline(self):
509524
bag = bagit.make_bag(self.tmpdir, {"test": "foo\nbar"})
510525
self.assertEqual(bag.info["test"], "foobar")

0 commit comments

Comments
 (0)