Browse Source

Use a Python3 venv for pip packages

Stefan Schmidt 6 years ago
parent
commit
a1e9b6baa5
4 changed files with 41 additions and 28 deletions
  1. 3 0
      .gitignore
  2. 22 3
      build.sh
  3. 1 25
      resources/install_dependencies.sh
  4. 15 0
      resources/requirements.txt

+ 3 - 0
.gitignore

@@ -39,4 +39,7 @@ test_efficiency
 *.png
 *.dat
 
+# Python environment
+.venv
+
 .DS_Store

+ 22 - 3
build.sh

@@ -5,6 +5,19 @@ if [ "$1" != '--non-interactive' ]; then
     ./resources/install_dependencies.sh
 fi
 
+# Create a new venv
+rm -Rf .venv
+python3 -m venv .venv
+
+# Activate the venv
+source .venv/bin/activate
+
+# Install python packages
+pip3 install -r resources/requirements.txt
+
+# Deactivate the venv
+deactivate
+
 # Create the Makefile using cmake, from a clean build directory
 cd code_boost/src/build/
 if [ ${PWD##*/} = 'build' ]; then
@@ -65,7 +78,9 @@ fi
 SCRIPT_PATH=\${ID2T_DIR%/*}
 cd \$SCRIPT_PATH
 # Execute ID2T
+source .venv/bin/activate
 exec ./code/CLI.py "\$@"
+deactivate
 EOF
 
 # Create the test script
@@ -79,6 +94,7 @@ else
 fi
 SCRIPT_PATH=\${ID2T_DIR%/*}
 cd \$SCRIPT_PATH
+source .venv/bin/activate
 # Regenerate the statistics DB
 ./id2t -i resources/test/reference_1998.pcap -r >/dev/null
 cd code
@@ -90,11 +106,12 @@ if [ -e "Test/test_\$1.py" ]; then
     testpath="Test/test_\$1.py"
     PRINT_COV=false
 fi
-PYTHONWARNINGS="ignore" coverage3 run --source=. -m unittest \$testpath >/dev/null
+PYTHONWARNINGS="ignore" python3 -m coverage run --source=. -m unittest \$testpath >/dev/null
 if \$PRINT_COV ; then
-    coverage3 html
-    coverage3 report -m
+    python3 -m coverage html
+    python3 -m coverage report -m
 fi
+deactivate
 EOF
 
 # Create the test script
@@ -117,6 +134,7 @@ set +e
 python3 -m unittest Test/efficiency_testing.py
 error=\$?
 cd \$SCRIPT_PATH
+source .venv/bin/activate
 mkdir \$PLOT_DIR
 smbloris="SMBLorisAttack attackers.count=4 packets.per-second=8.0"
 smbscan1="SMBScanAttack ip.src=192.168.178.1 ip.dst=192.168.178.10-192.168.179.253"
@@ -137,6 +155,7 @@ for i in "\$smbloris" "\$smbscan1" "\$smbscan2" "\$ftp" "\$porto" "\$portc" "\$s
 done
 echo "\nPlotted images can be found in \"\${TEST_DIR}\"."
 echo "By executing \"mprof plot <file>.dat\" you can get a more detailed look."
+deactivate
 exit \$error
 EOF
 

+ 1 - 25
resources/install_dependencies.sh

@@ -43,7 +43,7 @@ install_pkg_arch()
 
 install_pkg_ubuntu()
 {
-    APT_PKGS="build-essential libboost-dev libboost-python-dev cmake python3-dev python3-pip sqlite tcpdump libtins-dev libpcap-dev"
+    APT_PKGS="build-essential libboost-dev libboost-python-dev cmake python3-dev python3-pip python3-venv sqlite tcpdump libtins-dev libpcap-dev"
 
     # Check first to avoid unnecessary sudo
     echo -e "Packages: Checking..."
@@ -73,27 +73,6 @@ install_pkg_darwin()
     fi
 }
 
-install_pip()
-{
-    PYTHON_MODULES="pyxdg lea numpy matplotlib scapy-python3 scipy coverage memory_profiler"
-    echo -e "Python modules: Checking..."
-
-    # Check first to avoid unnecessary sudo
-    echo $PYTHON_MODULES | xargs -n 1 pip3 show >/dev/null
-    if [ $? == 0 ]; then
-        echo -e "Python modules: Found."
-        return
-    fi
-
-    # Install all missing packages
-    echo -e "Python modules: Installing..."
-    if [ $KERNEL == 'Darwin' ]; then
-        pip3 install $PYTHON_MODULES
-    else
-        sudo pip3 install $PYTHON_MODULES
-    fi
-}
-
 # Make sure the SQLiteCpp submodule is there
 echo -e "Updating SQLiteCpp"
 git submodule update --init
@@ -110,7 +89,6 @@ if [ "$KERNEL" = 'Darwin' ]; then
     fi
 
     install_pkg_darwin
-    install_pip
     exit 0
 elif [ "$KERNEL" = 'Linux' ]; then
     # Kernel is Linux, check for supported distributions
@@ -120,12 +98,10 @@ elif [ "$KERNEL" = 'Linux' ]; then
     if [ "$OS_LIKE" = 'archlinux' ]; then
         echo -e "Detected OS: Arch Linux"
         install_pkg_arch
-        install_pip
         exit 0
     elif [ "$OS_LIKE" = 'debian' ]; then
         echo -e "Detected OS: Debian"
         install_pkg_ubuntu
-        install_pip
         exit 0
     fi
 fi

+ 15 - 0
resources/requirements.txt

@@ -0,0 +1,15 @@
+coverage==4.5.1
+cycler==0.10.0
+kiwisolver==1.0.1
+lea==2.3.5
+matplotlib==2.2.2
+memory-profiler==0.52.0
+numpy==1.14.2
+psutil==5.4.5
+pyparsing==2.2.0
+python-dateutil==2.7.2
+pytz==2018.4
+pyxdg==0.26
+scapy-python3==0.23
+scipy==1.0.1
+six==1.11.0