tempfiles.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #
  2. # tempfiles module - Temporary file handling for reportbug
  3. # Written by Chris Lawrence <lawrencc@debian.org>
  4. # (C) 1999-2008 Chris Lawrence
  5. # Copyright (C) 2008-2016 Sandro Tosi <morph@debian.org>
  6. #
  7. # This program is freely distributable per the following license:
  8. #
  9. # Permission to use, copy, modify, and distribute this software and its
  10. # documentation for any purpose and without fee is hereby granted,
  11. # provided that the above copyright notice appears in all copies and that
  12. # both that copyright notice and this permission notice appear in
  13. # supporting documentation.
  14. #
  15. # I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I
  17. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  18. # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. # SOFTWARE.
  22. import os
  23. import tempfile
  24. import time
  25. def tempfile_prefix(package=None, extra=None):
  26. if extra:
  27. if package:
  28. package = '%s-%s' % (package, extra)
  29. else:
  30. package = extra
  31. if package:
  32. return 'reportbug-%s-%s-%d-' % (
  33. package, time.strftime('%Y%m%d'), os.getpid())
  34. return 'reportbug-%s-%d-' % (time.strftime('%Y%m%d'), os.getpid())
  35. template = tempfile_prefix()
  36. # Derived version of mkstemp that returns a Python file object
  37. _text_openflags = os.O_RDWR | os.O_CREAT | os.O_EXCL
  38. if hasattr(os, 'O_NOINHERIT'):
  39. _text_openflags |= os.O_NOINHERIT
  40. if hasattr(os, 'O_NOFOLLOW'):
  41. _text_openflags |= os.O_NOFOLLOW
  42. _bin_openflags = _text_openflags
  43. if hasattr(os, 'O_BINARY'):
  44. _bin_openflags |= os.O_BINARY
  45. # Safe open, prevents filename races in shared tmp dirs
  46. # Based on python-1.5.2/Lib/tempfile.py
  47. def open_write_safe(filename, mode='w+b', bufsize=-1):
  48. if 'b' in mode:
  49. fd = os.open(filename, _bin_openflags, 0600)
  50. else:
  51. fd = os.open(filename, _text_openflags, 0600)
  52. try:
  53. return os.fdopen(fd, mode, bufsize)
  54. except:
  55. os.close(fd)
  56. raise
  57. # Wrapper for mkstemp; main difference is that text defaults to True, and it
  58. # returns a Python file object instead of an os-level file descriptor
  59. def TempFile(suffix="", prefix=template, dir=None, text=True,
  60. mode="w+", bufsize=-1):
  61. fh, filename = tempfile.mkstemp(suffix, prefix, dir, text)
  62. fd = os.fdopen(fh, mode, bufsize)
  63. return (fd, filename)
  64. def cleanup_temp_file(temp_filename):
  65. """ Clean up a temporary file.
  66. :parameters:
  67. `temp_filename`
  68. Full filename of the file to clean up.
  69. :return value:
  70. None
  71. Removes (unlinks) the named file if it exists.
  72. """
  73. if os.path.exists(temp_filename):
  74. os.unlink(temp_filename)