Bug 1310937 - Autophone - mozdevice - sync Bug 1238450 - Autophone - adb.py - use ifconfig interface or netcfg to obtain ip address, r=gbrown
authorBob Clary <bclary@bclary.com>
Wed, 19 Oct 2016 06:06:54 -0700
changeset 318537 815de87b6dbaad501c84fa221630e9597f0caaf6
parent 318536 a9ee75a0ae5a70198dc86bb6d6d1edeebaa04564
child 318538 5b6b91ca5d506ba30ecf1a5d800e53be76c76fdd
push id82957
push userbclary@mozilla.com
push dateWed, 19 Oct 2016 13:07:48 +0000
treeherdermozilla-inbound@7362a3b3d239 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgbrown
bugs1310937, 1238450
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1310937 - Autophone - mozdevice - sync Bug 1238450 - Autophone - adb.py - use ifconfig interface or netcfg to obtain ip address, r=gbrown
testing/mozbase/mozdevice/mozdevice/adb.py
--- a/testing/mozbase/mozdevice/mozdevice/adb.py
+++ b/testing/mozbase/mozdevice/mozdevice/adb.py
@@ -1273,30 +1273,114 @@ class ADBDevice(ADBCommand):
             may exceed this value. If it is not specified, the value
             set in the ADBDevice constructor is used.
         :type timeout: integer or None
         :returns: string ip address of the device or None if it could not
             be found.
         :raises: * ADBTimeoutError
                  * ADBError
         """
-        ip_regexp = re.compile(r'(\w+)\s+UP\s+([1-9]\d{0,2}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
-        data = self.shell_output('netcfg')
-        for line in data.split("\n"):
-            match = ip_regexp.search(line)
-            if match:
-                interface, ip = match.groups()
+        if not interfaces:
+            interfaces = ["wlan0", "eth0"]
+            wifi_interface = self.shell_output('getprop wifi.interface', timeout=timeout)
+            self._logger.debug('get_ip_address: wifi_interface: %s' % wifi_interface)
+            if wifi_interface and wifi_interface not in interfaces:
+                interfaces = interfaces.append(wifi_interface)
+
+        # ifconfig interface
+        # can return two different formats:
+        # eth0: ip 192.168.1.139 mask 255.255.255.0 flags [up broadcast running multicast]
+        # or
+        # wlan0     Link encap:Ethernet  HWaddr 00:9A:CD:B8:39:65
+        # inet addr:192.168.1.38  Bcast:192.168.1.255  Mask:255.255.255.0
+        # inet6 addr: fe80::29a:cdff:feb8:3965/64 Scope: Link
+        # UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
+        # RX packets:180 errors:0 dropped:0 overruns:0 frame:0
+        # TX packets:218 errors:0 dropped:0 overruns:0 carrier:0
+        # collisions:0 txqueuelen:1000
+        # RX bytes:84577 TX bytes:31202
+
+        re1_ip = re.compile(r'(\w+): ip ([0-9.]+) mask.*')
+        # re1_ip will match output of the first format
+        # with group 1 returning the interface and group 2 returing the ip address.
+
+        # re2_interface will match the interface line in the second format
+        # while re2_ip will match the inet addr line of the second format.
+        re2_interface = re.compile(r'(\w+)\s+Link')
+        re2_ip = re.compile(r'\s+inet addr:([0-9.]+)')
+
+        matched_interface = None
+        matched_ip = None
+        re_bad_addr = re.compile(r'127.0.0.1|0.0.0.0')
+
+        self._logger.debug('get_ip_address: ifconfig')
+        for interface in interfaces:
+            try:
+                output = self.shell_output('ifconfig %s' % interface,
+                                           timeout=timeout)
+            except ADBError:
+                output = ''
 
-                if interface == "lo" or ip == "127.0.0.1":
-                    continue
+            for line in output.split("\n"):
+                if not matched_interface:
+                    match = re1_ip.match(line)
+                    if match:
+                        matched_interface, matched_ip = match.groups()
+                    else:
+                        match = re2_interface.match(line)
+                        if match:
+                            matched_interface = match.group(1)
+                else:
+                    match = re2_ip.match(line)
+                    if match:
+                        matched_ip = match.group(1)
+
+                if matched_ip:
+                    if not re_bad_addr.match(matched_ip):
+                        self._logger.debug('get_ip_address: found: %s %s' %
+                                           (matched_interface, matched_ip))
+                        return matched_ip
+                    matched_interface = None
+                    matched_ip = None
 
-                if interfaces is None or interface in interfaces:
-                    return ip
+        self._logger.debug('get_ip_address: netcfg')
+        # Fall back on netcfg if ifconfig does not work.
+        # $ adb shell netcfg
+        # lo       UP   127.0.0.1/8       0x00000049 00:00:00:00:00:00
+        # dummy0   DOWN   0.0.0.0/0       0x00000082 8e:cd:67:48:b7:c2
+        # rmnet0   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet1   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet2   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet3   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet4   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet5   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet6   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # rmnet7   DOWN   0.0.0.0/0       0x00000000 00:00:00:00:00:00
+        # sit0     DOWN   0.0.0.0/0       0x00000080 00:00:00:00:00:00
+        # vip0     DOWN   0.0.0.0/0       0x00001012 00:01:00:00:00:01
+        # wlan0    UP   192.168.1.157/24  0x00001043 38:aa:3c:1c:f6:94
 
-        return None
+        re3_netcfg = re.compile(r'(\w+)\s+UP\s+([1-9]\d{0,2}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
+        try:
+            output = self.shell_output('netcfg', timeout=timeout)
+        except ADBError:
+            output = ''
+        for line in output.split("\n"):
+            match = re3_netcfg.search(line)
+            if match:
+                matched_interface, matched_ip = match.groups()
+                if matched_interface == "lo" or re_bad_addr.match(matched_ip):
+                    matched_interface = None
+                    matched_ip = None
+                elif matched_ip and matched_interface in interfaces:
+                    self._logger.debug('get_ip_address: found: %s %s' %
+                                       (matched_interface, matched_ip))
+                    return matched_ip
+        self._logger.debug('get_ip_address: not found')
+        return matched_ip
 
     # File management methods
 
     def remount(self, timeout=None):
         """Remount /system/ in read/write mode
 
         :param timeout: The maximum time in
             seconds for any spawned adb process to complete before throwing