artiq_flash: use atexit for tempfile cleanup.

This commit is contained in:
whitequark 2018-01-20 06:21:32 +00:00
parent ab9eb56ceb
commit 1ffabac06f
1 changed files with 10 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import subprocess
import tempfile import tempfile
import shutil import shutil
import re import re
import atexit
from functools import partial from functools import partial
from artiq import __artiq_dir__ as artiq_dir from artiq import __artiq_dir__ as artiq_dir
@ -279,7 +280,6 @@ def main():
programmer = config["programmer_factory"](client, preinit_script=args.preinit_command) programmer = config["programmer_factory"](client, preinit_script=args.preinit_command)
conv = False
for action in args.action: for action in args.action:
if action == "proxy": if action == "proxy":
proxy_found = False proxy_found = False
@ -298,14 +298,14 @@ def main():
path = bin_dir path = bin_dir
else: else:
path = os.path.join(args.srcbuild, "gateware") path = os.path.join(args.srcbuild, "gateware")
bin = os.path.join(path, "top.bin") bin_filename = os.path.join(path, "top.bin")
if not os.access(bin, os.R_OK): if not os.access(bin_filename, os.R_OK):
bin_handle, bin = tempfile.mkstemp() bin_handle, bin_filename = tempfile.mkstemp()
bit = os.path.join(path, "top.bit") bit_filename = os.path.join(path, "top.bit")
with open(bit, "rb") as f, open(bin_handle, "wb") as g: with open(bit_filename, "rb") as bit_handle:
bit2bin(f, g) bit2bin(bit_handle, bin_handle)
conv = True atexit.register(lambda: os.unlink(bin_filename))
programmer.flash_binary(*config["gateware"], bin) programmer.flash_binary(*config["gateware"], bin_filename)
elif action == "bootloader": elif action == "bootloader":
if args.srcbuild is None: if args.srcbuild is None:
path = bin_dir path = bin_dir
@ -339,11 +339,7 @@ def main():
if args.dry_run: if args.dry_run:
print("\n".join(programmer.script())) print("\n".join(programmer.script()))
else: else:
try: programmer.run()
programmer.run()
finally:
if conv:
os.unlink(bin)
if __name__ == "__main__": if __name__ == "__main__":