From 51875eb9b7f8e33754b8aac353e6322a84eac546 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Tue, 31 May 2022 12:49:00 +0300 Subject: [PATCH 1/3] Improved getting interface and network by making it more correct and getting rid of sed scripting. --- aioarping/__main__.py | 15 +++++---------- aioarping/utils.py | 39 +++++++++++++++++++++++++++++++++++++++ setup.py | 6 +++++- 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 aioarping/utils.py diff --git a/aioarping/__main__.py b/aioarping/__main__.py index 366628b..e9609aa 100644 --- a/aioarping/__main__.py +++ b/aioarping/__main__.py @@ -23,7 +23,10 @@ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE -import subprocess, asyncio, aioarping, ipaddress +import asyncio, aioarping, ipaddress + + +from .utils import get_interface_and_network def my_process(data): @@ -33,15 +36,7 @@ def my_process(data): event_loop = asyncio.get_event_loop() -mydomain = [ - x - for x in subprocess.getoutput( - "ip route|sed '/via/d' |sed '/docker/d'|sed '/linkdown/d'|sed '/src /!d' | sed '/dev /!d' |sed '2,$d'" - ).split(" ") - if x -] -myiface = mydomain[2] -mydomain = mydomain[0] +myiface, mydomain = get_interface_and_network() # First create and configure a raw socket mysocket = aioarping.create_raw_socket(myiface) diff --git a/aioarping/utils.py b/aioarping/utils.py new file mode 100644 index 0000000..46939e6 --- /dev/null +++ b/aioarping/utils.py @@ -0,0 +1,39 @@ +def prefilter_route_lines(lines): + """https://github.com/brandonmpace/routeparser/issues/2""" + + for el in lines: + if "linkdown" in el: + continue + if "src" not in el: + continue + yield el + + +def post_filter_routes(routes): + for r in routes: + if "docker" in r.interface: + continue + if r.gateway is not None: + continue + yield r + + +def get_ip_route_lines(): + import subprocess + + return subprocess.getoutput("/usr/sbin/ip route").splitlines() + + +def get_needed_routes(): + import routeparser + + routeLines = list(prefilter_route_lines(get_ip_route_lines())) + t = routeparser.RoutingTable.from_ip_route_lines(routeLines) + return post_filter_routes(t.routes) + + +def get_interface_and_network(): + r = next(iter(get_needed_routes())) + ifc = r.interface + domain = r.network + return ifc, domain diff --git a/setup.py b/setup.py index a616919..b412f7d 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,11 @@ download_url="http://github.com/frawau/aiolifx/archive/aioarping/0.1.3.tar.gz", keywords=["arp", "mac address", "presence", "automation"], license="MIT", - install_requires=["ipaddress",], + install_requires=["ipaddress"], + extras_require = { + "utils": ["routeparser"], + "cli": ["aioarping[utils]"], + }, # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ "License :: OSI Approved :: MIT License", From 9397544525f629d65bd77f0398b02359c51aec3a Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Thu, 9 Jun 2022 07:44:19 +0300 Subject: [PATCH 2/3] Moved the metadata into `setup.cfg`. Added `pyproject.toml`. Version is now fetched via `setuptools_scm`. --- pyproject.toml | 5 +++++ setup.cfg | 22 +++++++++++++++++++++- setup.py | 29 ----------------------------- 3 files changed, 26 insertions(+), 30 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.py diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..285405f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,5 @@ +[build-system] +requires = ["setuptools>=42.2", "wheel", "setuptools_scm[toml]>=3.4.3"] +build-backend = "setuptools.build_meta" + +[tool.setuptools_scm] diff --git a/setup.cfg b/setup.cfg index 224a779..2706713 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,22 @@ [metadata] -description-file = README.md \ No newline at end of file +name = aioarping +author = François Wautier +author_email = francois@wautier.eu +license = MIT +description = API for arping over a LAN with asyncio. +long_description = file: README.md +keywords = arp, mac address, presence, automation +url = http://github.com/frawau/aioarping +classifiers = + License :: OSI Approved :: MIT License + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.5 + +[options] +packages = aioarping +install_requires = ipaddress + +[options.extras_require] +utils = routeparser +cli = aioarping[utils] + diff --git a/setup.py b/setup.py deleted file mode 100644 index b412f7d..0000000 --- a/setup.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding:utf-8 -*- -from distutils.core import setup - -setup( - name="aioarping", - packages=["aioarping"], - version="0.1.3", - author="François Wautier", - author_email="francois@wautier.eu", - description="API for arping over a LAN with asyncio.", - url="http://github.com/frawau/aioarping", - download_url="http://github.com/frawau/aiolifx/archive/aioarping/0.1.3.tar.gz", - keywords=["arp", "mac address", "presence", "automation"], - license="MIT", - install_requires=["ipaddress"], - extras_require = { - "utils": ["routeparser"], - "cli": ["aioarping[utils]"], - }, - # See https://pypi.python.org/pypi?%3Aaction=list_classifiers - classifiers=[ - "License :: OSI Approved :: MIT License", - # Specify the Python versions you support here. In particular, ensure - # that you indicate whether you support Python 2, Python 3 or both. - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.5", - ], -) From c7955afa765607548c10141426867b94557abbdb Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Thu, 9 Jun 2022 07:52:03 +0300 Subject: [PATCH 3/3] Moved the metadata into `PEP 621`-compliant `pyproject.toml`. --- pyproject.toml | 25 ++++++++++++++++++++++++- setup.cfg | 22 ---------------------- 2 files changed, 24 insertions(+), 23 deletions(-) delete mode 100644 setup.cfg diff --git a/pyproject.toml b/pyproject.toml index 285405f..7452d64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,28 @@ [build-system] -requires = ["setuptools>=42.2", "wheel", "setuptools_scm[toml]>=3.4.3"] +requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"] build-backend = "setuptools.build_meta" +[project] +name = "aioarping" +authors = [{name = "François Wautier", email = "francois@wautier.eu"}] +license = {text = "MIT"} +description = "API for arping over a LAN with asyncio." +readme = "README.md" +keywords = ["arp", "mac address", "presence", "automation"] +classifiers = [ + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.5", +] +urls = {Homepage = "http://github.com/frawau/aioarping"} +dependencies = ["ipaddress"] +dynamic = ["version"] + +[project.optional-dependencies] +utils = ["routeparser"] +cli = ["aioarping[utils]"] + +[tool.setuptools] +packages = ["aioarping"] + [tool.setuptools_scm] diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 2706713..0000000 --- a/setup.cfg +++ /dev/null @@ -1,22 +0,0 @@ -[metadata] -name = aioarping -author = François Wautier -author_email = francois@wautier.eu -license = MIT -description = API for arping over a LAN with asyncio. -long_description = file: README.md -keywords = arp, mac address, presence, automation -url = http://github.com/frawau/aioarping -classifiers = - License :: OSI Approved :: MIT License - Programming Language :: Python :: 3 - Programming Language :: Python :: 3.5 - -[options] -packages = aioarping -install_requires = ipaddress - -[options.extras_require] -utils = routeparser -cli = aioarping[utils] -