[tor-commits] [onionperf/master] Refactors some code to make it testable

irl at torproject.org irl at torproject.org
Tue Feb 19 09:13:51 UTC 2019


commit 66f5f7de2cf9764c80f15d9af2b3ce1a5f31b844
Author: Ana Custura <ana at nestat.org.uk>
Date:   Sat Feb 16 21:13:48 2019 +0100

    Refactors some code to make it testable
---
 onionperf/util.py | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/onionperf/util.py b/onionperf/util.py
index fbc61cb..d4aebcf 100644
--- a/onionperf/util.py
+++ b/onionperf/util.py
@@ -58,12 +58,12 @@ def find_file_paths_pairs(searchpath, patterns_a, patterns_b):
                 paths.append((paths_a, paths_b))
     return paths
 
-def find_path(binpath, defaultname):
+def find_path(binpath, defaultname, search_path=None):
     # find the path to tor
     if binpath is not None:
         binpath = os.path.abspath(os.path.expanduser(binpath))
     else:
-        w = which(defaultname)
+        w = which(defaultname, search_path)
         if w is not None:
             binpath = os.path.abspath(os.path.expanduser(w))
         else:
@@ -78,15 +78,18 @@ def find_path(binpath, defaultname):
     # we found it and it exists
     return binpath
 
-def which(program):
-    def is_exe(fpath):
-        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+def is_exe(fpath):
+    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+
+def which(program, search_path=None):
+    if search_path is None:
+        search_path = os.environ["PATH"]
     fpath, fname = os.path.split(program)
     if fpath:
         if is_exe(program):
             return program
     else:
-        for path in os.environ["PATH"].split(os.pathsep):
+        for path in search_path.split(os.pathsep):
             exe_file = os.path.join(path, program)
             if is_exe(exe_file):
                 return exe_file
@@ -110,21 +113,26 @@ def do_dates_match(date1, date2):
     else:
         return False
 
-def get_ip_address():
+def find_ip_address_url(data):
     ip_address = None
-
-    data = urllib.urlopen('https://check.torproject.org/').read()
     if data is not None and len(data) > 0:
         ip_list = re.findall(r'[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}', data)
         if ip_list is not None and len(ip_list) > 0:
             ip_address = ip_list[0]
+    return ip_address
 
+def find_ip_address_local():
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    s.connect(("8.8.8.8", 53))
+    ip_address = s.getsockname()[0]
+    s.close()
+    return ip_address
+ 
+def get_ip_address():
+    data = urllib.urlopen('https://check.torproject.org/').read()
+    ip_address = find_ip_address_url(data) 
     if ip_address is None:
-        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-        s.connect(("8.8.8.8", 53))
-        ip_address = s.getsockname()[0]
-        s.close()
-
+        ip_address = find_ip_address_local() 
     return ip_address
 
 def get_random_free_port():





More information about the tor-commits mailing list