util.py function tmpfile does not work as intended on windows #13

Closed
syserr0r opened this Issue Dec 16, 2013 · 1 comment

Comments

2 participants
@syserr0r

The function tmpfile creates a temp file with a code snippet to be linted and runs the linter command on the file, finally returning the result, however on windows the temp file cannot be read while it is still open.

Apologies in advance for my messy code, but here is a working replacement tmpfile function:

def tmpfile(cmd, code, suffix='', output_stream=STREAM_STDOUT):
    """
    Return the result of running an executable against a temporary file containing code.

    It is assumed that the executable launched by cmd can take one more argument
    which is a filename to process.

    The result is a string combination of stdout and stderr.

    """

    with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as f:
        if isinstance(code, str):
            code = code.encode('utf8')

        f.write(code)
        f.flush()
        f.close()
        cmd = cmd + (f.name,)
        out = popen(cmd)
        if out:
            out = out.communicate()
            r = combine_output(out, output_stream)
        else:
            r = ''
    os.remove(f.name)
    return r

PS: This was noticed primarily when trying to get the phplint linter working

@aparajita

This comment has been minimized.

Show comment Hide comment
@aparajita

aparajita Dec 17, 2013

Contributor

Good catch. A fix has been pushed.

Contributor

aparajita commented Dec 17, 2013

Good catch. A fix has been pushed.

@aparajita aparajita closed this Dec 17, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment