コード例 #1
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_logical_or(self):
     with Capture() as out:
         with Pipeline('false || echo foo', stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().strip(), b'foo')
     with Capture() as out:
         with Pipeline('true || echo foo', stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().strip(), b'')
コード例 #2
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_pipeline_with_input_stdout(self):
     logger.debug('starting')
     with Capture() as out:
         with Pipeline('cat 2>> %s | cat | cat' % os.devnull,
                       stdout=out) as pl:
             pl.run(input='foo' * 1000)
         self.assertEqual(out.read().strip(), b'foo' * 1000)
コード例 #3
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_pipeline_no_input_redirect_stderr(self):
     if os.name != 'posix':
         raise unittest.SkipTest('This test works only on POSIX')
     with Capture() as err:
         with Pipeline('echo foo 2> %s | cat 2>&1 | cat >&2' % os.devnull,
                       stderr=err) as pl:
             pl.run()
         self.assertEqual(err.read().strip(), b'foo')
コード例 #4
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_pipeline_swap_outputs(self):
     for fn in ('stdout.log', 'stderr.log'):
         if os.path.exists(fn):
             os.unlink(fn)
     with Pipeline('echo foo | tee stdout.log 3>&1 1>&2 2>&3 | '
                   'tee stderr.log > %s' % os.devnull) as pl:
         pl.run()
         with open('stdout.log') as f:
             self.assertEqual(f.read().strip(), 'foo')
         with open('stderr.log') as f:
             self.assertEqual(f.read().strip(), 'foo')
     for fn in ('stdout.log', 'stderr.log'):
         os.unlink(fn)
コード例 #5
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_pipeline_large_file(self):
     if os.path.exists('dest.bin'):  #pragma: no cover
         os.unlink('dest.bin')
     if not os.path.exists('random.bin'):  #pragma: no cover
         with open('random.bin', 'wb') as f:
             f.write(os.urandom(20 * 1048576))
     with Pipeline('cat random.bin | cat | cat | cat | cat | '
                   'cat > dest.bin ') as pl:
         pl.run()
     with open('random.bin', 'rb') as f:
         data1 = f.read()
     with open('dest.bin', 'rb') as f:
         data2 = f.read()
     os.unlink('dest.bin')
     self.assertEqual(data1, data2)
コード例 #6
0
class CliCommand:
    """
    This class represents SDK tool program. You can run program with this like the below:

        >>> import beesting
        >>>
        >>> lscmd = beesting.CliCommand("/usr/bin/ls")
        >>> lscmd.run("-l")

    This class use Pipeline class of sarge project.
    Please refer to `sarge's documentation <http://sarge.readthedocs.org/en/latest/index.html>`_.

    :param cmd: SDK tool program full path
    :type cmd: str
    :param posix: Whether the source will be parsed using Posix conventions.
    :type posix: bool
    :param silence: Run command in slience. Default is True.
    :type silence: bool
    :param capture: Capture child process' output. Default is True.
    :type capture: bool
    :param kwargs: Whatever you might pass to `subprocess.Popen <https://docs.python.org/3/library/subprocess.html?highlight=subprocess#subprocess.Popen>`_.
    """
    def __init__(self, cmd, posix=None, silence=True, capture=True, **kwargs):
        self.cmd = cmd
        self.posix = posix
        self.process = None
        self.kwargs = kwargs

        #: standard out data of program
        self.stdout = None
        self.stderr = None

        #: standard error data of program
        if "encoding" in kwargs:
            encoding = kwargs.pop("encoding")
            self.stdout_encoding = encoding
            self.stderr_encoding = encoding
        elif sys.stdout.encoding is not None and sys.stderr.encoding is not None:
            self.stdout_encoding = sys.stdout.encoding
            self.stderr_encoding = sys.stderr.encoding
        else:
            self.stdout_encoding = "utf-8"
            self.stderr_encoding = "utf-8"

        self._silence = silence
        self._capture = capture

    def run(self, param=None, msg=None, input=None, async=False):
        """
        Run command line program with parameters.

        :rtype: Pipeline
        :param param: command line program's arguments. eg) ``-l``, ``list native-project``
        """

        self.terminate()

        _logger.info("Run command on slience mode %s" % str(self._silence))

        if self._capture is True:
            self.stdout = Capture(encoding=self.stdout_encoding)
            self.stderr = Capture(encoding=self.stderr_encoding)

        command = self.cmd

        if param:
            command = self.cmd + " " + param

        shell = None

        if platform.system() == "Windows":
            command += " & exit"
            shell = True

        _logger.debug("Creating Pipeline object.")
        self.process = Pipeline(source=command,
                                shell=shell,
                                posix=self.posix,
                                stdout=self.stdout,
                                stderr=self.stderr,
                                **self.kwargs)

        self.process.run(input=input, async=async)
コード例 #7
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_capture_when_other_piped(self):
     with Capture() as out:
         with Pipeline('echo foo; echo bar |& cat', stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().split(), [b'foo', b'bar'])
コード例 #8
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_list_merge(self):
     with Capture() as out:
         with Pipeline('echo foo; echo bar; echo baz', stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().split(), [b'foo', b'bar', b'baz'])
コード例 #9
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_list(self):
     with Capture() as out:
         with Pipeline('echo foo > %s; echo bar' % os.devnull,
                       stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().strip(), b'bar')
コード例 #10
0
ファイル: test_sarge.py プロジェクト: davidprush/sarge
 def test_pipeline_no_input_stdout(self):
     with Capture() as out:
         with Pipeline('echo foo 2> %s | cat | cat' % os.devnull,
                       stdout=out) as pl:
             pl.run()
         self.assertEqual(out.read().strip(), b'foo')