# along with this program. If not, see <https://www.gnu.org/licenses/>.
import configparser
+from contextlib import contextmanager
import filecmp
import functools
import json
return getpass("Blackboard password: ")
+@contextmanager
+def deleting(path):
+ yield
+ path.unlink()
+
+
cfg_parser = configparser.ConfigParser()
with open('config.ini') as f:
cfg_parser.read_file(f)
log('info', "Checking attachment {id}: \"{fileName}\"".format(**attachment_doc), indent = 1)
- with tempfile.NamedTemporaryFile() as tmp:
+ # On Windows, can't reopen the temp file while it's open.
+ # This is needed by `filecmp', so we can't use `tempfile''s auto deletion.
+ with tempfile.NamedTemporaryFile(delete = False) 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)
+ with deleting(tmp_path):
class Result:
NoVersions = namedtuple('NoVersions', [])
MultipleLatest = namedtuple('MultipleLatest', ['paths'])
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)
+ shutil.copy2(str(tmp_path), str(dest))
fs.write_metadata(dest, new_version)
elif isinstance(result, Result.SingleLatest):
# versions match