~jbauer/cloud-init/salt

« back to all changes in this revision

Viewing changes to cloudinit/netinfo.py

  • Committer: Scott Moser
  • Date: 2012-01-20 13:44:45 UTC
  • Revision ID: smoser@ubuntu.com-20120120134445-3f1hptdh546e35xc
in netinfo output (ci-info:), fill in empty fields with a "."

If you were trying to parse this output with something, an empty field would be
difficult to handle, as you'd have to know the expected lengths of each field.
The '.' means empty, but then all fields are non-whitespace delimited by one or
more whitespace.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import subprocess
23
23
 
24
24
 
25
 
def netdev_info():
 
25
def netdev_info(empty=""):
26
26
    fields = ("hwaddr", "addr", "bcast", "mask")
27
27
    ifcfg_out = str(subprocess.check_output(["ifconfig", "-a"]))
28
28
    devs = {}
59
59
                        pass
60
60
                elif toks[i].startswith("%s:" % field):
61
61
                    devs[curdev][target] = toks[i][len(field) + 1:]
 
62
 
 
63
    if empty != "":
 
64
        for (devname, dev) in devs.iteritems():
 
65
            for field in dev:
 
66
                if dev[field] == "":
 
67
                    dev[field] = empty
 
68
 
62
69
    return(devs)
63
70
 
64
71
 
85
92
def debug_info(pre="ci-info: "):
86
93
    lines = []
87
94
    try:
88
 
        netdev = netdev_info()
 
95
        netdev = netdev_info(empty=".")
89
96
    except Exception:
90
97
        lines.append("netdev_info failed!")
91
98
        netdev = {}