Skip to content

Commit ff83143

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a8088ca + 7684c79 commit ff83143

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

bagit.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
LOGGER = logging.getLogger(__name__)
4949

50+
VERSION = '1.5.4'
51+
5052
# standard bag-info.txt metadata
5153
STANDARD_BAG_INFO_HEADERS = [
5254
'Source-Organization',
@@ -141,7 +143,7 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
141143
if 'Bagging-Date' not in bag_info:
142144
bag_info['Bagging-Date'] = date.strftime(date.today(), "%Y-%m-%d")
143145
if 'Bag-Software-Agent' not in bag_info:
144-
bag_info['Bag-Software-Agent'] = 'bagit.py <http://github.com/libraryofcongress/bagit-python>'
146+
bag_info['Bag-Software-Agent'] = 'bagit.py v' + VERSION + ' <http://github.com/libraryofcongress/bagit-python>'
145147
bag_info['Payload-Oxum'] = Oxum
146148
_make_tag_file('bag-info.txt', bag_info)
147149

@@ -766,9 +768,9 @@ def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
766768
def _make_tagmanifest_file(alg, bag_dir):
767769
tagmanifest_file = join(bag_dir, "tagmanifest-%s.txt" % alg)
768770
LOGGER.info("writing %s", tagmanifest_file)
769-
files = [f for f in listdir(bag_dir) if isfile(join(bag_dir, f))]
771+
770772
checksums = []
771-
for f in files:
773+
for f in _find_tag_files(bag_dir):
772774
if re.match(r'^tagmanifest-.+\.txt$', f):
773775
continue
774776
with open(join(bag_dir, f), 'rb') as fh:
@@ -784,6 +786,15 @@ def _make_tagmanifest_file(alg, bag_dir):
784786
for digest, filename in checksums:
785787
tagmanifest.write('%s %s\n' % (digest, filename))
786788

789+
def _find_tag_files(bag_dir):
790+
for dir_name, _, filenames in os.walk(bag_dir):
791+
if not re.match(r'.*data$', dir_name):
792+
for filename in filenames:
793+
if filename.startswith('tagmanifest-'):
794+
continue
795+
#remove everything up to the bag_dir directory
796+
p = join(dir_name, filename)
797+
yield os.path.relpath(p, bag_dir)
787798

788799
def _walk(data_dir):
789800
for dirpath, dirnames, filenames in os.walk(data_dir):

setup.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from setuptools import setup
44

5+
import bagit
6+
57
if version < '2.6.0':
68
print("python 2.6 or higher is required")
79
exit(1)
@@ -24,10 +26,11 @@
2426
except:
2527
requirements.append("hashlib")
2628

29+
version = bagit.VERSION
2730

2831
setup(
2932
name = 'bagit',
30-
version = '1.5.4',
33+
version = version,
3134
url = 'https://libraryofcongress.github.io/bagit-python/',
3235
author = 'Ed Summers',
3336
author_email = '[email protected]',
@@ -49,5 +52,6 @@
4952
'Programming Language :: Python :: 3.2',
5053
'Programming Language :: Python :: 3.3',
5154
'Programming Language :: Python :: 3.4',
55+
'Programming Language :: Python :: 3.5',
5256
],
5357
)

test.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,42 @@ def test_validate_optional_tagfile(self):
268268
bag = bagit.Bag(self.tmpdir)
269269
self.assertRaises(bagit.BagValidationError, self.validate, bag)
270270

271+
def test_validate_optional_tagfile_in_directory(self):
272+
bag = bagit.make_bag(self.tmpdir)
273+
tagdir = tempfile.mkdtemp(dir=self.tmpdir)
274+
275+
if not os.path.exists(j(tagdir, "tagfolder")):
276+
os.makedirs(j(tagdir, "tagfolder"))
277+
278+
with open(j(tagdir, "tagfolder", "tagfile"), "w") as tagfile:
279+
tagfile.write("test")
280+
relpath = j(tagdir, "tagfolder", "tagfile").replace(self.tmpdir + os.sep, "")
281+
relpath.replace("\\", "/")
282+
with open(j(self.tmpdir, "tagmanifest-md5.txt"), "w") as tagman:
283+
# Incorrect checksum.
284+
tagman.write("8e2af7a0143c7b8f4de0b3fc90f27354 " + relpath + "\n")
285+
bag = bagit.Bag(self.tmpdir)
286+
self.assertRaises(bagit.BagValidationError, self.validate, bag)
287+
288+
hasher = hashlib.new("md5")
289+
with open(j(tagdir, "tagfolder", "tagfile"), "r") as tf:
290+
contents = tf.read().encode('utf-8')
291+
hasher.update(contents)
292+
with open(j(self.tmpdir, "tagmanifest-md5.txt"), "w") as tagman:
293+
tagman.write(hasher.hexdigest() + " " + relpath + "\n")
294+
bag = bagit.Bag(self.tmpdir)
295+
self.assertTrue(self.validate(bag))
296+
297+
# Missing tagfile.
298+
os.remove(j(tagdir, "tagfolder", "tagfile"))
299+
bag = bagit.Bag(self.tmpdir)
300+
self.assertRaises(bagit.BagValidationError, self.validate, bag)
301+
271302
def test_sha1_tagfile(self):
272303
info = {'Bagging-Date': '1970-01-01', 'Contact-Email': '[email protected]'}
273304
bag = bagit.make_bag(self.tmpdir, checksum=['sha1'], bag_info=info)
274305
self.assertTrue(os.path.isfile(j(self.tmpdir, 'tagmanifest-sha1.txt')))
275-
self.assertEqual(bag.entries['bag-info.txt']['sha1'], 'd7f086508df433e5d7464b5a3835d5501df14404')
306+
self.assertEqual(bag.entries['bag-info.txt']['sha1'], 'ec70407d895d4e550bc0a7ea40a82ad653d136e5')
276307

277308
def test_validate_unreadable_file(self):
278309
bag = bagit.make_bag(self.tmpdir, checksum=["md5"])
@@ -329,15 +360,15 @@ def test_make_bag(self):
329360
self.assertTrue('Contact-Email: [email protected]' in bag_info_txt)
330361
self.assertTrue('Bagging-Date: 1970-01-01' in bag_info_txt)
331362
self.assertTrue('Payload-Oxum: 991765.5' in bag_info_txt)
332-
self.assertTrue('Bag-Software-Agent: bagit.py <http://github.com/libraryofcongress/bagit-python>' in bag_info_txt)
363+
self.assertTrue('Bag-Software-Agent: bagit.py v1.5.4 <http://github.com/libraryofcongress/bagit-python>' in bag_info_txt)
333364

334365
# check tagmanifest-md5.txt
335366
self.assertTrue(os.path.isfile(j(self.tmpdir, 'tagmanifest-md5.txt')))
336367
with open(j(self.tmpdir, 'tagmanifest-md5.txt')) as tm:
337368
tagmanifest_txt = tm.read()
338369
self.assertTrue('9e5ad981e0d29adc278f6a294b8c2aca bagit.txt' in tagmanifest_txt)
339370
self.assertTrue('a0ce6631a2a6d1a88e6d38453ccc72a5 manifest-md5.txt' in tagmanifest_txt)
340-
self.assertTrue('6a5090e27cb29d5dda8a0142fbbdf37e bag-info.txt' in tagmanifest_txt)
371+
self.assertTrue('bfe59ad8af1a227d27c191b4178c399f bag-info.txt' in tagmanifest_txt)
341372

342373
def test_make_bag_sha1_manifest(self):
343374
bagit.make_bag(self.tmpdir, checksum=['sha1'])

0 commit comments

Comments
 (0)