@contextmanager
def make_tempfile(content=None, wrapped=None, **tkwargs):
    'Helper class to provide a temporary file name and remove it at the end (context manager)\n\n    Parameters\n    ----------\n    mkdir : bool, optional (default: False)\n        If True, temporary directory created using tempfile.mkdtemp()\n    content : str or bytes, optional\n        Content to be stored in the file created\n    wrapped : function, optional\n        If set, function name used to prefix temporary file name\n    `**tkwargs`:\n        All other arguments are passed into the call to tempfile.mk{,d}temp(),\n        and resultant temporary filename is passed as the first argument into\n        the function t.  If no \'prefix\' argument is provided, it will be\n        constructed using module and function names (\'.\' replaced with\n        \'_\').\n\n    To change the used directory without providing keyword argument \'dir\' set\n    DATALAD_TESTS_TEMP_DIR.\n\n    Examples\n    --------\n        >>> from os.path import exists\n        >>> from datalad.utils import make_tempfile\n        >>> with make_tempfile() as fname:\n        ...    k = open(fname, \'w\').write(\'silly test\')\n        >>> assert not exists(fname)  # was removed\n\n        >>> with make_tempfile(content="blah") as fname:\n        ...    assert open(fname).read() == "blah"\n    '
    if (tkwargs.get('mkdir', None) and (content is not None)):
        raise ValueError('mkdir=True while providing content makes no sense')
    tkwargs_ = get_tempfile_kwargs(tkwargs, wrapped=wrapped)
    mkdir = tkwargs_.pop('mkdir', False)
    filename = {False: tempfile.mktemp, True: tempfile.mkdtemp}[mkdir](**tkwargs_)
    filename = Path(filename).resolve()
    if content:
        (filename.write_bytes if isinstance(content, bytes) else filename.write_text)(content)
    filename = str(filename)
    if __debug__:
        lgr.debug('Created temporary %s named %s', ('directory' if mkdir else 'file'), filename)
    try:
        (yield filename)
    finally:
        lsuffix = len(tkwargs_.get('suffix', ''))
        filename_ = ((lsuffix and filename[:(- lsuffix)]) or filename)
        filenames = glob.glob((filename_ + '*'))
        if ((len(filename_) < 3) or (len(filenames) > 5)):
            lgr.warning(('It is unlikely that it was intended to remove all files matching %r. Skipping' % filename_))
            return
        for f in filenames:
            try:
                rmtemp(f)
            except OSError:
                pass
Helper class to provide a temporary file name and remove it at the end (context manager)

Parameters
----------
mkdir : bool, optional (default: False)
    If True, temporary directory created using tempfile.mkdtemp()
content : str or bytes, optional
    Content to be stored in the file created
wrapped : function, optional
    If set, function name used to prefix temporary file name
`**tkwargs`:
    All other arguments are passed into the call to tempfile.mk{,d}temp(),
    and resultant temporary filename is passed as the first argument into
    the function t.  If no 'prefix' argument is provided, it will be
    constructed using module and function names ('.' replaced with
    '_').

To change the used directory without providing keyword argument 'dir' set
DATALAD_TESTS_TEMP_DIR.

Examples
--------
    >>> from os.path import exists
    >>> from datalad.utils import make_tempfile
    >>> with make_tempfile() as fname:
    ...    k = open(fname, 'w').write('silly test')
    >>> assert not exists(fname)  # was removed

    >>> with make_tempfile(content="blah") as fname:
    ...    assert open(fname).read() == "blah"
@contextmanager
def make_tempfile(content=None, wrapped=None, **tkwargs):
    'Helper class to provide a temporary file name and remove it at the end (context manager)\n\n    Parameters\n    ----------\n    mkdir : bool, optional (default: False)\n        If True, temporary directory created using tempfile.mkdtemp()\n    content : str or bytes, optional\n        Content to be stored in the file created\n    wrapped : function, optional\n        If set, function name used to prefix temporary file name\n    `**tkwargs`:\n        All other arguments are passed into the call to tempfile.mk{,d}temp(),\n        and resultant temporary filename is passed as the first argument into\n        the function t.  If no \'prefix\' argument is provided, it will be\n        constructed using module and function names (\'.\' replaced with\n        \'_\').\n\n    To change the used directory without providing keyword argument \'dir\' set\n    DATALAD_TESTS_TEMP_DIR.\n\n    Examples\n    --------\n        >>> from os.path import exists\n        >>> from datalad.utils import make_tempfile\n        >>> with make_tempfile() as fname:\n        ...    k = open(fname, \'w\').write(\'silly test\')\n        >>> assert not exists(fname)  # was removed\n\n        >>> with make_tempfile(content="blah") as fname:\n        ...    assert open(fname).read() == "blah"\n    '
    if (tkwargs.get('mkdir', None) and (content is not None)):
        raise ValueError('mkdir=True while providing content makes no sense')
    tkwargs_ = get_tempfile_kwargs(tkwargs, wrapped=wrapped)
    mkdir = tkwargs_.pop('mkdir', False)
    filename = {False: tempfile.mktemp, True: tempfile.mkdtemp}[mkdir](**tkwargs_)
    filename = Path(filename).resolve()
    if content:
        (filename.write_bytes if isinstance(content, bytes) else filename.write_text)(content)
    filename = str(filename)
    if __debug__:
        lgr.debug('Created temporary %s named %s', ('directory' if mkdir else 'file'), filename)
    try:
        (yield filename)
    finally:
        lsuffix = len(tkwargs_.get('suffix', ))
        filename_ = ((lsuffix and filename[:(- lsuffix)]) or filename)
        filenames = glob.glob((filename_ + '*'))
        if ((len(filename_) < 3) or (len(filenames) > 5)):
            lgr.warning(('It is unlikely that it was intended to remove all files matching %r. Skipping' % filename_))
            return
        for f in filenames:
            try:
                rmtemp(f)
            except OSError:
                pass