Conditions | 13 |
Total Lines | 51 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 182 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ssg_test_suite.virt.determine_ip() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python2 |
||
109 | def determine_ip(domain): |
||
110 | GUEST_AGENT_XML = ("<channel type='unix'>" |
||
111 | " <source mode='bind'/>" |
||
112 | " <target type='virtio'" |
||
113 | " name='org.qemu.guest_agent.0'" |
||
114 | " state='connected'/>" |
||
115 | "</channel>") |
||
116 | |||
117 | # wait for machine until it gets to RUNNING state, |
||
118 | # because it isn't possible to determine IP in e.g. PAUSED state |
||
119 | must_end = time.time() + 120 # wait max. 2 minutes |
||
120 | while time.time() < must_end: |
||
121 | if domain.state()[0] == libvirt.VIR_DOMAIN_RUNNING: |
||
122 | break |
||
123 | time.sleep(1) |
||
124 | |||
125 | domain_xml = ET.fromstring(domain.XMLDesc()) |
||
126 | for mac_node in domain_xml.iter('mac'): |
||
127 | domain_mac = mac_node.attrib['address'] |
||
128 | break |
||
129 | |||
130 | logging.debug('Fetching IP address of the domain') |
||
131 | try: |
||
132 | ifaces = domain.interfaceAddresses( |
||
133 | libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, |
||
134 | 0) |
||
135 | except libvirt.libvirtError: |
||
136 | # guest agent is not connected properly |
||
137 | # let's try to reattach the guest-agent device |
||
138 | guest_agent_xml_string = None |
||
139 | domain_xml = ET.fromstring(domain.XMLDesc()) |
||
140 | for guest_agent_node in domain_xml.iter('channel'): |
||
141 | if guest_agent_node.attrib['type'] == 'unix': |
||
142 | guest_agent_xml_string = ET.tostring(guest_agent_node) |
||
143 | break |
||
144 | if guest_agent_xml_string: |
||
145 | domain.detachDevice(guest_agent_xml_string) |
||
146 | domain.attachDevice(GUEST_AGENT_XML) |
||
147 | time.sleep(1) |
||
148 | # now it should be ok |
||
149 | ifaces = domain.interfaceAddresses( |
||
150 | libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, |
||
151 | 0) |
||
152 | |||
153 | # get IPv4 address of the guest |
||
154 | for (name, val) in ifaces.items(): |
||
155 | if val['hwaddr'] == domain_mac and val['addrs']: |
||
|
|||
156 | for ipaddr in val['addrs']: |
||
157 | if ipaddr['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV4: |
||
158 | logging.debug('IP address is {0}'.format(ipaddr['addr'])) |
||
159 | return ipaddr['addr'] |
||
160 | |||
207 |