Esempio n. 1
0
def mkstemp_rename(destination, **kwargs):
    """For writing to a temporary file and then move it ontop of a
    (possibly) existing file only when finished.  This enables us to
    perform long running operations on a file that other people might
    be using and let everyone else see a consistent version.

    * other args are passed to tempfile.mkstemp

    Example::

        with mkstemp_rename('foobar.txt') as f:
            f.write('stuff\n')

    """
    kwargs.setdefault('dir', os.path.dirname(destination))

    (fd, path) = tempfile.mkstemp(**kwargs)
    path = pathlib(path)
    try:
        filelike = os.fdopen(fd, 'wb')
        yield filelike
        filelike.close()
        path.rename(destination)
    finally:
        path.remove_p()
Esempio n. 2
0
def mkdtemp_rename(destination, **kwargs):
    """A wrapper for tempfile.mkdtemp that always cleans up.

    This wrapper sets defaults based on the class values."""
    dest = pathlib(destination).normpath()
    kwargs.setdefault('dir', dest.parent)
    tmppath = pathlib(tempfile.mkdtemp(**kwargs))
    try:
        yield tmppath
        try:
            tmppath.rename(dest)
        except OSError as e:
            if e.errno == errno.EEXIST:
                # I don't think we'll ever get here.
                dest.rmtree_p()
                tmppath.rename(dest)
            else:
                raise
    finally:
        tmppath.rmtree_p()
Esempio n. 3
0
def run_cmdline(gbkfile, executorName):
    config = parsing.initial(gbkfile)
    with getexecutor(executorName) as executor:
        config = process('allplots', config, executor=executor)
        jid = config.get('pdf_filename') or config.get('combined_ps_name')
        if not pathlib(jid).exists():
            logging.info("Work scheduled, waiting")
            output = executor.result(jid, timeout=None)
            logging.info("Finished processing %r", gbkfile)
        else:
            output = jid
        logging.info("See output at %r", output)
Esempio n. 4
0
 def wrapper(config, target):
     if not pathlib(target).exists():
         with tmpmanager(target) as tmp:
             func(config, tmp)
             return target
Esempio n. 5
0
def replace_ext(base, newext):
    base = pathlib(base)
    if newext[0] == '.':
        newext = newext[1:]
    return base.stripext() + '.' + newext