|
5 | 5 | import glob |
6 | 6 | import time |
7 | 7 | import shutil |
| 8 | +import tempfile |
8 | 9 | import tarfile |
9 | 10 |
|
10 | 11 | from gratia.common.config import ConfigProxy |
@@ -409,18 +410,8 @@ def SearchOutstandingRecord(): |
409 | 410 |
|
410 | 411 | def GenerateFilename(prefix, current_dir): |
411 | 412 | '''Generate a filename of the for current_dir/prefix.$pid.ConfigFragment.gratia.xml__Unique''' |
412 | | - filename = prefix + str(global_state.RecordPid) + '.' + Config.get_GratiaExtension() \ |
413 | | - + '__XXXXXXXXXX' |
414 | | - filename = os.path.join(current_dir, filename) |
415 | | - mktemp_pipe = os.popen('mktemp -q "' + filename + '"') |
416 | | - if mktemp_pipe != None: |
417 | | - filename = mktemp_pipe.readline() |
418 | | - mktemp_pipe.close() |
419 | | - filename = filename.strip() |
420 | | - if filename != r'': |
421 | | - return filename |
422 | | - |
423 | | - raise IOError |
| 413 | + fn_prefix = f'{prefix}.{global_state.RecordPid}.{Config.get_GratiaExtension()}__' |
| 414 | + return tempfile.NamedTemporaryFile(prefix=fn_prefix, dir=current_dir, delete=False, mode='w') |
424 | 415 |
|
425 | 416 | def UncompressOutbox(staging_name, target_dir): |
426 | 417 |
|
@@ -487,20 +478,21 @@ def CompressOutbox(probe_dir, outbox, outfiles): |
487 | 478 | DebugPrint(0, msg + ':' + exc) |
488 | 479 | raise InternalError(msg) from exc |
489 | 480 |
|
490 | | - staging_name = GenerateFilename('tz.', staged_store) |
491 | | - DebugPrint(1, 'Compressing outbox in tar.bz2 file: ' + staging_name) |
| 481 | + with GenerateFilename('tz', staged_store) as temp_tarfile: |
| 482 | + staging_name = temp_tarfile.name |
| 483 | + DebugPrint(1, 'Compressing outbox in tar.bz2 file: ' + staging_name) |
492 | 484 |
|
493 | | - try: |
494 | | - tar = tarfile.open(staging_name, 'w:bz2') |
495 | | - except KeyboardInterrupt: |
496 | | - raise |
497 | | - except SystemExit: |
498 | | - raise |
499 | | - except Exception as e: |
500 | | - DebugPrint(0, 'Warning: Exception caught while opening tar.bz2 file: ' + staging_name + ':') |
501 | | - DebugPrint(0, 'Caught exception: ', e) |
502 | | - DebugPrintTraceback() |
503 | | - return False |
| 485 | + try: |
| 486 | + tar = tarfile.open(staging_name, 'w:bz2') |
| 487 | + except KeyboardInterrupt: |
| 488 | + raise |
| 489 | + except SystemExit: |
| 490 | + raise |
| 491 | + except Exception as e: |
| 492 | + DebugPrint(0, 'Warning: Exception caught while opening tar.bz2 file: ' + staging_name + ':') |
| 493 | + DebugPrint(0, 'Caught exception: ', e) |
| 494 | + DebugPrintTraceback() |
| 495 | + return False |
504 | 496 |
|
505 | 497 | try: |
506 | 498 | for f in outfiles: |
@@ -599,12 +591,11 @@ def OpenNewRecordFile(dirIndex): |
599 | 591 | raise InternalError(msg) from exc |
600 | 592 |
|
601 | 593 | try: |
602 | | - filename = GenerateFilename('r.', working_dir) |
603 | | - DebugPrint(3, 'Creating file:', filename) |
604 | | - outstandingRecordCount += 1 |
605 | | - f = open(filename, 'w') |
606 | | - dirIndex = index |
607 | | - return (f, dirIndex) |
| 594 | + with GenerateFilename('r', working_dir) as recordfile: |
| 595 | + DebugPrint(3, 'Creating file:', recordfile.name) |
| 596 | + outstandingRecordCount += 1 |
| 597 | + dirIndex = index |
| 598 | + return (recordfile, dirIndex) |
608 | 599 | except Exception as exc: |
609 | 600 | msg = 'ERROR: Caught exception while creating file' |
610 | 601 | DebugPrint(0, msg + ': ', exc) |
|
0 commit comments