Ejemplo n.º 1
0
    def test_functions(self):
        cmd = 'function a() { bar; }'
        shellresults = [
            (0, 14, helpconstants._function, 'function a() {'),
            (18, 19, helpconstants.OPSEMICOLON, ';'),
            (20, 21, helpconstants._function, '}'),
        ]

        matchresults = [(15, 18, 'bar synopsis', 'bar')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)

        cmd = 'function a() { bar "$(a)"; }'
        shellresults = [
            (0, 14, helpconstants._function, 'function a() {'),
            (25, 26, helpconstants.OPSEMICOLON, ';'),
            (27, 28, helpconstants._function, '}'),
        ]

        matchresults = [(15, 18, 'bar synopsis', 'bar'),
                        (19, 25, None, '"$(a)"')]

        m = matcher.matcher(cmd, s)
        groups = m.match()

        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)
        self.assertEquals(m.expansions, [(22, 23, 'substitution')])
Ejemplo n.º 2
0
    def test_functions(self):
        cmd = 'function a() { bar; }'
        shellresults = [(0, 14, helpconstants._function, 'function a() {'),
                        (18, 19, helpconstants.OPSEMICOLON, ';'),
                        (20, 21, helpconstants._function, '}'),]

        matchresults = [(15, 18, 'bar synopsis', 'bar')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)

        cmd = 'function a() { bar "$(a)"; }'
        shellresults = [(0, 14, helpconstants._function, 'function a() {'),
                        (25, 26, helpconstants.OPSEMICOLON, ';'),
                        (27, 28, helpconstants._function, '}'),]

        matchresults = [(15, 18, 'bar synopsis', 'bar'),
                        (19, 25, None, '"$(a)"')]

        m = matcher.matcher(cmd, s)
        groups = m.match()

        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)
        self.assertEquals(m.expansions, [(22, 23, 'substitution')])
Ejemplo n.º 3
0
    def test_known_and_unknown_arg(self):
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 6, '-a desc', '-a'), (7, 9, None, '-x')])
        self.assertEquals(
            matcher.matcher('bar -a -x', s).match(), [matchedresult])

        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 6, '-a desc', '-a'), (6, 7, None, 'x')])
        self.assertEquals(
            matcher.matcher('bar -ax', s).match(), [matchedresult])
Ejemplo n.º 4
0
    def test_arg_with_expected_value_from_list(self):
        cmd = 'bar -c one'
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 10, '-c=one,two\ndesc', '-c one')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])

        cmd = 'bar -c notinlist'
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 6, '-c=one,two\ndesc', '-c'),
                                 (7, 16, None, 'notinlist')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 5
0
    def test_multicommand(self):
        cmd = 'bar baz --b foo'
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 7, None, 'baz'),
                                 (8, 15, '-b <arg> desc', '--b foo')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])

        cmd = 'bar foo --b foo'
        matchedresult = ('bar-foo', [(0, 7, 'bar foo synopsis', 'bar foo'),
                                     (8, 15, '-b <arg> desc', '--b foo')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 6
0
    def test_multicommand(self):
        cmd = 'bar baz --b foo'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 7, None, 'baz'),
            (8, 15, '-b <arg> desc', '--b foo')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])

        cmd = 'bar foo --b foo'
        matchedresult = ('bar-foo', [
            (0, 7, 'bar foo synopsis', 'bar foo'),
            (8, 15, '-b <arg> desc', '--b foo')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 7
0
    def test_arg_with_expected_value_from_list(self):
        cmd = 'bar -c one'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 10, '-c=one,two\ndesc', '-c one')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])

        cmd = 'bar -c notinlist'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 6, '-c=one,two\ndesc', '-c'),
            (7, 16, None, 'notinlist')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 8
0
    def test_arg_in_fuzzy_with_expected_value(self):
        cmd = 'baz -ab arg'
        matchedresult = ('baz', [(0, 3, 'baz synopsis', 'baz'),
                                 (4, 6, '-a desc', '-a'),
                                 (6, 11, '-b <arg> desc', 'b arg')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 9
0
    def test_known_arg(self):
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 10, '-a desc', '-a --a'),
                                 (11, 13, '-? help text', '-?')])

        self.assertEquals(
            matcher.matcher('bar -a --a -?', s).match(), [matchedresult])
