Пример #1
0
 def testCwd(self):
     cwd = "/proc"
     p = CPopen(["python", "-c", "import os; print os.getcwd()"], cwd=cwd)
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(p.stdout.read().strip(), cwd)
Пример #2
0
 def testEcho(self):
     data = "Hello"
     p = CPopen([EXT_ECHO, "-n", data])
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(p.stdout.read(), data)
Пример #3
0
 def _subTest(self, name, params, *args, **kwargs):
     p = CPopen(["python", EXT_HELPER, name] + params,
                *args, **kwargs)
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(p.stdout.read().strip(), "True")
Пример #4
0
 def testUserFileWithRedirect(self):
     data = "it works!"
     with tempfile.TemporaryFile() as f:
         p = CPopen(["echo", "-n", data], stdout=f, stderr=f)
         p.wait()
         f.seek(0)
         self.assertEqual(data, f.read())
Пример #5
0
 def testNoStreams(self):
     p = CPopen(['true'], stdin=None, stdout=None, stderr=None)
     self.assertEqual(p.stdin, None)
     self.assertEqual(p.stdout, None)
     self.assertEqual(p.stderr, None)
     p.wait()
     self.assertEquals(p.returncode, 0)
Пример #6
0
 def testCwd(self):
     cwd = "/proc"
     p = CPopen(["python", "-c", "import os; print os.getcwd()"], cwd=cwd)
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(p.stdout.read().strip(), cwd)
Пример #7
0
 def testNoStreams(self):
     p = CPopen(['true'], stdin=None, stdout=None, stderr=None)
     self.assertIsNone(p.stdin)
     self.assertIsNone(p.stdout)
     self.assertIsNone(p.stderr)
     p.wait()
     self.assertEquals(p.returncode, 0)
Пример #8
0
 def testEcho(self):
     data = "Hello"
     p = CPopen([EXT_ECHO, "-n", data])
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(p.stdout.read(), data)
Пример #9
0
 def testCat(self):
     path = "/etc/passwd"
     p = CPopen(["cat", path])
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     with open(path, "r") as f:
         self.assertEquals(p.stdout.read(), f.read())
Пример #10
0
 def testBrokenPipe(self):
     p = CPopen(["sleep", "1"])
     try:
         p.send_signal(signal.SIGPIPE)
     finally:
         p.kill()
     p.wait()
     self.assertEqual(p.returncode, -signal.SIGKILL)
Пример #11
0
 def testCloseOnExecStderr(self):
     # Unset close-on-exec on stderr fd
     with tempfile.NamedTemporaryFile() as f:
         set_close_on_exec(f.fileno())
         p = CPopen(["sh", "-c", "echo -n data >&2"], stderr=f.fileno())
         p.wait()
         f.seek(0)
         self.assertEqual(f.read(), "data")
Пример #12
0
 def testCat(self):
     path = "/etc/passwd"
     p = CPopen(["cat", path])
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     with open(path, "r") as f:
         self.assertEquals(p.stdout.read(), f.read())
Пример #13
0
 def testCloseFDs(self):
     with open("/dev/zero") as f:
         p = CPopen(["sleep", "1"], close_fds=True)
         try:
             child_fds = set(os.listdir("/proc/%s/fd" % p.pid))
         finally:
             p.kill()
             p.wait()
         self.assertEqual(child_fds, set(["0", "1", "2"]))
Пример #14
0
 def testBrokenPipeSIGPIPERestored(self):
     if not cpopen.SUPPORTS_RESTORE_SIGPIPE:
         raise SkipTest("subprocess module does not support restore_sigpipe")
     p = CPopen(["sleep", "1"], restore_sigpipe=True)
     try:
         p.send_signal(signal.SIGPIPE)
     finally:
         p.kill()
     p.wait()
     self.assertEqual(p.returncode, -signal.SIGPIPE)
Пример #15
0
 def testPipeline(self):
     # echo -n 'it works!' | cat
     data = "it works!"
     p2 = CPopen(["cat"], stdin=PIPE, stdout=PIPE)
     try:
         p1 = CPopen(["echo", "-n", data], stdin=PIPE, stdout=p2.stdin)
         p1.wait()
     finally:
         out, err = p2.communicate()
     self.assertEqual(data, out)
