commit b32d6fd187e78539e2f9c4415f478a2da80d7a0f
Author: Arturo Filastò <art(a)fuffa.org>
Date: Wed Feb 27 15:57:35 2013 +0100
Refactor logic for importing scapy related functions
* Make scapy related requirements less strict
* Always print out a useful error message when something goes wrong
(cherry picked from commit 77b2f6e7206f5a1bb1531fb2416eaf36675b098b)
---
ooni/utils/net.py | 2 +-
ooni/utils/txscapy.py | 54 ++++++++++++++++++++++++++++++++++++------------
2 files changed, 41 insertions(+), 15 deletions(-)
diff --git a/ooni/utils/net.py b/ooni/utils/net.py
index 49f9998..67a98c2 100644
--- a/ooni/utils/net.py
+++ b/ooni/utils/net.py
@@ -7,7 +7,7 @@ from twisted.internet import protocol, defer
from twisted.internet import threads, reactor
from twisted.web.iweb import IBodyProducer
-from ooni.utils import log, txscapy
+from ooni.utils import log
#if sys.platform.system() == 'Windows':
# import _winreg as winreg
diff --git a/ooni/utils/txscapy.py b/ooni/utils/txscapy.py
index 0e805e7..84e69a6 100644
--- a/ooni/utils/txscapy.py
+++ b/ooni/utils/txscapy.py
@@ -14,24 +14,50 @@ from scapy.config import conf
from ooni.utils import log
from ooni import config
-try:
- conf.use_pcap = True
- conf.use_dnet = True
+class LibraryNotInstalledError(Exception):
+ pass
- from scapy.all import PcapWriter
- from scapy.arch import pcapdnet
+def pcapdnet_installed():
+ """
+ Checks to see if libdnet or libpcap are installed and set the according
+ variables.
- config.pcap_dnet = True
- from scapy.all import Gen, SetGen, MTU
+ Returns:
-except ImportError, e:
- log.err("pypcap or dnet not installed. "
- "Certain tests may not work.")
+ True
+ if pypcap and libdnet are installed
- config.pcap_dnet = False
- conf.use_pcap = False
- conf.use_dnet = False
+ False
+ if one of the two is absent
+ """
+ try:
+ conf.use_pcap = True
+ conf.use_dnet = True
+ from scapy.arch import pcapdnet
+
+ config.pcap_dnet = True
+
+ except ImportError:
+ log.err("pypcap or dnet not installed. "
+ "Certain tests may not work.")
+
+ config.pcap_dnet = False
+ conf.use_pcap = False
+ conf.use_dnet = False
+
+ # This is required for unix systems that are different than linux (OSX for
+ # example) since scapy explicitly wants pcap and libdnet installed for it
+ # to work.
+ try:
+ from scapy.arch import pcapdnet
+ except ImportError:
+ log.err("Your platform requires to having libdnet and libpcap installed.")
+ raise LibraryNotInstalledError
+
+if pcapdnet_installed():
+ from scapy.all import PcapWriter
+else:
class DummyPcapWriter:
def __init__(self, pcap_filename, *arg, **kw):
log.err("Initializing DummyPcapWriter. We will not actually write to a pcapfile")
@@ -41,7 +67,7 @@ except ImportError, e:
PcapWriter = DummyPcapWriter
- from scapy.all import Gen, SetGen, MTU
+from scapy.all import Gen, SetGen, MTU
def getNetworksFromRoutes():
""" Return a list of networks from the routing table """