Пример #1
0
def test_dry(proc):
    r = RunnerDry(0, proc)
    r.dir.mkdir()
    (r.dir / 'job.script').write_text('')
    r.output.b = ('dir', 'b.dir')  # make output directory first
    r.output.a = ('file', 'a.txt')
    r.wrapScript()

    assert r.script.read_text() == '''#!/usr/bin/env bash
#
# Collect return code on exit
trap "status=\\$?; echo \\$status > '{jobdir}/job.rc'; if [ ! -e '{jobdir}/job.stdout' ]; then touch '{jobdir}/job.stdout'; fi; if [ ! -e '{jobdir}/job.stderr' ]; then touch '{jobdir}/job.stderr'; fi; exit \\$status" 1 2 3 6 7 8 9 10 11 12 15 16 17 EXIT
#
# Run pre-script

# Dry-run script to create empty output files and directories.

mkdir -p {jobdir}/output/b.dir
touch {jobdir}/output/a.txt

#
# Run the real script
#
# Run post-script
#'''.format(jobdir=r.dir)

    utils.fs.remove(r.dir / 'output' / 'b.dir')
    utils.fs.remove(r.dir / 'output' / 'a.txt')

    cmdy.bash(r.script, _fg=True)

    # check if output file and directory generated
    assert (r.dir / 'output' / 'b.dir').exists()
    assert (r.dir / 'output' / 'a.txt').exists()
Пример #2
0
def test_mixed_actions_pipe_then():
    # actions could be
    # hold
    # async_
    # redirect
    # fg
    # iter
    # pipe
    c = cmdy.echo().h().pipe().r()
    assert isinstance(c, CmdyHolding)
    _CMDY_EVENT.clear()

    with pytest.raises(CmdyActionError):
        cmdy.echo().p().h()

    c = cmdy.echo().p().a()
    assert isinstance(c, CmdyHolding)
    _CMDY_EVENT.clear()

    c = cmdy.bash(
        c='echo "1\n2\n3" 1>&2').p().r(STDERR) ^ STDOUT | cmdy.grep(2)
    assert c.str() == '2\n'

    with pytest.raises(CmdyActionError) as ex:
        cmdy.echo().p().p()
    assert 'Unconsumed piping' in str(ex.value)
Пример #3
0
    def on_run_request(self, data):
        """Running a command/script"""
        logger.debug('Got request run_request from client '
                     f'{request.remote_addr}')
        # eleid: logger.id,
        # proc: that.proc,
        # job: that.job,
        # target: type,
        # cmd: $(this).val()});
        data = Diot(data)
        running = pipeline_data.setdefault('running', Diot())
        if data.eleid in running:
            # is already running
            return

        cmd = data.cmd
        if 'target' in data:
            workdir = pipeline_data.procs[data['proc']].props.workdir
            target = Path(workdir) / str(int(data.job)) / str(data.target)
            target = repr(str(target))
            cmd = cmd + ' ' + target if cmd else target

        running[data.eleid] = {
            'cmdy': cmdy.bash(c=cmd, _raise=False).iter,
            'buffer': ''
        }
Пример #4
0
def test_redirect_failing_fetching(no_iter):
    c = cmdy.echo(n=123).r(STDOUT) > DEVNULL
    assert c.holding.stdout != curio.subprocess.PIPE
    assert c.stdout is None

    c = cmdy.bash(c='echo 123 1>&2').r(STDERR) > DEVNULL
    assert c.stderr is None
Пример #5
0
def tag(options):
    """Tag the version"""
    default_options = Config()
    default_options._load('./.tagitrc')
    default_options._use('TAGIT')
    default_options.update(vars(options['c']))
    publish = default_options.get('publish', False)
    #changelog = default_options.get('changelog', '')
    increment = default_options.get('increment', 'patch')
    versource = default_options.get('versource', '')
    vertoml = default_options.get('vertoml', '')
    #checksource = default_options.get('checksource', True)
    #checktoml = default_options.get('checktoml', True)
    extra = default_options.get('extra', '')

    specver = options[POSITIONAL] or None
    ret = status(default_options, specver, True)

    if not ret:
        return

    tagver = _get_version_from_gittag() or Tag((0, 0, 0))
    specver = specver or tagver.increment(increment)

    if versource:
        _log('Updating version in source file ...')
        _update_version_to_source(_getsrcfile(versource), specver)

    if vertoml:
        _log('Updating version in pyproject.toml ...')
        _update_version_to_toml(specver, vertoml)

    if extra:
        cmd = bash(c=extra).fg
        if cmd.rc != 0:
            raise RuntimeError('Failed to run %r' % extra)
    _log('Committing the change ...')
    try:
        git.commit({'allow-empty': True}, a=True, m=str(specver)).fg
    except CmdyReturnCodeError:
        # pre-commit fails, do it again
        _log('Pre-commit failed, try again ...')
        git.add('.')
        git.commit({'allow-empty': True}, a=True, m=str(specver)).fg

    _log('Pushing the commit to remote ...')
    git.push().fg

    _log('Adding tag %r ...' % specver)
    git.tag(str(specver)).fg
    _log('Pushing the tag to remote ...')
    git.push(tag=True).fg

    if publish:
        _log('Building the release ...')
        poetry.build().fg
        _log('Publishing the release ...')
        poetry.publish().fg
    _log('Done!')
