| OLD | NEW |
| 1 """ | 1 """ |
| 2 Autotest tempfile wrapper for mkstemp (known as tempfile here) and | 2 Autotest tempfile wrapper for mkstemp (known as tempfile here) and |
| 3 mkdtemp (known as tempdir). | 3 mkdtemp (known as tempdir). |
| 4 | 4 |
| 5 This wrapper provides a mechanism to clean up temporary files/dirs once they | 5 This wrapper provides a mechanism to clean up temporary files/dirs once they |
| 6 are no longer need. | 6 are no longer need. |
| 7 | 7 |
| 8 Files/Dirs will have a unique_id prepended to the suffix and a | 8 Files/Dirs will have a unique_id prepended to the suffix and a |
| 9 _autotmp_ tag appended to the prefix. | 9 _autotmp_ tag appended to the prefix. |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 @param unique_id: required, a unique string to help identify what | 25 @param unique_id: required, a unique string to help identify what |
| 26 part of code created the tempfile. | 26 part of code created the tempfile. |
| 27 @var name: The name of the temporary file. | 27 @var name: The name of the temporary file. |
| 28 @var fd: the file descriptor of the temporary file that was created. | 28 @var fd: the file descriptor of the temporary file that was created. |
| 29 @return a tempfile object | 29 @return a tempfile object |
| 30 example usage: | 30 example usage: |
| 31 t = autotemp.tempfile(unique_id='fig') | 31 t = autotemp.tempfile(unique_id='fig') |
| 32 t.name # name of file | 32 t.name # name of file |
| 33 t.fd # file descriptor | 33 t.fd # file descriptor |
| 34 t.fo # file object |
| 34 t.clean() # clean up after yourself | 35 t.clean() # clean up after yourself |
| 35 """ | 36 """ |
| 36 def __init__(self, unique_id, suffix='', prefix='', dir=None, | 37 def __init__(self, unique_id, suffix='', prefix='', dir=None, |
| 37 text=False): | 38 text=False): |
| 38 suffix = unique_id + suffix | 39 suffix = unique_id + suffix |
| 39 prefix = prefix + _TEMPLATE | 40 prefix = prefix + _TEMPLATE |
| 40 self.fd, self.name = module_tempfile.mkstemp(suffix=suffix, | 41 self.fd, self.name = module_tempfile.mkstemp(suffix=suffix, |
| 41 prefix=prefix, | 42 prefix=prefix, |
| 42 dir=dir, text=text) | 43 dir=dir, text=text) |
| 44 self.fo = os.fdopen(self.fd) |
| 43 | 45 |
| 44 | 46 |
| 45 def clean(self): | 47 def clean(self): |
| 46 """ | 48 """ |
| 47 Remove the temporary file that was created. | 49 Remove the temporary file that was created. |
| 48 This is also called by the destructor. | 50 This is also called by the destructor. |
| 49 """ | 51 """ |
| 52 if self.fo: |
| 53 self.fo.close() |
| 50 if self.name and os.path.exists(self.name): | 54 if self.name and os.path.exists(self.name): |
| 51 os.remove(self.name) | 55 os.remove(self.name) |
| 52 | 56 |
| 53 self.fd = self.name = None | 57 self.fd = self.fo = self.name = None |
| 54 | 58 |
| 55 | 59 |
| 56 def __del__(self): | 60 def __del__(self): |
| 57 try: | 61 try: |
| 58 if self.name: | 62 if self.name is not None: |
| 59 logging.debug('Clean was not called for ' + self.name) | 63 logging.debug('Clean was not called for ' + self.name) |
| 60 self.clean() | 64 self.clean() |
| 61 except: | 65 except: |
| 62 try: | 66 try: |
| 63 msg = 'An exception occurred while calling the destructor' | 67 msg = 'An exception occurred while calling the destructor' |
| 64 logging.exception(msg) | 68 logging.exception(msg) |
| 65 except: | 69 except: |
| 66 pass | 70 pass |
| 67 | 71 |
| 68 | 72 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 99 try: | 103 try: |
| 100 if self.name: | 104 if self.name: |
| 101 logging.debug('Clean was not called for ' + self.name) | 105 logging.debug('Clean was not called for ' + self.name) |
| 102 self.clean() | 106 self.clean() |
| 103 except: | 107 except: |
| 104 try: | 108 try: |
| 105 msg = 'An exception occurred while calling the destructor' | 109 msg = 'An exception occurred while calling the destructor' |
| 106 logging.exception(msg) | 110 logging.exception(msg) |
| 107 except: | 111 except: |
| 108 pass | 112 pass |
| OLD | NEW |