Пример #16
0
    def testUnicodeArg(self):
        data = u'hello'
        cmd = [EXT_ECHO, "-n", data]

        p = CPopen(cmd)
        p.wait()
        p2 = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p2.wait()
        self.assertEquals(p.stdout.read(), p2.stdout.read())
Пример #17
0
 def testNoCloseFds(self):
     with open("/dev/zero") as f:
         p = CPopen(["sleep", "1"], close_fds=False)
         try:
             child_fds = set(os.listdir("/proc/%s/fd" % p.pid))
         finally:
             p.kill()
             p.wait()
         # We cannot know which fds will be inherited in the child since the
         # test framework may open some fds.
         self.assertTrue(str(f.fileno()) in child_fds)
Пример #18
0
    def testStdin(self):
        data = "Hello World"
        p = CPopen(["cat"])
        p.stdin.write(data)
        p.stdin.flush()
        p.stdin.close()
        p.wait()
        self.assertTrue(p.returncode == 0,
                        "Process failed: %s" % os.strerror(p.returncode))

        self.assertEquals(p.stdout.read(), data)
Пример #19
0
    def testStdin(self):
        data = "Hello World"
        p = CPopen(["cat"])
        p.stdin.write(data)
        p.stdin.flush()
        p.stdin.close()
        p.wait()
        self.assertTrue(p.returncode == 0,
                        "Process failed: %s" % os.strerror(p.returncode))

        self.assertEquals(p.stdout.read(), data)
Пример #20
0
 def testUmaskTmpfile(self):
     name = os.tempnam()
     p = CPopen(['touch', name], childUmask=0o007)
     p.wait()
     data = os.stat(name)
     os.unlink(name)
     self.assertTrue(data.st_mode & stat.S_IROTH == 0,
                     "%s is world-readable" % name)
     self.assertTrue(data.st_mode & stat.S_IWOTH == 0,
                     "%s is world-writeable" % name)
     self.assertTrue(data.st_mode & stat.S_IXOTH == 0,
                     "%s is world-executable" % name)
