Archive to temp directory with fixed filename
Previously, the archive was generated at a randomized temporary path
before being sent to the client under a fixed name corresponding to
`repo.name + "-" + rev_escaped`

The temporary filename caused the archive checksums to vary between
downloads, despite the otherwise unchanged archive contents. If
instead the archive is created in a randomized _directory_ with a
fixed filename the generated archive checksum should not vary.

Intended to address: todo.sr.ht/~sircmpwn/hg.sr.ht/33
1 files changed, 10 insertions(+), 13 deletions(-)

M hgsrht/blueprints/repo.py
M hgsrht/blueprints/repo.py +10 -13
@@ 1,4 1,4 @@ 
-import binascii
+import tempfile
 import hashlib
 import io
 import json

          
@@ 644,22 644,19 @@ def archive(owner, repo, rev):
 
         rev = commit.display_name
         rev_escaped = rev.replace('/', '_')
-        path = f"/tmp/{rev_escaped}{binascii.hexlify(os.urandom(8))}.tar.gz"
         basename = repo.name + "-" + rev_escaped
-        try:
-            hg_repo.client.archive(path.encode(),
-                    rev=rev,
-                    prefix=basename,
-                    type="tgz")
-        except:
+
+        with tempfile.TemporaryDirectory() as tmpdir:
+            path = f"{tmpdir}/{rev_escaped}.tar.gz"
             try:
-                os.unlink(path)
+                hg_repo.client.archive(path.encode(),
+                        rev=rev,
+                        prefix=basename,
+                        type="tgz")
             except:
-                pass
-            raise
+                raise
 
-        f = open(path, "rb")
-        os.unlink(path)
+            f = open(path, "rb")
 
         return send_file(f,
                 as_attachment=True,