-
Notifications
You must be signed in to change notification settings - Fork 91
Fix issue 68 #69
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
Fix issue 68 #69
Changes from 2 commits
5b3ff56
2e4c517
db6e663
79b9af5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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): | ||
for dirName, subdirList, fileList in os.walk(bag_dir): | ||
|
||
if not re.match(r'.*data$', dirName): | ||
for filename in fileList: | ||
if re.match(r'^tagmanifest-.+\.txt$', filename): | ||
|
||
continue | ||
#remove everything up to the bag_dir directory | ||
p=join(dirName, filename) | ||
|
||
yield p[len(bag_dir)+1:] | ||
|
||
|
||
def _walk(data_dir): | ||
for dirpath, dirnames, filenames in os.walk(data_dir): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,6 +267,37 @@ def test_validate_optional_tagfile(self): | |
os.remove(j(tagdir, "tagfile")) | ||
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': '[email protected]'} | ||
|
There was a problem hiding this comment.
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.