Completed
Pull Request — master (#346)
by Anthony
04:32
created

generate_meta()   B

Complexity

Conditions 4

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 40
rs 8.5806
1
#!/usr/bin/python
2
3
import sys, re
4
import fabric.docs
5
import fabric, simplejson, inspect, pprint
6
from lib import fabfile
7
8
action_dir = "./"
9
10
def generate_meta(fabfile):
11
 for i in dir(fabfile):
12
  action_meta = {}
13
  fabtask = getattr(fabfile,i)
14
  if isinstance(fabtask,fabric.tasks.WrappedCallableTask):
15
    print "%s is a Fabric Callable Task..." % i
16
    fabparams = getArgs(i,fabfile)
17
    print "\n"
18
    try:
19
      action_meta['name'] = fabtask.wrapped.func_name
20
      action_meta['description'] =  fabtask.wrapped.func_doc
21
    except TypeError, e:
22
      print e
23
      next
24
    action_meta['entry_point'] = "fabaction.py"
25
    action_meta['runner_type'] = "run-local-script"
26
    action_meta['enabled'] = True
27
28
    parameters = {}
29
    parameters['kwarg_op'] = {"immutable": True, "type": "string", "default": ""}
30
    parameters['user'] = {"immutable": True}
31
    parameters['dir'] = {"immutable": True}
32
    parameters["task"] = {  "type": "string",
33
                            "description": "task name to be executed",
34
                            "immutable": True,
35
                            "default": fabtask.wrapped.func_name }
36
    if fabparams:
37
      parameters.update(fabparams)
38
    action_meta['parameters'] = parameters
39
40
    fname = action_dir + action_meta['name'] + ".json"
41
    try:
42
      print "Writing %s..." % fname
43
      fh = open(fname, 'w')
44
      fh.write(simplejson.dumps(action_meta,indent=2,sort_keys=True))
45
    except:
46
      print "Could not write file %s" % fname
47
      next
48
49
    print "\n"
50
51
def getArgs(task, fabfile):
52
  args = {}
53
  sourcelines = inspect.getsourcelines(fabfile)[0]
54
  for i, line in enumerate(sourcelines):
55
    line = line.rstrip()
56
    pattern = re.compile('def ' + task + '\(')
57
    if pattern.search(line):
58
      filtered = filter(None,re.split('\((.*)\):.*',line))
59
      if len(filtered) < 2:
60
        return None
61
62
      argstring = filtered[1]
63
      for arg in argstring.split(','):
64
        if re.search('=',arg):
65
          arg,v = arg.split('=')
66
          if v == "''" or v == '""' or v == 'None':
67
            value={"type":"string"}
68
          else:
69
            value={"type":"string","default":v.strip()}
70
        else:
71
          value={"type":"string"}
72
        args[arg.strip()]=value
73
  return args
74
75
generate_meta(fabfile)
76