Skip to content

Commit 5e05700

Browse files
committed
Rename fetch_files_to_be_fetched -> fetch, force option
1 parent 76fb26e commit 5e05700

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

bagit.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,12 @@ def files_to_be_fetched(self):
588588
for _, _, filename in self.fetch_entries():
589589
yield filename
590590

591-
def fetch_files_to_be_fetched(self):
591+
def fetch(self, force=False):
592592
"""
593593
Fetches files from the fetch.txt
594+
595+
Arguments:
596+
force (boolean): Fetch files even when they are present in the data directory
594597
"""
595598
proxy_handler = ProxyHandler() # will default to adhere to *_proxy env vars
596599
opener = build_opener(proxy_handler)
@@ -599,7 +602,7 @@ def fetch_files_to_be_fetched(self):
599602
if not fnmatch_any(url, self.fetch_url_whitelist):
600603
raise BagError(_("Malformed URL in fetch.txt: %s, matches none of the whitelisted URL patterns %s") % (url, self.fetch_url_whitelist))
601604
expected_size = -1 if expected_size == '-' else int(expected_size)
602-
if filename in self.payload_files():
605+
if filename in self.payload_files() and not force:
603606
LOGGER.info(_("File already fetched: %s"), filename)
604607
continue
605608
req = Request(url)

test.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,30 @@ def test_fetching_payload_file(self):
11301130
with self.assertRaises(bagit.BagError):
11311131
self.bag.validate()
11321132
# re-fetch it
1133-
self.bag.fetch_files_to_be_fetched()
1133+
self.bag.fetch()
1134+
# should be valid again
1135+
self.bag.validate()
1136+
self.assertEqual(len(self.bag.compare_fetch_with_fs()), 0, 'complete')
1137+
1138+
def test_force_fetching(self):
1139+
test_payload = 'loc/2478433644_2839c5e8b8_o_d.jpg'
1140+
with open(j(self.tmpdir, "fetch.txt"), "w") as fetch_txt:
1141+
print("https://github.com/LibraryOfCongress/bagit-python/raw/master/test-data/%s %s data/%s" % (
1142+
test_payload, 139367, test_payload), file=fetch_txt)
1143+
self.bag.save(manifests=True)
1144+
# now replace one payload file with an empty string
1145+
with open(j(self.tmpdir, "data", test_payload), 'w') as payload:
1146+
payload.write('')
1147+
# should be invalid now
1148+
with self.assertRaisesRegexp(bagit.BagError, "^Payload-Oxum validation failed."):
1149+
self.bag.validate()
1150+
# non-forcefully downloading should not help
1151+
self.bag.fetch()
1152+
# should **still* be invalid now
1153+
with self.assertRaisesRegexp(bagit.BagError, "^Payload-Oxum validation failed."):
1154+
self.bag.validate()
1155+
# fetch with force
1156+
self.bag.fetch(force=True)
11341157
# should be valid again
11351158
self.bag.validate()
11361159
self.assertEqual(len(self.bag.compare_fetch_with_fs()), 0, 'complete')

0 commit comments

Comments
 (0)