rltempfile.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #Copyright ReportLab Europe Ltd. 2000-2017
  2. #see license.txt for license details
  3. # $URI:$
  4. __version__='3.3.0'
  5. __doc__='''Helper for the test suite - determines where to write output.
  6. When our test suite runs as source, a script "test_foo.py" will typically
  7. create "test_foo.pdf" alongside it. But if you are testing a package of
  8. compiled code inside a zip archive, this won't work. This determines
  9. where to write test suite output, creating a subdirectory of /tmp/ or
  10. whatever if needed.
  11. '''
  12. _rl_tempdir=None
  13. __all__ = ('get_rl_tempdir', 'get_rl_tempdir')
  14. import os, tempfile
  15. def _rl_getuid():
  16. if hasattr(os,'getuid'):
  17. return os.getuid()
  18. else:
  19. return ''
  20. def get_rl_tempdir(*subdirs):
  21. global _rl_tempdir
  22. if _rl_tempdir is None:
  23. _rl_tempdir = os.path.join(tempfile.gettempdir(),'ReportLab_tmp%s' % str(_rl_getuid()))
  24. d = _rl_tempdir
  25. if subdirs: d = os.path.join(*((d,)+subdirs))
  26. try:
  27. os.makedirs(d)
  28. except:
  29. pass
  30. return d
  31. def get_rl_tempfile(fn=None):
  32. if not fn:
  33. fn = tempfile.mktemp()
  34. return os.path.join(get_rl_tempdir(),fn)