From 842871c6dcaf57cb2cdafd02c5766fa657f993e6 Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Fri, 3 Nov 2023 01:28:26 +0000 Subject: [PATCH] bindings: (fixes #796) Gracefully exit when bindings are unavailable --- examples/realtime/realtime-udp-echo.py | 9 +++- examples/routing/simple-routing-ping6.py | 9 +++- examples/tutorial/first.py | 9 +++- examples/tutorial/second.py | 9 +++- examples/tutorial/third.py | 9 +++- examples/wireless/mixed-wired-wireless.py | 9 +++- examples/wireless/wifi-ap.py | 9 +++- src/bridge/examples/csma-bridge.py | 39 ++++++++------- src/brite/examples/brite-generic-example.py | 9 +++- src/click/examples/nsclick-simple-lan.py | 9 +++- src/core/examples/sample-rng-plot.py | 39 ++++++++------- src/core/examples/sample-simulator.py | 48 +++++++++++-------- .../generic-battery-discharge-example.py | 9 +++- .../examples/wifi-olsr-flowmon.py | 9 +++- src/openflow/examples/openflow-switch.py | 9 +++- .../examples/tap-csma-virtual-machine.py | 9 +++- .../examples/tap-wifi-virtual-machine.py | 9 +++- .../visualizer/plugins/show_last_packets.py | 9 +++- .../plugins/wifi_intrastructure_link.py | 9 +++- utils/python-unit-tests.py | 9 +++- 20 files changed, 209 insertions(+), 70 deletions(-) diff --git a/examples/realtime/realtime-udp-echo.py b/examples/realtime/realtime-udp-echo.py index a90bb6423..9be8344d9 100644 --- a/examples/realtime/realtime-udp-echo.py +++ b/examples/realtime/realtime-udp-echo.py @@ -23,7 +23,14 @@ # - DropTail queues # - Tracing of queues and packet receptions to file "udp-echo.tr" -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(argv): # diff --git a/examples/routing/simple-routing-ping6.py b/examples/routing/simple-routing-ping6.py index f14d521d8..ba7de57f3 100644 --- a/examples/routing/simple-routing-ping6.py +++ b/examples/routing/simple-routing-ping6.py @@ -27,7 +27,14 @@ # router # -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(argv): diff --git a/examples/tutorial/first.py b/examples/tutorial/first.py index d3138bec8..a98c597d3 100644 --- a/examples/tutorial/first.py +++ b/examples/tutorial/first.py @@ -13,7 +13,14 @@ # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # */ -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) # // Default Network Topology # // diff --git a/examples/tutorial/second.py b/examples/tutorial/second.py index e3ccaec11..8bab9d413 100644 --- a/examples/tutorial/second.py +++ b/examples/tutorial/second.py @@ -16,7 +16,14 @@ # * Ported to Python by Mohit P. Tahiliani # */ -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) import sys # // Default Network Topology diff --git a/examples/tutorial/third.py b/examples/tutorial/third.py index 6fad4e55e..83520b73d 100644 --- a/examples/tutorial/third.py +++ b/examples/tutorial/third.py @@ -16,7 +16,14 @@ # * Ported to Python by Mohit P. Tahiliani # */ -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) import sys # // Default Network Topology diff --git a/examples/wireless/mixed-wired-wireless.py b/examples/wireless/mixed-wired-wireless.py index db595f52c..e8b7b1dbc 100644 --- a/examples/wireless/mixed-wired-wireless.py +++ b/examples/wireless/mixed-wired-wireless.py @@ -51,7 +51,14 @@ # +----------------+ +----------------+ # -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) # # # # This function will be used below as a trace sink diff --git a/examples/wireless/wifi-ap.py b/examples/wireless/wifi-ap.py index 65be16d4a..ef1561997 100644 --- a/examples/wireless/wifi-ap.py +++ b/examples/wireless/wifi-ap.py @@ -22,7 +22,14 @@ import sys -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) # void # DevTxTrace (std::string context, Ptr p, Mac48Address address) diff --git a/src/bridge/examples/csma-bridge.py b/src/bridge/examples/csma-bridge.py index 293a02aad..c0002cb3c 100644 --- a/src/bridge/examples/csma-bridge.py +++ b/src/bridge/examples/csma-bridge.py @@ -1,17 +1,17 @@ -# /* -# * This program is free software; you can redistribute it and/or modify -# * it under the terms of the GNU General Public License version 2 as -# * published by the Free Software Foundation -# * -# * This program is distributed in the hope that it will be useful, -# * but WITHOUT ANY WARRANTY; without even the implied warranty of -# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# * GNU General Public License for more details. -# * -# * You should have received a copy of the GNU General Public License -# * along with this program; if not, write to the Free Software -# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# */ +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# # Network topology # @@ -32,8 +32,15 @@ # \ingroup bridge # Bridge example connecting two broadcast domains. - -from ns import ns +## Import ns-3 +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(argv): diff --git a/src/brite/examples/brite-generic-example.py b/src/brite/examples/brite-generic-example.py index 24e45b963..c6f9c5262 100644 --- a/src/brite/examples/brite-generic-example.py +++ b/src/brite/examples/brite-generic-example.py @@ -18,7 +18,14 @@ # Modified by: Gabriel Ferreira # -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) ns.LogComponentEnable("BriteTopologyHelper", ns.LOG_LEVEL_ALL) diff --git a/src/click/examples/nsclick-simple-lan.py b/src/click/examples/nsclick-simple-lan.py index 3a32e62f1..83b5f40ab 100644 --- a/src/click/examples/nsclick-simple-lan.py +++ b/src/click/examples/nsclick-simple-lan.py @@ -18,7 +18,14 @@ import os.path -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) ns.LogComponentEnable("Ipv4ClickRouting", ns.LOG_LEVEL_ALL) ns.LogComponentEnable("Ipv4L3ClickProtocol", ns.LOG_LEVEL_ALL) diff --git a/src/core/examples/sample-rng-plot.py b/src/core/examples/sample-rng-plot.py index 936ec3121..5b800f934 100644 --- a/src/core/examples/sample-rng-plot.py +++ b/src/core/examples/sample-rng-plot.py @@ -1,18 +1,17 @@ -# -*- Mode:Python; -*- -# /* -# * This program is free software; you can redistribute it and/or modify -# * it under the terms of the GNU General Public License version 2 as -# * published by the Free Software Foundation -# * -# * This program is distributed in the hope that it will be useful, -# * but WITHOUT ANY WARRANTY; without even the implied warranty of -# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# * GNU General Public License for more details. -# * -# * You should have received a copy of the GNU General Public License -# * along with this program; if not, write to the Free Software -# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# */ +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# ## @file # @ingroup core-examples @@ -27,7 +26,15 @@ import numpy as np import matplotlib.pyplot as plt import sys import argparse -from ns import ns +## Import ns-3 +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(): diff --git a/src/core/examples/sample-simulator.py b/src/core/examples/sample-simulator.py index a8bb55ee7..ac8ca55bd 100755 --- a/src/core/examples/sample-simulator.py +++ b/src/core/examples/sample-simulator.py @@ -1,22 +1,21 @@ -# -*- Mode:Python; -*- -# /* -# * Copyright (c) 2010 INRIA -# * -# * This program is free software; you can redistribute it and/or modify -# * it under the terms of the GNU General Public License version 2 as -# * published by the Free Software Foundation; -# * -# * This program is distributed in the hope that it will be useful, -# * but WITHOUT ANY WARRANTY; without even the implied warranty of -# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# * GNU General Public License for more details. -# * -# * You should have received a copy of the GNU General Public License -# * along with this program; if not, write to the Free Software -# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# * -# * Authors: Mathieu Lacage -# */ +# +# Copyright (c) 2010 INRIA +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation; +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# Authors: Mathieu Lacage +# # # Python version of sample-simulator.cc @@ -25,8 +24,15 @@ # \ingroup simulator # Python example program demonstrating use of various Schedule functions. - -from ns import ns +## Import ns-3 +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) ## Example function - triggered at a random time. ## \return None. diff --git a/src/energy/examples/generic-battery-discharge-example.py b/src/energy/examples/generic-battery-discharge-example.py index a6dfde1c9..ed18534d6 100644 --- a/src/energy/examples/generic-battery-discharge-example.py +++ b/src/energy/examples/generic-battery-discharge-example.py @@ -20,7 +20,14 @@ # Demonstrates the discharge behavior of a NIMH battery discharged with a # constant current of 6.5 A (1C) -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(argv): """The main function in this Battery discharge example diff --git a/src/flow-monitor/examples/wifi-olsr-flowmon.py b/src/flow-monitor/examples/wifi-olsr-flowmon.py index 57a3160cd..84fdd56de 100644 --- a/src/flow-monitor/examples/wifi-olsr-flowmon.py +++ b/src/flow-monitor/examples/wifi-olsr-flowmon.py @@ -19,7 +19,14 @@ from __future__ import print_function import sys -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) DISTANCE = 20 # (m) NUM_NODES_SIDE = 3 diff --git a/src/openflow/examples/openflow-switch.py b/src/openflow/examples/openflow-switch.py index 7e6e77002..036f5e746 100644 --- a/src/openflow/examples/openflow-switch.py +++ b/src/openflow/examples/openflow-switch.py @@ -18,7 +18,14 @@ # Gabriel Ferreira # -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) ns.LogComponentEnable("OpenFlowInterface", ns.LOG_LEVEL_ALL) ns.LogComponentEnable("OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL) diff --git a/src/tap-bridge/examples/tap-csma-virtual-machine.py b/src/tap-bridge/examples/tap-csma-virtual-machine.py index b23de3c6a..93c6350d7 100644 --- a/src/tap-bridge/examples/tap-csma-virtual-machine.py +++ b/src/tap-bridge/examples/tap-csma-virtual-machine.py @@ -18,7 +18,14 @@ import sys -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(argv): diff --git a/src/tap-bridge/examples/tap-wifi-virtual-machine.py b/src/tap-bridge/examples/tap-wifi-virtual-machine.py index 92c6115cb..ea72e2a48 100644 --- a/src/tap-bridge/examples/tap-wifi-virtual-machine.py +++ b/src/tap-bridge/examples/tap-wifi-virtual-machine.py @@ -17,7 +17,14 @@ # import sys -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) def main(argv): diff --git a/src/visualizer/visualizer/plugins/show_last_packets.py b/src/visualizer/visualizer/plugins/show_last_packets.py index 8cb7ad3de..54bfa388c 100644 --- a/src/visualizer/visualizer/plugins/show_last_packets.py +++ b/src/visualizer/visualizer/plugins/show_last_packets.py @@ -1,7 +1,14 @@ from gi.repository import GObject from gi.repository import Gtk -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) try: from ns3.visualizer.base import InformationWindow diff --git a/src/visualizer/visualizer/plugins/wifi_intrastructure_link.py b/src/visualizer/visualizer/plugins/wifi_intrastructure_link.py index d4a7918ee..d8580548b 100644 --- a/src/visualizer/visualizer/plugins/wifi_intrastructure_link.py +++ b/src/visualizer/visualizer/plugins/wifi_intrastructure_link.py @@ -1,5 +1,12 @@ import math -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) from gi.repository import GooCanvas try: diff --git a/utils/python-unit-tests.py b/utils/python-unit-tests.py index ee67a5657..5053c91b2 100644 --- a/utils/python-unit-tests.py +++ b/utils/python-unit-tests.py @@ -19,7 +19,14 @@ # Author: Gustavo J. A. M. Carneiro import unittest -from ns import ns +try: + from ns import ns +except ModuleNotFoundError: + raise SystemExit( + "Error: ns3 Python module not found;" + " Python bindings may not be enabled" + " or your PYTHONPATH might not be properly configured" + ) import sys UINT32_MAX = 0xFFFFFFFF