src/reportlab/lib/rltempfile.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
2625
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
     3
# $URI:$
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3617
diff changeset
     4
__version__='3.3.0'
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     5
__doc__='''Helper for the test suite - determines where to write output.
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     6
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     7
When our test suite runs as source, a script "test_foo.py" will typically
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     8
create "test_foo.pdf" alongside it.  But if you are testing a package of
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     9
compiled code inside a zip archive, this won't work.  This determines
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    10
where to write test suite output, creating a subdirectory of /tmp/ or
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    11
whatever if needed.
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    12
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    13
'''
2625
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    14
_rl_tempdir=None
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    15
__all__ = ('get_rl_tempdir', 'get_rl_tempdir')
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    16
import os, tempfile
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    17
def _rl_getuid():
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    18
    if hasattr(os,'getuid'):
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    19
        return os.getuid()
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    20
    else:
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    21
        return ''
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    22
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    23
def get_rl_tempdir(*subdirs):
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    24
    global _rl_tempdir
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    25
    if _rl_tempdir is None:
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    26
        _rl_tempdir = os.path.join(tempfile.gettempdir(),'ReportLab_tmp%s' % str(_rl_getuid()))
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    27
    d = _rl_tempdir
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    28
    if subdirs: d = os.path.join(*((d,)+subdirs))
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    29
    try:
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    30
        os.makedirs(d)
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    31
    except:
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    32
        pass
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    33
    return d
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    34
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    35
def get_rl_tempfile(fn=None):
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    36
    if not fn:
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    37
        fn = tempfile.mktemp()
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
diff changeset
    38
    return os.path.join(get_rl_tempdir(),fn)