Bug 1160087 - [moznetwork] Add command line interface. r=wlach

--HG--
extra : rebase_source : 71181891478fe3c64271435c3e728bb6fa030864
This commit is contained in:
Dave Hunt 2015-04-30 02:33:00 -04:00
parent fb8ad5dbce
commit 613906b8dc
2 changed files with 23 additions and 3 deletions

View File

@ -2,16 +2,20 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import socket
import argparse
import array
import re
import socket
import struct
import subprocess
import sys
import mozinfo
if mozinfo.isLinux:
import fcntl
class NetworkError(Exception):
"""Exception thrown when unable to obtain interface or IP."""
@ -34,12 +38,13 @@ def _get_interface_list():
))[0]
namestr = names.tostring()
return [(namestr[i:i + 32].split('\0', 1)[0],
socket.inet_ntoa(namestr[i + 20:i + 24]))\
socket.inet_ntoa(namestr[i + 20:i + 24]))
for i in range(0, outbytes, struct_size)]
except IOError:
raise NetworkError('Unable to call ioctl with SIOCGIFCONF')
def _proc_matches(args, regex):
"""Helper returns the matches of regex in the output of a process created with
the given arguments"""
@ -48,6 +53,7 @@ def _proc_matches(args, regex):
stderr=subprocess.STDOUT).stdout.read()
return re.findall(regex, output)
def _parse_ifconfig():
"""Parse the output of running ifconfig on mac in cases other methods
have failed"""
@ -77,6 +83,7 @@ def _parse_ifconfig():
except IndexError:
return None
def get_ip():
"""Provides an available network interface address, for example
"192.168.1.3".
@ -112,3 +119,14 @@ def get_ip():
def get_lan_ip():
"""Deprecated. Please use get_ip() instead."""
return get_ip()
def cli(args=sys.argv[1:]):
parser = argparse.ArgumentParser(
description='Retrieve IP address')
parser.parse_args()
print 'IP address: %s' % get_ip()
if __name__ == '__main__':
cli()

View File

@ -21,5 +21,7 @@ setup(name='moznetwork',
packages=['moznetwork'],
include_package_data=True,
zip_safe=False,
install_requires=deps
install_requires=deps,
entry_points={'console_scripts': [
'moznetwork = moznetwork:cli']},
)