Ejemplo n.º 10
0
    def test(self):
        mngr = manager.manager('localhost', 'explainshell_tests', [os.path.join(os.path.dirname(__file__), 'echo.1.gz')], drop=True)
        mngr.run()

        cmd = 'echo -en foobar --version'

        m = matcher.matcher(cmd, mngr.store)
        matchprog, matches = m.match()[0]

        self.assertEquals(matchprog, 'echo')

        #self.assertEquals(matches[0].text, 'display a line of text')
        self.assertEquals(matches[0].match, 'echo')

        self.assertEquals(matches[1].text, '<b>-e</b>     enable interpretation of backslash escapes')
        self.assertEquals(matches[1].match, '-e')

        self.assertEquals(matches[2].text, '<b>-n</b>     do not output the trailing newline')
        self.assertEquals(matches[2].match, 'n')

        self.assertEquals(matches[3].text, None)
        self.assertEquals(matches[3].match, 'foobar')

        self.assertEquals(matches[4].text, '<b>--version</b>\n       output version information and exit')
        self.assertEquals(matches[4].match, '--version')
Ejemplo n.º 11
0
    def test_long(self):
        cmd = 'bar --b=b'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 9, '-b <arg> desc', '--b=b')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 12
0
    def test_unknown_arg(self):
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'), (4, 6, None, '-x')])
        self.assertEquals(matcher.matcher('bar -x', s).match(), [matchedresult])

        # merges
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'), (4, 10, None, '-x --x')])
        self.assertEquals(matcher.matcher('bar -x --x', s).match(), [matchedresult])

        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'), (4, 8, None, '-xyz')])
        self.assertEquals(matcher.matcher('bar -xyz', s).match(), [matchedresult])

        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 6, None, '-x'),
                                 (6, 7, '-a desc', 'a'), (7, 8, None, 'z')])

        self.assertEquals(matcher.matcher('bar -xaz', s).match(), [matchedresult])
Ejemplo n.º 13
0
    def test_arg_with_expected_value(self):
        cmd = 'bar -b arg --b arg'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 18, '-b <arg> desc', '-b arg --b arg')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 14
0
    def test_known_arg(self):
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 10, '-a desc', '-a --a'),
            (11, 13, '-? help text', '-?')])

        self.assertEquals(matcher.matcher('bar -a --a -?', s).match(), [matchedresult])
Ejemplo n.º 15
0
def explain(section, program):
    s = store.store('explainshell', config.MONGO_URI)
    try:
        if 'args' in request.args:
            args = request.args['args']
            command = '%s %s' % (program, args)
            matcher_ = matcher.matcher(command, s, section)
            mrs = matcher_.match()
            mr = mrs[0][1]
            l = []
            it = util.peekable(iter(mr))
            while it.hasnext():
                m = it.next()
                spaces = 0
                if it.hasnext():
                    spaces = it.peek().start - m.end
                spaces = ' ' * spaces
                text = m.text
                if text:
                    text = text.decode('utf-8')
                d = {
                    'match': m.match,
                    'unknown': m.unknown,
                    'text': text,
                    'spaces': spaces
                }
                l.append(d)

            d = l[0]
            d['section'] = matcher_.manpage.section
            d['match'] = '%s(%s)' % (d['match'], d['section'])
            d['source'] = matcher_.manpage.source[:-5]
            others = helpers.others([x[0] for x in mrs[1:]])

            return render_template('explain.html',
                                   program=l[0],
                                   matches=l,
                                   othersections=others,
                                   getargs=args)
        else:
            logger.info('/explain section=%r program=%r', section, program)
            mps = s.findmanpage(program, section)
            mp = mps.pop(0)
            program = mp.namesection

            mp = {
                'source': mp.source[:-3],
                'section': mp.section,
                'program': program,
                'synopsis': mp.synopsis,
                'options': [o.text.decode('utf-8') for o in mp.options]
            }

            othersections = helpers.others(mps)
            logger.info('others: %s', othersections)
            return render_template('options.html',
                                   mp=mp,
                                   othersections=helpers.others(mps))
    except errors.ProgramDoesNotExist, e:
        return render_template('error.html', prog=e.args[0])
