## Copyright 2014 ngld <ngld@tproxy.de>
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

import os as _os
import warnings as _warnings
import tempfile
import subprocess
import six

if six.PY3:
    raise RuntimeError('py2_compat was imported even though we are running python 3!!!')


# This class is copied straight from /usr/lib/python3.3/tempfile.py
class TemporaryDirectory(object):
    """Create and return a temporary directory.  This has the same
    behavior as mkdtemp but can be used as a context manager.  For
    example:

        with TemporaryDirectory() as tmpdir:
            ...

    Upon exiting the context, the directory and everything contained
    in it are removed.
    """

    def __init__(self, suffix="", prefix="tmp", dir=None):
        self._closed = False
        self.name = None  # Handle mkdtemp raising an exception
        self.name = tempfile.mkdtemp(suffix, prefix, dir)

    def __repr__(self):
        return "<{} {!r}>".format(self.__class__.__name__, self.name)

    def __enter__(self):
        return self.name

    def cleanup(self, _warn=False):
        if self.name and not self._closed:
            try:
                self._rmtree(self.name)
            except (TypeError, AttributeError) as ex:
                # Issue #10188: Emit a warning on stderr
                # if the directory could not be cleaned
                # up due to missing globals
                if "None" not in str(ex):
                    raise
                self._warn("ERROR: {!r} while cleaning up {!r}".format(ex, self,), ResourceWarning)
                return
            self._closed = True
            if _warn:
                self._warn("Implicitly cleaning up {!r}".format(self),
                           ResourceWarning)

    def __exit__(self, exc, value, tb):
        self.cleanup()

    def __del__(self):
        # Issue a ResourceWarning if implicit cleanup needed
        self.cleanup(_warn=True)

    # XXX (ncoghlan): The following code attempts to make
    # this class tolerant of the module nulling out process
    # that happens during CPython interpreter shutdown
    # Alas, it doesn't actually manage it. See issue #10188
    _listdir = staticmethod(_os.listdir)
    _path_join = staticmethod(_os.path.join)
    _isdir = staticmethod(_os.path.isdir)
    _islink = staticmethod(_os.path.islink)
    _remove = staticmethod(_os.remove)
    _rmdir = staticmethod(_os.rmdir)
    _os_error = OSError
    _warn = _warnings.warn

    def _rmtree(self, path):
        # Essentially a stripped down version of shutil.rmtree.  We can't
        # use globals because they may be None'ed out at shutdown.
        for name in self._listdir(path):
            fullname = self._path_join(path, name)
            try:
                isdir = self._isdir(fullname) and not self._islink(fullname)
            except self._os_error:
                isdir = False
            if isdir:
                self._rmtree(fullname)
            else:
                try:
                    self._remove(fullname)
                except self._os_error:
                    pass
        try:
            self._rmdir(path)
        except self._os_error:
            pass


tempfile.TemporaryDirectory = TemporaryDirectory
subprocess.DEVNULL = open(_os.devnull, 'wb')

@@ -672,16 +672,39 @@
 process%0A
+import re%0Aimport shlex%0A
 import s
@@ -3724,16 +3724,474 @@
  pass%0A%0A%0A
+# This code is copied straight from /usr/lib/python3.3/shlex.py%0Ashlex_find_unsafe = re.compile(r'%5B%5E%5Cw@%25+=:,./-%5D').search%0A%0A%0Adef shlex_quote(s):%0A    %22%22%22Return a shell-escaped version of the string *s*.%22%22%22%0A    if not s:%0A        return %22''%22%0A    if shlex_find_unsafe(s) is None:%0A        return s%0A%0A    # use single quotes, and put single quotes into double quotes%0A    # the string $'b is then quoted as '$'%22'%22'b'%0A    return %22'%22 + s.replace(%22'%22, %22'%5C%22'%5C%22'%22) + %22'%22%0A%0A%0A
 tempfile
@@ -4231,16 +4231,42 @@
 rectory%0A
+shlex.quote = shlex_quote%0A
 subproce

Add shlex.quote() for python 2.