log('info', "Checking attachment {id}: \"{fileName}\"".format(**attachment_doc), indent = 1)
- class Result:
- NoVersions = namedtuple('NoVersions', [])
- MultipleLatest = namedtuple('MultipleLatest', ['paths'])
- SingleLatest = namedtuple('SingleLatest', ['version', 'matches'])
-
- @staticmethod
- def to_update(result):
- return (
- isinstance(result, Result.SingleLatest) and not result.matches
- or isinstance(result, Result.NoVersions)
- )
-
- my_versions = [info for info in versions.keys() if info.bb_id == att_id]
- if my_versions:
- latest = max(my_versions, key = attrgetter('version'))
- latest_paths = versions[latest]
- if len(latest_paths) == 1:
- [latest_path] = latest_paths
- with api_iface.download_attachment(course_id, child_doc['id'], att_id) as resp:
- with tempfile.NamedTemporaryFile(delete = False) as tmp:
- tmp_path = Path(tmp.name)
- shutil.copyfileobj(resp, tmp)
- matches = filecmp.cmp(str(tmp_path), str(latest_path), shallow = False)
- result = Result.SingleLatest(latest, matches)
+ with tempfile.NamedTemporaryFile() as tmp:
+ tmp_path = Path(tmp.name)
+ with api_iface.download_attachment(course_id, child_doc['id'], att_id) as resp:
+ shutil.copyfileobj(resp, tmp)
+ tmp.seek(0)
+
+ class Result:
+ NoVersions = namedtuple('NoVersions', [])
+ MultipleLatest = namedtuple('MultipleLatest', ['paths'])
+ SingleLatest = namedtuple('SingleLatest', ['version', 'matches'])
+
+ @staticmethod
+ def to_update(result):
+ return (
+ isinstance(result, Result.SingleLatest) and not result.matches
+ or isinstance(result, Result.NoVersions)
+ )
+
+ my_versions = [info for info in versions.keys() if info.bb_id == att_id]
+ if my_versions:
+ latest = max(my_versions, key = attrgetter('version'))
+ latest_paths = versions[latest]
+ if len(latest_paths) == 1:
+ [latest_path] = latest_paths
+ matches = filecmp.cmp(str(tmp_path), str(latest_path), shallow = False)
+ result = Result.SingleLatest(latest, matches)
+ else:
+ result = Result.MultipleLatest(latest_paths)
else:
- result = Result.MultipleLatest(latest_paths)
- else:
- result = Result.NoVersions()
-
- if Result.to_update(result):
- if isinstance(result, Result.SingleLatest):
- new_version = result.version.next()
- log('info', "Found new revision ({})".format(new_version.version), indent = 2)
+ result = Result.NoVersions()
+
+ if Result.to_update(result):
+ if isinstance(result, Result.SingleLatest):
+ new_version = result.version.next()
+ log('info', "Found new revision ({})".format(new_version.version), indent = 2)
+ else:
+ new_version = fs.VersionInfo(att_id, 0)
+ log('info', "Storing initial revision", indent = 2)
+ dest = fs.get_new_path(local_path, attachment_doc['fileName'])
+ log('info', "Destination: {}".format(dest), indent = 2)
+ tmp.seek(0)
+ with dest.open('wb') as f:
+ shutil.copyfileobj(tmp, f)
+ fs.write_metadata(dest, new_version)
+ elif isinstance(result, Result.SingleLatest):
+ # versions match
+ pass
+ elif isinstance(result, Result.MultipleLatest):
+ log(
+ 'error',
+ "Identified multiple latest versions for {id}: {fileName}".format(**attachment_doc),
+ indent = 2,
+ )
+ log(
+ 'error',
+ "To allow the program to check this attachment, delete all or all but one of these files:",
+ indent = 2,
+ )
+ for path_str in sorted(map(str, result.paths)):
+ log('error', path_str, indent = 3)
else:
- new_version = fs.VersionInfo(att_id, 0)
- log('info', "Storing initial revision", indent = 2)
- dest = fs.get_new_path(local_path, attachment_doc['fileName'])
- log('info', "Destination: {}".format(dest), indent = 2)
- tmp_path.replace(dest)
- fs.write_metadata(dest, new_version)
- elif isinstance(result, Result.SingleLatest):
- # versions match
- tmp_path.unlink()
- elif isinstance(result, Result.MultipleLatest):
- log(
- 'error',
- "Identified multiple latest versions for {id}: {fileName}".format(**attachment_doc),
- indent = 2,
- )
- log(
- 'error',
- "To allow the program to check this attachment, delete all or all but one of these files:",
- indent = 2,
- )
- for path_str in sorted(map(str, result.paths)):
- log('error', path_str, indent = 3)
- else:
- raise AssertionError()
+ raise AssertionError()