build.run: fix product extraction to work on Windows.
authorwhitequark <whitequark@whitequark.org>
Tue, 4 Jun 2019 11:40:56 +0000 (11:40 +0000)
committerwhitequark <whitequark@whitequark.org>
Tue, 4 Jun 2019 11:40:56 +0000 (11:40 +0000)
Before this commit, it would fail with a "Permission denied" error.

nmigen/build/run.py

index 16992f92e754dbb1f645b315ead97c9ba0062f46..251ba3e17938e4dd15fd7700a9a49c42143e1458 100644 (file)
@@ -68,9 +68,14 @@ class BuildProducts:
         files = []
         try:
             for filename in filenames:
-                file = tempfile.NamedTemporaryFile(prefix="nmigen_", suffix="_" + filename)
+                # On Windows, a named temporary file (as created by Python) is not accessible to
+                # others if it's still open within the Python process, so we close it and delete
+                # it manually.
+                file = tempfile.NamedTemporaryFile(prefix="nmigen_", suffix="_" + filename,
+                                                   delete=False)
                 files.append(file)
                 file.write(self.get(filename))
+                file.close()
 
             if len(files) == 0:
                 return (yield)
@@ -80,4 +85,4 @@ class BuildProducts:
                 return (yield [file.name for file in files])
         finally:
             for file in files:
-                file.close()
+                os.unlink(file.name)