Ejemplo n.º 16
0
    def test(self):
        mngr = manager.manager(
            'localhost',
            'explainshell_tests',
            [os.path.join(os.path.dirname(__file__), 'echo.1.gz')],
            drop=True)
        mngr.run()

        cmd = 'echo -en foobar --version'

        m = matcher.matcher(cmd, mngr.store)
        group = m.match()[1]
        matchprog, matches = group.manpage.name, group.results

        self.assertEquals(matchprog, 'echo')

        #self.assertEquals(matches[0].text, 'display a line of text')
        self.assertEquals(matches[0].match, 'echo')

        self.assertEquals(
            matches[1].text,
            '<b>-e</b>     enable interpretation of backslash escapes')
        self.assertEquals(matches[1].match, '-e')

        self.assertEquals(matches[2].text,
                          '<b>-n</b>     do not output the trailing newline')
        self.assertEquals(matches[2].match, 'n')

        self.assertEquals(matches[3].text, None)
        self.assertEquals(matches[3].match, 'foobar')

        self.assertEquals(
            matches[4].text,
            '<b>--version</b>\n       output version information and exit')
        self.assertEquals(matches[4].match, '--version')
Ejemplo n.º 17
0
    def test_arg_no_dash(self):
        cmd = 'baz ab -x'
        matchedresult = ('baz', [(0, 3, 'baz synopsis', 'baz'),
                                 (4, 5, '-a desc', 'a'),
                                 (5, 6, '-b <arg> desc', 'b'),
                                 (7, 9, None, '-x')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 18
0
    def test_arg_in_fuzzy_with_expected_value(self):
        cmd = 'baz -ab arg'
        matchedresult = ('baz', [
            (0, 3, 'baz synopsis', 'baz'),
            (4, 6, '-a desc', '-a'),
            (6, 11, '-b <arg> desc', 'b arg')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 19
0
    def test_arg_is_dash(self):
        cmd = 'bar -b - -a -'
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 8, '-b <arg> desc', '-b -'),
                                 (9, 11, '-a desc', '-a'),
                                 (12, 13, None, '-')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 20
0
    def test_arg_with_expected_value_clash(self):
        '''the first option expects an arg but the arg is actually an option'''
        cmd = 'bar -b -a'
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 6, '-b <arg> desc', '-b'),
                                 (7, 9, '-a desc', '-a')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 21
0
    def test_multiple_matches(self):
        cmd = 'dup -ab'
        matchedresult = [(0, 3, 'dup1 synopsis', 'dup'),
                         (4, 6, '-a desc', '-a'), (6, 7, '-b <arg> desc', 'b')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(groups[1].results, matchedresult)
        self.assertEquals(groups[1].suggestions[0].source, 'dup.2.gz')
Ejemplo n.º 22
0
    def test_redirect_first_word_of_command(self):
        cmd = '2>&1'
        matchedresult = [(0, 4, helpconstants.REDIRECTION + '\n\n' +
                                helpconstants.REDIRECTION_KIND['>'], '2>&1')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 1)
        self.assertEquals(groups[0].results, matchedresult)

        cmd = '2>&1 bar'
        matchedresult = [[(0, 4, helpconstants.REDIRECTION + '\n\n' +
                                 helpconstants.REDIRECTION_KIND['>'], '2>&1')],
                         [(5, 8, 'bar synopsis', 'bar')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, matchedresult[0])
        self.assertEquals(groups[1].results, matchedresult[1])
Ejemplo n.º 23
0
    def test_multiple_matches(self):
        cmd = 'dup -ab'
        matchedresult = ('dup', [(0, 3, 'dup1 synopsis', 'dup'),
                                 (4, 6, '-a desc', '-a'),
                                 (6, 7, '-b <arg> desc', 'b')])

        mr = matcher.matcher(cmd, s).match()
        self.assertEquals(mr[0], matchedresult)
        self.assertEquals(mr[1][0].source, 'dup.2.gz')
Ejemplo n.º 24
0
    def test_arg_no_dash(self):
        cmd = 'baz ab -x'
        matchedresult = ('baz', [
            (0, 3, 'baz synopsis', 'baz'),
            (4, 5, '-a desc', 'a'),
            (5, 6, '-b <arg> desc', 'b'),
            (7, 9, None, '-x')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 25
0
    def test_arg_with_expected_value_no_clash(self):
        '''the first option expects an arg but the arg is not an option even though
        it looks like one'''
        cmd = 'bar -b -xa'
        matchedresult = ('bar', [(0, 3, 'bar synopsis', 'bar'),
                                 (4, 6, '-b <arg> desc', '-b'),
                                 (7, 9, None, '-x'), (9, 10, '-a desc', 'a')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 26
0
    def test_arg_is_dash(self):
        cmd = 'bar -b - -a -'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 8, '-b <arg> desc', '-b -'),
            (9, 11, '-a desc', '-a'),
            (12, 13, None, '-')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 27
0
    def test_arg_with_expected_value_clash(self):
        '''the first option expects an arg but the arg is actually an option'''
        cmd = 'bar -b -a'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 6, '-b <arg> desc', '-b'),
            (7, 9, '-a desc', '-a')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 28
0
    def test_pipe(self):
        cmd = 'bar | baz'
        matchedresult = [[(4, 5, helpconstants.PIPELINES, '|')],
                         [(0, 3, 'bar synopsis', 'bar')],
                         [(6, 9, 'baz synopsis', 'baz')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(groups[0].results, matchedresult[0])
        self.assertEquals(groups[1].results, matchedresult[1])
Ejemplo n.º 29
0
    def test_assignment_with_expansion(self):
        cmd = 'a="$1" bar'

        shellresults = [(0, 6, helpconstants.ASSIGNMENT, 'a="$1"')]
        matchresults = [[(7, 10, 'bar synopsis', 'bar')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults[0])
Ejemplo n.º 30
0
    def test_no_synopsis(self):
        cmd = 'nosynopsis a'

        matchresults = [(0, 10, helpconstants.NOSYNOPSIS, 'nosynopsis'),
                        (11, 12, None, 'a')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEqual(len(groups), 2)
        self.assertEqual(groups[0].results, [])
        self.assertEqual(groups[1].results, matchresults)
Ejemplo n.º 31
0
    def test_no_synopsis(self):
        cmd = 'nosynopsis a'

        matchresults = [(0, 10, helpconstants.NOSYNOPSIS, 'nosynopsis'),
                        (11, 12, None, 'a')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, matchresults)
Ejemplo n.º 32
0
    def test_assignment_as_first_word(self):
        cmd = 'a=b bar'

        shellresults = [(0, 3, helpconstants.ASSIGNMENT, 'a=b')]
        matchresults = [(4, 7, 'bar synopsis', 'bar')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)
Ejemplo n.º 33
0
    def test_assignment_with_expansion(self):
        cmd = 'a="$1" bar'

        shellresults = [(0, 6, helpconstants.ASSIGNMENT, 'a="$1"')]
        matchresults = [[(7, 10, 'bar synopsis', 'bar')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults[0])
Ejemplo n.º 34
0
    def test_assignment_as_first_word(self):
        cmd = 'a=b bar'

        shellresults = [(0, 3, helpconstants.ASSIGNMENT, 'a=b')]
        matchresults = [(4, 7, 'bar synopsis', 'bar')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)
Ejemplo n.º 35
0
    def test_multiple_matches(self):
        cmd = 'dup -ab'
        matchedresult = [
            (0, 3, 'dup1 synopsis', 'dup'),
            (4, 6, '-a desc', '-a'),
            (6, 7, '-b <arg> desc', 'b')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(groups[1].results, matchedresult)
        self.assertEquals(groups[1].suggestions[0].source, 'dup.2.gz')
Ejemplo n.º 36
0
    def test_multiple_matches(self):
        cmd = 'dup -ab'
        matchedresult = ('dup', [
            (0, 3, 'dup1 synopsis', 'dup'),
            (4, 6, '-a desc', '-a'),
            (6, 7, '-b <arg> desc', 'b')])

        mr = matcher.matcher(cmd, s).match()
        self.assertEquals(mr[0], matchedresult)
        self.assertEquals(mr[1][0].source, 'dup.2.gz')
Ejemplo n.º 37
0
    def test_arguments(self):
        cmd = 'withargs -x -b freearg freearg'
        matchedresult = ('withargs', [
            (0, 8, 'withargs synopsis', 'withargs'),
            # tokens that look like options are still unknown
            (9, 11, None, '-x'),
            (12, 22, '-b <arg> desc', '-b freearg'),
            (23, 30, 'FILE argument', 'freearg')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 38
0
    def test_comsub_as_first_word(self):
        cmd = '$(a) b'

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, [(0, 6, None, '$(a) b')])

        # check expansions
        self.assertEquals(m.expansions, [(2, 3, 'substitution')])
Ejemplo n.º 39
0
    def test_comment(self):
        cmd = 'bar # a comment'

        shellresults = [(4, 15, helpconstants.COMMENT, '# a comment')]
        matchresults = [(0, 3, 'bar synopsis', 'bar')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)

        cmd = '# just a comment'

        shellresults = [(0, 16, helpconstants.COMMENT, '# just a comment')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 1)
        self.assertEquals(groups[0].results, shellresults)
Ejemplo n.º 40
0
    def test_comsub_as_first_word(self):
        cmd = '$(a) b'

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, [(0, 6, None, '$(a) b')])

        # check expansions
        self.assertEquals(m.expansions, [(2, 3, 'substitution')])
Ejemplo n.º 41
0
    def test_arg_with_expected_value_no_clash(self):
        '''the first option expects an arg but the arg is not an option even though
        it looks like one'''
        cmd = 'bar -b -xa'
        matchedresult = ('bar', [
            (0, 3, 'bar synopsis', 'bar'),
            (4, 6, '-b <arg> desc', '-b'),
            (7, 9, None, '-x'),
            (9, 10, '-a desc', 'a')])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 42
0
    def test_comment(self):
        cmd = 'bar # a comment'

        shellresults = [(4, 15, helpconstants.COMMENT, '# a comment')]
        matchresults = [(0, 3, 'bar synopsis', 'bar')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)

        cmd = '# just a comment'

        shellresults = [(0, 16, helpconstants.COMMENT, '# just a comment')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 1)
        self.assertEquals(groups[0].results, shellresults)
Ejemplo n.º 43
0
    def test_nested_command_is_unknown(self):
        cmd = 'withargs -b arg unknown'

        matchedresult = [(0, 8, 'withargs synopsis', 'withargs'),
                          (9, 15, '-b <arg> desc', '-b arg'),
                          (16, 23, 'FILE argument', 'unknown')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, matchedresult)
Ejemplo n.º 44
0
    def test_heredoc_at_eof(self):
        cmd = 'bar <<EOF'

        shellresults = [(4, 9, helpconstants.REDIRECTION + '\n\n' +
                               helpconstants.REDIRECTION_KIND['<<'], '<<EOF')]

        matchresults = [(0, 3, 'bar synopsis', 'bar')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)
Ejemplo n.º 45
0
    def test_heredoc_at_eof(self):
        cmd = 'bar <<EOF'

        shellresults = [(4, 9, helpconstants.REDIRECTION + '\n\n' +
                         helpconstants.REDIRECTION_KIND['<<'], '<<EOF')]

        matchresults = [(0, 3, 'bar synopsis', 'bar')]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)
Ejemplo n.º 46
0
    def test_subshells(self):
        cmd = '((bar); bar)'
        matchedresult = [[(0, 2, helpconstants.SUBSHELL, '(('),
                          (5, 6, helpconstants.SUBSHELL, ')'),
                          (6, 7, helpconstants.OPERATORS[';'], ';'),
                          (11, 12, helpconstants.SUBSHELL, ')')],
                         [(2, 5, 'bar synopsis', 'bar')],
                         [(8, 11, 'bar synopsis', 'bar')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(groups[0].results, matchedresult[0])
        self.assertEquals(groups[1].results, matchedresult[1])
        self.assertEquals(groups[2].results, matchedresult[2])
Ejemplo n.º 47
0
    def test_unparsed(self):
        cmd = '(bar; bar) c'
        matchedresult = [[(0, 1, helpconstants.SUBSHELL, '('),
                          (4, 5, helpconstants.OPERATORS[';'], ';'),
                          (9, 10, helpconstants.SUBSHELL, ')'),
                          (11, 12, None, 'c')],
                         [(1, 4, 'bar synopsis', 'bar')],
                         [(6, 9, 'bar synopsis', 'bar')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(groups[0].results, matchedresult[0])
        self.assertEquals(groups[1].results, matchedresult[1])
        self.assertEquals(groups[2].results, matchedresult[2])
Ejemplo n.º 48
0
    def test_arguments(self):
        cmd = 'withargs -x -b freearg freearg'
        matchedresult = (
            'withargs',
            [
                (0, 8, 'withargs synopsis', 'withargs'),
                # tokens that look like options are still unknown
                (9, 11, None, '-x'),
                (12, 22, '-b <arg> desc', '-b freearg'),
                (23, 30, 'FILE argument', 'freearg')
            ])

        self.assertEquals(matcher.matcher(cmd, s).match(), [matchedresult])
Ejemplo n.º 49
0
    def test_comsub_as_arg(self):
        cmd = 'withargs $(a)'

        matchedresult = [(0, 8, 'withargs synopsis', 'withargs'),
                         (9, 13, 'FILE argument', '$(a)')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, matchedresult)

        # check expansions
        self.assertEquals(m.expansions, [(11, 12, 'substitution')])
Ejemplo n.º 50
0
    def test_comsub_as_arg(self):
        cmd = 'withargs $(a)'

        matchedresult = [(0, 8, 'withargs synopsis', 'withargs'),
                         (9, 13, 'FILE argument', '$(a)')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, matchedresult)

        # check expansions
        self.assertEquals(m.expansions, [(11, 12, 'substitution')])
Ejemplo n.º 51
0
def explain(section, program):
    s = store.store('explainshell', config.MONGO_URI)
    try:
        if 'args' in request.args:
            args = request.args['args']
            if program is None:
                program = args.split(' ')[0]
                args = ' '.join(args.split(' ')[1:])
            command = '%s %s' % (program, args)
            matcher_ = matcher.matcher(command, s, section)
            mrs = matcher_.match()
            mr = mrs[0][1]
            l = []
            it = util.peekable(iter(mr))
            while it.hasnext():
                m = it.next()
                spaces = 0
                if it.hasnext():
                    spaces = it.peek().start - m.end
                spaces = ' ' * spaces
                text = m.text
                if text:
                    text = text.decode('utf-8')
                d = {'match' : m.match, 'unknown' : m.unknown, 'text' : text, 'spaces' : spaces}
                l.append(d)

            d = l[0]
            d['section'] = matcher_.manpage.section
            d['match'] = '%s(%s)' % (d['match'], d['section'])
            d['source'] = matcher_.manpage.source[:-5]
            others = helpers.others([x[0] for x in mrs[1:]])

            return render_template('explain.html', program=l[0], matches=l,
                                   othersections=others, getargs=args)
        else:
            logger.info('/explain section=%r program=%r', section, program)
            mps = s.findmanpage(program, section)
            mp = mps.pop(0)
            program = mp.namesection

            mp = {'source' : mp.source[:-3],
                  'section' : mp.section,
                  'program' : program,
                  'synopsis' : mp.synopsis,
                  'options' : [o.text.decode('utf-8') for o in mp.options]}

            othersections = helpers.others(mps)
            logger.info('others: %s', othersections)
            return render_template('options.html', mp=mp, othersections=helpers.others(mps))
    except errors.ProgramDoesNotExist, e:
        return render_template('error.html', prog=e.args[0])
Ejemplo n.º 52
0
    def test_known_and_unknown_program(self):
        cmd = 'bar; foo arg >f; baz'
        matchedresult = [[(3, 4, helpconstants.OPERATORS[';'], ';'),
                          (13, 15,helpconstants.REDIRECTION + '\n\n' +
                                  helpconstants.REDIRECTION_KIND['>'], '>f'),
                          (15, 16, helpconstants.OPERATORS[';'], ';')],
                         [(0, 3, 'bar synopsis', 'bar')],
                         [(5, 12, None, 'foo arg')],
                         [(17, 20, 'baz synopsis', 'baz')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(groups[0].results, matchedresult[0])
        self.assertEquals(groups[1].results, matchedresult[1])
        self.assertEquals(groups[2].results, matchedresult[2])
Ejemplo n.º 53
0
    def test_nested_command(self):
        cmd = 'withargs -b arg bar -a unknown'

        matchedresult = [[(0, 8, 'withargs synopsis', 'withargs'),
                          (9, 15, '-b <arg> desc', '-b arg')],
                         [(16, 19, 'bar synopsis', 'bar'),
                          (20, 22, '-a desc', '-a'),
                          (23, 30, None, 'unknown')]]

        groups = matcher.matcher(cmd, s).match()
        self.assertEquals(len(groups), 3)
        self.assertEquals(groups[0].results, [])
        self.assertEquals(groups[1].results, matchedresult[0])
        self.assertEquals(groups[2].results, matchedresult[1])
Ejemplo n.º 54
0
    def test_for_expansion(self):
        cmd = 'for a in $(bar); do baz; done'
        shellresults = [(0, 19, helpconstants._for, 'for a in $(bar); do'),
                        (23, 29, helpconstants._for, '; done')]

        matchresults = [(20, 23, 'baz synopsis', 'baz')]

        m = matcher.matcher(cmd, s)
        groups = m.match()
        self.assertEquals(len(groups), 2)
        self.assertEquals(groups[0].results, shellresults)
        self.assertEquals(groups[1].results, matchresults)

        self.assertEquals(m.expansions, [(11, 14, 'substitution')])