Commits

Jason R. Coombs committed febf68e

Updated setup.py so it will automatically download the WPDPack library to build the extension

Comments (0)

Files changed (1)

 # $Id: setup.py,v 1.11 2007/03/27 17:26:13 max Exp $
 
 from distutils.core import setup, Extension
-import sys, os
+import os
+import contextlib
+import zipfile
+import io
+import urllib2
+import shutil
+import platform
 
 PACKAGE_NAME = 'pcapy'
 
-# You might want to change these to reflect your specific configuration
-include_dirs = []
-library_dirs = []
-libraries = []
+@contextlib.contextmanager
+def Windows_context(extension):
+    """
+    Install with wpdpack
+    """
+    source_url = 'http://www.winpcap.org/archive/4.1.1-WpdPack.zip'
+    dest_fn = 'WpdPack'
+    if not os.path.exists(dest_fn):
+        print("Loading WPDPack from " + source_url)
+        stream = io.BytesIO(urllib2.urlopen(source_url).read())
+        zf = zipfile.ZipFile(stream)
+        zf.extractall()
+    extension.include_dirs.append(r'{dest_fn}\Include'.format(**vars()))
+    library_dir = os.path.join(dest_fn, 'Lib')
+    if platform.machine() == 'AMD64':
+        library_dir = os.path.join(library_dir, 'x64')
+    extension.library_dirs.append(library_dir)
+    extension.libraries.extend(['wpcap', 'packet', 'ws2_32'])
+    extension.sources.append(os.path.join('win32', 'dllmain.cc'))
+    extension.define_macros.append(('WIN32', '1'))
+    try:
+        yield
+    finally:
+        shutil.rmtree(dest_fn)
 
-if sys.platform =='win32':
-    # WinPcap include files
-    include_dirs.append(r'c:\devel\wpdpack\Include')
-    # WinPcap library files
-    library_dirs.append(r'c:\devel\wpdpack\Lib')
-    libraries = ['wpcap', 'packet', 'ws2_32']
-else:
-    libraries = ['pcap']
+@contextlib.contextmanager
+def pcap_context(extension):
+    extension.libraries.append('pcap')
+    yield
 
-
-# end of user configurable parameters
-macros = []
-sources = ['pcapdumper.cc',
-           'bpfobj.cc',
-           'pcapy.cc',
-           'pcapobj.cc',
-           'pcap_pkthdr.cc',
-           ]
-
-if sys.platform == 'win32':
-    sources.append(os.path.join('win32', 'dllmain.cc'))
-    macros.append(('WIN32', '1'))
+context = globals().get(platform.system() + '_context', pcap_context)
 
 # HACK replace linker gcc with g++
 from distutils import sysconfig
 save_init_posix = sysconfig._init_posix
 def my_init_posix():
-	save_init_posix()
-	g = sysconfig._config_vars
-	if g['LDSHARED'][:3]=='gcc':
-		print 'my_init_posix: changing LDSHARED =',`g['LDSHARED']`,
-		g['LDSHARED'] = 'g++'+g['LDSHARED'][3:]
-		print 'to',`g['LDSHARED']`
+    save_init_posix()
+    g = sysconfig._config_vars
+    if g['LDSHARED'][:3] == 'gcc':
+        print 'my_init_posix: changing LDSHARED =', repr(g['LDSHARED']),
+        g['LDSHARED'] = 'g++' + g['LDSHARED'][3:]
+        print 'to', repr(g['LDSHARED'])
 sysconfig._init_posix = my_init_posix
 
-setup(name = PACKAGE_NAME,
-      version = "0.10.5",
-      url = "http://oss.coresecurity.com/projects/pcapy.html",
-      author = "Maximiliano Caceres",
-      author_email = "max@coresecurity.com",
-      maintainer = "Maximiliano Caceres",
-      maintainer_email = "max@coresecurity.com",
-      description = "Python pcap extension",
-      ext_modules = [Extension(
-          name = PACKAGE_NAME,
-          sources = sources,
-          define_macros = macros,
-          include_dirs = include_dirs,
-          library_dirs = library_dirs,
-          libraries = libraries)],
-      data_files = [(os.path.join('share', 'doc', PACKAGE_NAME),
-                     ['README', 'LICENSE', 'pcapy.html'])],
-      )
+pcap_extension = Extension(
+    name = PACKAGE_NAME,
+    sources = [
+        'pcapdumper.cc',
+        'bpfobj.cc',
+        'pcapy.cc',
+        'pcapobj.cc',
+        'pcap_pkthdr.cc',
+    ],
+)
 
+setup_params = dict(
+    name = PACKAGE_NAME,
+    version = "0.10.5",
+    url = "http://oss.coresecurity.com/projects/pcapy.html",
+    author = "Maximiliano Caceres",
+    author_email = "max@coresecurity.com",
+    maintainer = "Maximiliano Caceres",
+    maintainer_email = "max@coresecurity.com",
+    description = "Python pcap extension",
+    ext_modules = [pcap_extension],
+    data_files = [
+        (
+            os.path.join('share', 'doc', PACKAGE_NAME),
+            ['README', 'LICENSE', 'pcapy.html']
+        )
+    ],
+)
+
+with context(pcap_extension):
+    setup(**setup_params)