Пример #6
0
def test_line_fetch_timeout():

    import time
    c = cmdy.bash(c='echo 1; sleep .21; echo 2').iter()
    time.sleep(.1)
    assert c.next(timeout=.1) == '1\n'
    assert c.next(timeout=.1) == ''
    assert c.next(timeout=.1) == '2\n'
Пример #7
0
    def submitImpl(self):
        """
		Try to submit the job
		"""
        cmd = cmdy.bash(self.script, _raise=False, _bg=True)
        cmd.rc = 0
        self.pid = cmd.pid
        return cmd
Пример #8
0
    def succeed(self):
        """@API
		Tell if a job succeeds.
		Check whether output files generated, expectation met and return code met.
		@return:
			(bool): `True` if succeeds else `False`
		"""
        procclass = self.proc.__class__
        # first check if bare rc is allowed
        if self.rc not in self.proc.rc:
            pluginmgr.hook.jobFail(job=self)
            return False

        # refresh output directory
        # check if output files have been generated
        utime(self.dir / DIR_OUTPUT, None)
        for outtype, outdata in self.output.values():
            if outtype not in procclass.OUT_VARTYPE and not fs.exists(outdata):
                self.rc += (1 << RCBIT_NO_OUTFILE)
                self.logger('Outfile not generated: {}'.format(outdata),
                            dlevel="OUTFILE_NOT_EXISTS",
                            level='debug')
                pluginmgr.hook.jobFail(job=self)
                return False

        expect_cmd = self.proc.expect.render(self.data)
        if expect_cmd:
            self.logger('Check expectation: %s' % expect_cmd,
                        dlevel="EXPECT_CHECKING",
                        level='debug')
            cmd = cmdy.bash(c=expect_cmd)  # pylint: disable=no-member
            if cmd.rc != 0:
                self.rc += (1 << RCBIT_UNMET_EXPECT)
                pluginmgr.hook.jobFail(job=self)
                return False
        return True
Пример #9
0
def test_env():
    a = cmdy.bash(c='echo $A', _env=dict(A=1))
    assert a.int() == 1
Пример #10
0
def test_mixed_actions_pipe_then_iter(capsys):
    c = cmdy.bash(c='echo "1\n2\n3" 1>&2').p(STDERR).iter(STDOUT)
    d = c | cmdy.grep(2, cmdy_okcode='0,1')
    assert isinstance(d, CmdyResult)
    assert isinstance(c, CmdyHolding)
    assert d == '2\n'
Пример #11
0
def test_mixed_actions_pipe_then_fg(capsys):

    c = cmdy.bash(c='echo 123 1>&2 && echo 456').p(STDERR).fg() | cmdy.grep(
        4, cmdy_okcode='0,1')
    assert capsys.readouterr().out == '456\n'
    assert c.str() == ''  # have been redirected by fg
Пример #12
0
def test_async_rc_error():
    c = cmdy.bash('exit', '1').a()
    assert isinstance(c, CmdyAsyncResult)
    with pytest.raises(CmdyReturnCodeError):
        curio.run(c.wait())
Пример #13
0
def test_str_from_stderr(no_iter):
    c = cmdy.bash(c='echo -n 123 1>&2')
    assert c.int(STDERR) == 123
    assert c.stderr == '123'
    assert c.stderr == '123'  # trigger cache
Пример #14
0
def test_returncode_error():

    with pytest.raises(CmdyReturnCodeError):
        cmdy.bash('exit', 1)
Пример #15
0
def test_fg_wait(capsys):
    cmdy.bash(c='sleep .2 && echo 123').fg()
    assert capsys.readouterr().out == '123\n'
Пример #16
0
def test_fg_hold_right(capsys):

    c = cmdy.bash(c='echo 1234 && sleep .1').p() | cmdy.cat().fg()
    assert isinstance(c, CmdyResult)
    assert capsys.readouterr().out == '1234\n'