Skip to content

Commit 27176ee

Browse files
committed
allow non-md5 tagmanifests to be created. fixes #32
1 parent 0a9fed9 commit 27176ee

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def test_validate_optional_tagfile(self):
272272
def test_sha1_tagfile(self):
273273
bag = bagit.make_bag(self.tmpdir, checksum=['sha1'])
274274
self.assertTrue(os.path.isfile(j(self.tmpdir, 'tagmanifest-sha1.txt')))
275+
self.assertEqual(bag.entries['bag-info.txt']['sha1'], 'b537642e07abc0c22c428aee65180e97f78e61dc')
275276

276277
def test_validate_unreadable_file(self):
277278
bag = bagit.make_bag(self.tmpdir, checksum=["md5"])

0 commit comments

Comments
 (0)