ipaddrs: import function from latest Tahoe
I'm hoping this will help with Cygwin (#13), by removing/bypassing the assert(os.path.isabs) check.
This commit is contained in:
parent
58f20d79a7
commit
2596c58e4a
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import os, re, subprocess, errno
|
import os, re, subprocess, errno
|
||||||
from sys import platform
|
from sys import platform
|
||||||
|
from twisted.python.procutils import which
|
||||||
|
|
||||||
# Wow, I'm really amazed at home much mileage we've gotten out of calling
|
# Wow, I'm really amazed at home much mileage we've gotten out of calling
|
||||||
# the external route.exe program on windows... It appears to work on all
|
# the external route.exe program on windows... It appears to work on all
|
||||||
|
@ -14,6 +15,7 @@ _win32_commands = (('route.exe', ('print',), _win32_re),)
|
||||||
# These work in most Unices.
|
# These work in most Unices.
|
||||||
_addr_re = re.compile(r'^\s*inet [a-zA-Z]*:?(?P<address>\d+\.\d+\.\d+\.\d+)[\s/].+$', flags=re.M|re.I|re.S)
|
_addr_re = re.compile(r'^\s*inet [a-zA-Z]*:?(?P<address>\d+\.\d+\.\d+\.\d+)[\s/].+$', flags=re.M|re.I|re.S)
|
||||||
_unix_commands = (('/bin/ip', ('addr',), _addr_re),
|
_unix_commands = (('/bin/ip', ('addr',), _addr_re),
|
||||||
|
('/sbin/ip', ('addr',), _addr_re),
|
||||||
('/sbin/ifconfig', ('-a',), _addr_re),
|
('/sbin/ifconfig', ('-a',), _addr_re),
|
||||||
('/usr/sbin/ifconfig', ('-a',), _addr_re),
|
('/usr/sbin/ifconfig', ('-a',), _addr_re),
|
||||||
('/usr/etc/ifconfig', ('-a',), _addr_re),
|
('/usr/etc/ifconfig', ('-a',), _addr_re),
|
||||||
|
@ -32,16 +34,24 @@ def find_addresses():
|
||||||
commands = _unix_commands
|
commands = _unix_commands
|
||||||
|
|
||||||
for (pathtotool, args, regex) in commands:
|
for (pathtotool, args, regex) in commands:
|
||||||
if platform != 'win32':
|
# If pathtotool is a fully qualified path then we just try that.
|
||||||
assert os.path.isabs(pathtotool), pathtotool
|
# If it is merely an executable name then we use Twisted's
|
||||||
if not os.path.isfile(pathtotool):
|
# "which()" utility and try each executable in turn until one
|
||||||
continue
|
# gives us something that resembles a dotted-quad IPv4 address.
|
||||||
try:
|
|
||||||
addresses = _query(pathtotool, args, regex)
|
if os.path.isabs(pathtotool):
|
||||||
except Exception:
|
exes_to_try = [pathtotool]
|
||||||
addresses = []
|
else:
|
||||||
if addresses:
|
exes_to_try = which(pathtotool)
|
||||||
return addresses
|
|
||||||
|
for exe in exes_to_try:
|
||||||
|
try:
|
||||||
|
addresses = _query(exe, args, regex)
|
||||||
|
except Exception:
|
||||||
|
addresses = []
|
||||||
|
if addresses:
|
||||||
|
return addresses
|
||||||
|
|
||||||
return ["127.0.0.1"]
|
return ["127.0.0.1"]
|
||||||
|
|
||||||
def _query(path, args, regex):
|
def _query(path, args, regex):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user