Пример #21
0
    def testUnicodeArg(self):
        data = u'hello'
        cmd = [EXT_ECHO, "-n", data]

        p = CPopen(cmd)
        p.wait()
        p2 = subprocess.Popen(cmd,
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
        p2.wait()
        self.assertEquals(p.stdout.read(), p2.stdout.read())
Пример #22
0
 def testInheritParentFdsCloseFds(self):
     # From Python docs: If close_fds is true, all file descriptors except
     # 0, 1 and 2 will be closed before the child process is executed.
     with open("/dev/zero") as f:
         p = CPopen(["sleep", "1"], stdin=None, stdout=None, stderr=None,
                    close_fds=True)
         try:
             child_fds = set(os.listdir("/proc/%s/fd" % p.pid))
         finally:
             p.kill()
             p.wait()
     self.assertEqual(child_fds, set(["0", "1", "2"]))
Пример #23
0
 def testUmaskTmpfile(self):
     tmp_dir = tempfile.mkdtemp()
     name = os.path.join(tmp_dir, "file.txt")
     p = CPopen(['touch', name], childUmask=0o007)
     p.wait()
     data = os.stat(name)
     os.unlink(name)
     self.assertTrue(data.st_mode & stat.S_IROTH == 0,
                     "%s is world-readable" % name)
     self.assertTrue(data.st_mode & stat.S_IWOTH == 0,
                     "%s is world-writeable" % name)
     self.assertTrue(data.st_mode & stat.S_IXOTH == 0,
                     "%s is world-executable" % name)
     posix.rmdir(tmp_dir)
Пример #24
0
 def testInheritParentFds(self):
     # From Python docs: With the default settings of None, no redirection
     # will occur; the child's file handles will be inherited from the
     # parent.
     with open("/dev/zero") as f:
         p = CPopen(["sleep", "1"], stdin=None, stdout=None, stderr=None,
                    close_fds=False)
         try:
             child_fds = set(os.listdir("/proc/%s/fd" % p.pid))
         finally:
             p.kill()
             p.wait()
         expected_fds = set(["0", "1", "2", str(f.fileno())])
     self.assertEqual(child_fds, expected_fds)
Пример #25
0
 def testUmaskTmpfile(self):
     warnings.filterwarnings(
         'ignore',
         '^tempnam is a potential security risk.*')
     name = os.tempnam()
     p = CPopen(['touch', name], childUmask=0o007)
     p.wait()
     data = os.stat(name)
     os.unlink(name)
     self.assertTrue(data.st_mode & stat.S_IROTH == 0,
                     "%s is world-readable" % name)
     self.assertTrue(data.st_mode & stat.S_IWOTH == 0,
                     "%s is world-writeable" % name)
     self.assertTrue(data.st_mode & stat.S_IXOTH == 0,
                     "%s is world-executable" % name)
Пример #26
0
 def testUmaskTmpfile(self):
     tmp_dir = tempfile.mkdtemp()
     try:
         name = os.path.join(tmp_dir, "file.txt")
         p = CPopen(['touch', name], childUmask=0o007)
         p.wait()
         data = os.stat(name)
         self.assertTrue(data.st_mode & stat.S_IROTH == 0,
                         "%s is world-readable" % name)
         self.assertTrue(data.st_mode & stat.S_IWOTH == 0,
                         "%s is world-writeable" % name)
         self.assertTrue(data.st_mode & stat.S_IXOTH == 0,
                         "%s is world-executable" % name)
     finally:
         shutil.rmtree(tmp_dir)
Пример #27
0
    def testNonASCIIUnicodeArg(self):
        data = u'\u05e9\u05dc\u05d5\u05dd'
        # If the default encoding is not utf-8 the test *should* fail as non
        # ascii conversion shouldn't work
        if sys.getfilesystemencoding() != "UTF-8":
            raise SkipTest("The default encoding isn't unicode")

        cmd = [EXT_ECHO, "-n", data]

        p = CPopen(cmd)
        p.wait()
        p2 = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p2.wait()
        self.assertEquals(p.stdout.read(), p2.stdout.read())
Пример #28
0
    def testStdinEpoll(self):
        import select

        data = "Hello World"
        p = CPopen(["cat"])
        ep = select.epoll()
        ep.register(p.stdin, select.EPOLLOUT)
        fd, ev = ep.poll(1)[0]
        ep.close()
        os.write(fd, data)
        p.stdin.close()
        p.wait()
        self.assertTrue(p.returncode == 0,
                        "Process failed: %s" % os.strerror(p.returncode))

        self.assertEquals(p.stdout.read(), data)
Пример #29
0
    def testStdinEpoll(self):
        import select

        data = "Hello World"
        p = CPopen(["cat"])
        ep = select.epoll()
        ep.register(p.stdin, select.EPOLLOUT)
        fd, ev = ep.poll(1)[0]
        ep.close()
        os.write(fd, data)
        p.stdin.close()
        p.wait()
        self.assertTrue(p.returncode == 0,
                        "Process failed: %s" % os.strerror(p.returncode))

        self.assertEquals(p.stdout.read(), data)
Пример #30
0
    def testNonASCIIUnicodeArg(self):
        data = u'\u05e9\u05dc\u05d5\u05dd'
        # If the default encoding is not utf-8 the test *should* fail as non
        # ascii conversion shouldn't work
        if sys.getfilesystemencoding() != "UTF-8":
            raise SkipTest("The default encoding isn't unicode")

        cmd = [EXT_ECHO, "-n", data]

        p = CPopen(cmd)
        p.wait()
        p2 = subprocess.Popen(cmd,
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
        p2.wait()
        self.assertEquals(p.stdout.read(), p2.stdout.read())
Пример #31
0
class QemuImgOperation(object):
    REGEXPR = re.compile(r'\s*\(([\d.]+)/100%\)\s*')

    def __init__(self, cmd, cwd=None):
        self._aborted = False
        self._progress = 0.0

        self._stdout = bytearray()
        self._stderr = bytearray()

        cmd = cmdutils.wrap_command(cmd, with_nice=utils.NICENESS.HIGH,
                                    with_ioclass=utils.IOCLASS.IDLE)
        _log.debug(cmdutils.command_log_line(cmd, cwd=cwd))
        self._command = CPopen(cmd, cwd=cwd, deathSignal=signal.SIGKILL)
        self._stream = utils.CommandStream(
            self._command, self._recvstdout, self._recvstderr)

    def _recvstderr(self, buffer):
        self._stderr += buffer

    def _recvstdout(self, buffer):
        self._stdout += buffer

        # Checking the presence of '\r' before splitting will prevent
        # generating the array when it's not needed.
        try:
            idx = self._stdout.rindex('\r')
        except ValueError:
            return

        # qemu-img updates progress by printing \r (0.00/100%) to standard out.
        # The output could end with a partial progress so we must discard
        # everything after the last \r and then try to parse a progress record.
        valid_progress = self._stdout[:idx]
        last_progress = valid_progress.rsplit('\r', 1)[-1]

        # No need to keep old progress information around
        del self._stdout[:idx + 1]

        m = self.REGEXPR.match(last_progress)
        if m is None:
            raise ValueError('Unable to parse: "%r"' % last_progress)

        self._progress = float(m.group(1))

    @property
    def progress(self):
        return self._progress

    @property
    def error(self):
        return str(self._stderr)

    @property
    def finished(self):
        return self._command.poll() is not None

    def wait(self, timeout=None):
        self._stream.receive(timeout=timeout)

        if not self._stream.closed:
            return

        self._command.wait()

        if self._aborted:
            raise utils.ActionStopped()

        cmdutils.retcode_log_line(self._command.returncode, self.error)
        if self._command.returncode != 0:
            raise QImgError(self._command.returncode, "", self.error)

    def abort(self):
        if self._command.poll() is None:
            self._aborted = True
            self._command.terminate()
Пример #32
0
 def _subTest(self, name, params, *args, **kwargs):
     p = CPopen(["python", __file__, name] + params, *args, **kwargs)
     p.wait()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(p.stdout.read().strip(), "True")
Пример #33
0
class QemuImgOperation(object):
    REGEXPR = re.compile(r'\s*\(([\d.]+)/100%\)\s*')

    def __init__(self, cmd, cwd=None):
        self._aborted = False
        self._progress = 0.0

        self._stdout = bytearray()
        self._stderr = bytearray()

        cmd = cmdutils.wrap_command(cmd,
                                    with_nice=utils.NICENESS.HIGH,
                                    with_ioclass=utils.IOCLASS.IDLE)
        _log.debug(cmdutils.command_log_line(cmd, cwd=cwd))
        self._command = CPopen(cmd, cwd=cwd, deathSignal=signal.SIGKILL)
        self._stream = utils.CommandStream(self._command, self._recvstdout,
                                           self._recvstderr)

    def _recvstderr(self, buffer):
        self._stderr += buffer

    def _recvstdout(self, buffer):
        self._stdout += buffer

        # Checking the presence of '\r' before splitting will prevent
        # generating the array when it's not needed.
        try:
            idx = self._stdout.rindex('\r')
        except ValueError:
            return

        # qemu-img updates progress by printing \r (0.00/100%) to standard out.
        # The output could end with a partial progress so we must discard
        # everything after the last \r and then try to parse a progress record.
        valid_progress = self._stdout[:idx]
        last_progress = valid_progress.rsplit('\r', 1)[-1]

        # No need to keep old progress information around
        del self._stdout[:idx + 1]

        m = self.REGEXPR.match(last_progress)
        if m is None:
            raise ValueError('Unable to parse: "%r"' % last_progress)

        self._progress = float(m.group(1))

    @property
    def progress(self):
        return self._progress

    @property
    def error(self):
        return str(self._stderr)

    @property
    def finished(self):
        return self._command.poll() is not None

    def wait(self, timeout=None):
        self._stream.receive(timeout=timeout)

        if not self._stream.closed:
            return

        self._command.wait()

        if self._aborted:
            raise utils.ActionStopped()

        cmdutils.retcode_log_line(self._command.returncode, self.error)
        if self._command.returncode != 0:
            raise QImgError(self._command.returncode, "", self.error)

    def abort(self):
        if self._command.poll() is None:
            self._aborted = True
            self._command.terminate()
Пример #34
0
 def testUmaskChange(self):
     p = CPopen(['umask'], childUmask=0o007)
     p.wait()
     out = p.stdout.readlines()
     self.assertEquals(out[0].strip(), '0007')
Пример #35
0
 def testUmaskChange(self):
     p = CPopen(['umask'], childUmask=0o007)
     p.wait()
     out = p.stdout.readlines()
     self.assertEquals(out[0].strip(), '0007')