|
63 | 63 | # - Sets the executable permission (`stat.S_IRWXU`) for the generated scripts on POSIX systems. |
64 | 64 |
|
65 | 65 | from bs4 import BeautifulSoup |
| 66 | +import atexit |
66 | 67 | import logging |
67 | 68 | import re |
68 | 69 | import sys |
|
104 | 105 | M_IS_OCTAVE = True #treat .m as octave 9/27/21 |
105 | 106 |
|
106 | 107 | if os.path.exists(CONCOREPATH+"/concore.mcr"): # 11/12/21 |
107 | | - MCRPATH = open(CONCOREPATH+"/concore.mcr", "r").readline().strip() #path to local Ubunta Matlab Compiler Runtime |
| 108 | + with open(CONCOREPATH+"/concore.mcr", "r") as f: |
| 109 | + MCRPATH = f.readline().strip() #path to local Ubunta Matlab Compiler Runtime |
108 | 110 |
|
109 | 111 | if os.path.exists(CONCOREPATH+"/concore.sudo"): # 12/04/21 |
110 | | - DOCKEREXE = open(CONCOREPATH+"/concore.sudo", "r").readline().strip() #to omit sudo in docker |
| 112 | + with open(CONCOREPATH+"/concore.sudo", "r") as f: |
| 113 | + DOCKEREXE = f.readline().strip() #to omit sudo in docker |
111 | 114 |
|
112 | 115 | if os.path.exists(CONCOREPATH+"/concore.repo"): # 12/04/21 |
113 | | - DOCKEREPO = open(CONCOREPATH+"/concore.repo", "r").readline().strip() #docker id for repo |
| 116 | + with open(CONCOREPATH+"/concore.repo", "r") as f: |
| 117 | + DOCKEREPO = f.readline().strip() #docker id for repo |
114 | 118 |
|
115 | 119 |
|
116 | 120 | prefixedgenode = "" |
|
166 | 170 | funlock = open("unlock", "w") # 12/4/21 |
167 | 171 | fparams = open("params", "w") # 9/18/22 |
168 | 172 |
|
| 173 | +def cleanup_script_files(): |
| 174 | + for fh in [fbuild, frun, fdebug, fstop, fclear, fmaxtime, funlock, fparams]: |
| 175 | + if not fh.closed: |
| 176 | + fh.close() |
| 177 | +atexit.register(cleanup_script_files) |
| 178 | + |
169 | 179 | os.mkdir("src") |
170 | 180 | os.chdir("..") |
171 | 181 |
|
|
179 | 189 | logging.info(f"MCR path: {MCRPATH}") |
180 | 190 | logging.info(f"Docker repository: {DOCKEREPO}") |
181 | 191 |
|
182 | | -f = open(GRAPHML_FILE, "r") |
183 | | -text_str = f.read() |
| 192 | +with open(GRAPHML_FILE, "r") as f: |
| 193 | + text_str = f.read() |
184 | 194 |
|
185 | 195 | soup = BeautifulSoup(text_str, 'xml') |
186 | 196 |
|
|
410 | 420 | #copy proper concore.py into /src |
411 | 421 | try: |
412 | 422 | if concoretype=="docker": |
413 | | - fsource = open(CONCOREPATH+"/concoredocker.py") |
| 423 | + with open(CONCOREPATH+"/concoredocker.py") as fsource: |
| 424 | + source_content = fsource.read() |
414 | 425 | else: |
415 | | - fsource = open(CONCOREPATH+"/concore.py") |
| 426 | + with open(CONCOREPATH+"/concore.py") as fsource: |
| 427 | + source_content = fsource.read() |
416 | 428 | except (FileNotFoundError, IOError) as e: |
417 | 429 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
418 | 430 | quit() |
419 | 431 | with open(outdir+"/src/concore.py","w") as fcopy: |
420 | | - fcopy.write(fsource.read()) |
421 | | -fsource.close() |
| 432 | + fcopy.write(source_content) |
422 | 433 |
|
423 | 434 | #copy proper concore.hpp into /src 6/22/21 |
424 | 435 | try: |
425 | 436 | if concoretype=="docker": |
426 | | - fsource = open(CONCOREPATH+"/concoredocker.hpp") |
| 437 | + with open(CONCOREPATH+"/concoredocker.hpp") as fsource: |
| 438 | + source_content = fsource.read() |
427 | 439 | else: |
428 | | - fsource = open(CONCOREPATH+"/concore.hpp") |
| 440 | + with open(CONCOREPATH+"/concore.hpp") as fsource: |
| 441 | + source_content = fsource.read() |
429 | 442 | except (FileNotFoundError, IOError) as e: |
430 | 443 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
431 | 444 | quit() |
432 | 445 | with open(outdir+"/src/concore.hpp","w") as fcopy: |
433 | | - fcopy.write(fsource.read()) |
434 | | -fsource.close() |
| 446 | + fcopy.write(source_content) |
435 | 447 |
|
436 | 448 | #copy proper concore.v into /src 6/25/21 |
437 | 449 | try: |
438 | 450 | if concoretype=="docker": |
439 | | - fsource = open(CONCOREPATH+"/concoredocker.v") |
| 451 | + with open(CONCOREPATH+"/concoredocker.v") as fsource: |
| 452 | + source_content = fsource.read() |
440 | 453 | else: |
441 | | - fsource = open(CONCOREPATH+"/concore.v") |
| 454 | + with open(CONCOREPATH+"/concore.v") as fsource: |
| 455 | + source_content = fsource.read() |
442 | 456 | except (FileNotFoundError, IOError) as e: |
443 | 457 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
444 | 458 | quit() |
445 | 459 | with open(outdir+"/src/concore.v","w") as fcopy: |
446 | | - fcopy.write(fsource.read()) |
447 | | -fsource.close() |
| 460 | + fcopy.write(source_content) |
448 | 461 |
|
449 | 462 | #copy mkcompile into /src 5/27/21 |
450 | 463 | try: |
451 | | - fsource = open(CONCOREPATH+"/mkcompile") |
| 464 | + with open(CONCOREPATH+"/mkcompile") as fsource: |
| 465 | + source_content = fsource.read() |
452 | 466 | except (FileNotFoundError, IOError) as e: |
453 | 467 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
454 | 468 | quit() |
455 | 469 | with open(outdir+"/src/mkcompile","w") as fcopy: |
456 | | - fcopy.write(fsource.read()) |
457 | | -fsource.close() |
| 470 | + fcopy.write(source_content) |
458 | 471 | os.chmod(outdir+"/src/mkcompile",stat.S_IRWXU) |
459 | 472 |
|
460 | 473 | #copy concore*.m into /src 4/2/21 |
461 | 474 | try: #maxtime in matlab 11/22/21 |
462 | | - fsource = open(CONCOREPATH+"/concore_default_maxtime.m") |
| 475 | + with open(CONCOREPATH+"/concore_default_maxtime.m") as fsource: |
| 476 | + source_content = fsource.read() |
463 | 477 | except (FileNotFoundError, IOError) as e: |
464 | 478 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
465 | 479 | quit() |
466 | 480 | with open(outdir+"/src/concore_default_maxtime.m","w") as fcopy: |
467 | | - fcopy.write(fsource.read()) |
468 | | -fsource.close() |
| 481 | + fcopy.write(source_content) |
469 | 482 | try: |
470 | | - fsource = open(CONCOREPATH+"/concore_unchanged.m") |
| 483 | + with open(CONCOREPATH+"/concore_unchanged.m") as fsource: |
| 484 | + source_content = fsource.read() |
471 | 485 | except (FileNotFoundError, IOError) as e: |
472 | 486 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
473 | 487 | quit() |
474 | 488 | with open(outdir+"/src/concore_unchanged.m","w") as fcopy: |
475 | | - fcopy.write(fsource.read()) |
476 | | -fsource.close() |
| 489 | + fcopy.write(source_content) |
477 | 490 | try: |
478 | | - fsource = open(CONCOREPATH+"/concore_read.m") |
| 491 | + with open(CONCOREPATH+"/concore_read.m") as fsource: |
| 492 | + source_content = fsource.read() |
479 | 493 | except (FileNotFoundError, IOError) as e: |
480 | 494 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
481 | 495 | quit() |
482 | 496 | with open(outdir+"/src/concore_read.m","w") as fcopy: |
483 | | - fcopy.write(fsource.read()) |
484 | | -fsource.close() |
| 497 | + fcopy.write(source_content) |
485 | 498 | try: |
486 | | - fsource = open(CONCOREPATH+"/concore_write.m") |
| 499 | + with open(CONCOREPATH+"/concore_write.m") as fsource: |
| 500 | + source_content = fsource.read() |
487 | 501 | except (FileNotFoundError, IOError) as e: |
488 | 502 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
489 | 503 | quit() |
490 | 504 | with open(outdir+"/src/concore_write.m","w") as fcopy: |
491 | | - fcopy.write(fsource.read()) |
492 | | -fsource.close() |
| 505 | + fcopy.write(source_content) |
493 | 506 | try: #4/9/21 |
494 | | - fsource = open(CONCOREPATH+"/concore_initval.m") |
| 507 | + with open(CONCOREPATH+"/concore_initval.m") as fsource: |
| 508 | + source_content = fsource.read() |
495 | 509 | except (FileNotFoundError, IOError) as e: |
496 | 510 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
497 | 511 | quit() |
498 | 512 | with open(outdir+"/src/concore_initval.m","w") as fcopy: |
499 | | - fcopy.write(fsource.read()) |
500 | | -fsource.close() |
| 513 | + fcopy.write(source_content) |
501 | 514 | try: #11/19/21 |
502 | | - fsource = open(CONCOREPATH+"/concore_iport.m") |
| 515 | + with open(CONCOREPATH+"/concore_iport.m") as fsource: |
| 516 | + source_content = fsource.read() |
503 | 517 | except (FileNotFoundError, IOError) as e: |
504 | 518 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
505 | 519 | quit() |
506 | 520 | with open(outdir+"/src/concore_iport.m","w") as fcopy: |
507 | | - fcopy.write(fsource.read()) |
508 | | -fsource.close() |
| 521 | + fcopy.write(source_content) |
509 | 522 | try: #11/19/21 |
510 | | - fsource = open(CONCOREPATH+"/concore_oport.m") |
| 523 | + with open(CONCOREPATH+"/concore_oport.m") as fsource: |
| 524 | + source_content = fsource.read() |
511 | 525 | except (FileNotFoundError, IOError) as e: |
512 | 526 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
513 | 527 | quit() |
514 | 528 | with open(outdir+"/src/concore_oport.m","w") as fcopy: |
515 | | - fcopy.write(fsource.read()) |
516 | | -fsource.close() |
| 529 | + fcopy.write(source_content) |
517 | 530 | try: # 4/4/21 |
518 | 531 | if concoretype=="docker": |
519 | | - fsource = open(CONCOREPATH+"/import_concoredocker.m") |
| 532 | + with open(CONCOREPATH+"/import_concoredocker.m") as fsource: |
| 533 | + source_content = fsource.read() |
520 | 534 | else: |
521 | | - fsource = open(CONCOREPATH+"/import_concore.m") |
| 535 | + with open(CONCOREPATH+"/import_concore.m") as fsource: |
| 536 | + source_content = fsource.read() |
522 | 537 | except (FileNotFoundError, IOError) as e: |
523 | 538 | logging.error(f"{CONCOREPATH} is not correct path to concore: {e}") |
524 | 539 | quit() |
525 | 540 | with open(outdir+"/src/import_concore.m","w") as fcopy: |
526 | | - fcopy.write(fsource.read()) |
527 | | -fsource.close() |
| 541 | + fcopy.write(source_content) |
528 | 542 |
|
529 | 543 | # --- Generate iport and oport mappings --- |
530 | 544 | logging.info("Generating iport/oport mappings...") |
|
593 | 607 | if not os.path.exists(outdir+"/src/Dockerfile."+dockername): # 3/30/21 |
594 | 608 | try: |
595 | 609 | if langext=="py": |
596 | | - fsource = open(CONCOREPATH+"/Dockerfile.py") |
| 610 | + src_path = CONCOREPATH+"/Dockerfile.py" |
597 | 611 | logging.info("assuming .py extension for Dockerfile") |
598 | 612 | elif langext == "cpp": # 6/22/21 |
599 | | - fsource = open(CONCOREPATH+"/Dockerfile.cpp") |
| 613 | + src_path = CONCOREPATH+"/Dockerfile.cpp" |
600 | 614 | logging.info("assuming .cpp extension for Dockerfile") |
601 | 615 | elif langext == "v": # 6/26/21 |
602 | | - fsource = open(CONCOREPATH+"/Dockerfile.v") |
| 616 | + src_path = CONCOREPATH+"/Dockerfile.v" |
603 | 617 | logging.info("assuming .v extension for Dockerfile") |
604 | 618 | elif langext == "sh": # 5/19/21 |
605 | | - fsource = open(CONCOREPATH+"/Dockerfile.sh") |
| 619 | + src_path = CONCOREPATH+"/Dockerfile.sh" |
606 | 620 | logging.info("assuming .sh extension for Dockerfile") |
607 | 621 | else: |
608 | | - fsource = open(CONCOREPATH+"/Dockerfile.m") |
| 622 | + src_path = CONCOREPATH+"/Dockerfile.m" |
609 | 623 | logging.info("assuming .m extension for Dockerfile") |
| 624 | + with open(src_path) as fsource: |
| 625 | + source_content = fsource.read() |
610 | 626 | except: |
611 | 627 | logging.error(f"{CONCOREPATH} is not correct path to concore") |
612 | 628 | quit() |
613 | 629 | with open(outdir+"/src/Dockerfile."+dockername,"w") as fcopy: |
614 | | - fcopy.write(fsource.read()) |
| 630 | + fcopy.write(source_content) |
615 | 631 | if langext=="py": |
616 | 632 | fcopy.write('CMD ["python", "-i", "'+sourcecode+'"]\n') |
617 | 633 | if langext=="m": |
|
622 | 638 | if langext=="v": |
623 | 639 | fcopy.write('RUN iverilog ./'+sourcecode+'\n') # 7/02/21 |
624 | 640 | fcopy.write('CMD ["./a.out"]\n') # 7/02/21 |
625 | | - fsource.close() |
626 | 641 |
|
627 | 642 | fbuild.write('#!/bin/bash' + "\n") |
628 | 643 | for node in nodes_dict: |
|
1110 | 1125 | frun.close() |
1111 | 1126 | fbuild.close() |
1112 | 1127 | fdebug.close() |
1113 | | -fstop.close() |
1114 | | -fclear.close() |
1115 | | -fmaxtime.close() |
1116 | | -fparams.close() |
1117 | 1128 | if concoretype != "windows": |
1118 | 1129 | os.chmod(outdir+"/build",stat.S_IRWXU) |
1119 | 1130 | os.chmod(outdir+"/run",stat.S_IRWXU) |
|
0 commit comments