Test Failed
Pull Request — master (#4197)
by W
03:53
created

DigAction.run()   B

Complexity

Conditions 7

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 29
rs 7.784
c 0
b 0
f 0
1
#! /usr/bin/python
2
3
import subprocess
4
import random
5
import re
6
from st2common.runners.base_action import Action
7
8
9
class DigAction(Action):
10
11
    def run(self, rand, count, nameserver, hostname, queryopts):
12
        opt_list = []
13
        output = []
14
15
        cmd_args = ['dig']
16
        if nameserver:
17
            nameserver = '@' + nameserver
18
            cmd_args.append(nameserver)
19
20
        if re.search(',', queryopts):
21
            opt_list = queryopts.split(',')
22
        else:
23
            opt_list.append(queryopts)
24
        for k, v in enumerate(opt_list):
25
            cmd_args.append('+' + v)
26
27
        cmd_args.append(hostname)
28
        result_list = filter(None, subprocess.Popen(cmd_args,
29
                                                    stderr=subprocess.PIPE,
30
                                                    stdout=subprocess.PIPE)
31
                                             .communicate()[0]
32
                                             .split('\n'))
33
        if int(count) > len(result_list) or count <= 0:
34
            count = len(result_list)
35
36
        output = result_list[0:count]
37
        if rand is True:
38
            random.shuffle(output)
39
        return output
40