Skip to content

Commit 38a0a65

Browse files
committed
Refactored check_network_connection() outside the AboutFile class
1 parent 4aa89a7 commit 38a0a65

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

about_code_tool/about.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ def resource_name(resource_path):
386386
return ''
387387

388388

389+
def check_network_connection():
390+
"""
391+
Returns True if an HTTP connection to the live internet is possible.
392+
"""
393+
http_connection = httplib.HTTPConnection('dejacode.org', timeout=10)
394+
try:
395+
http_connection.connect()
396+
except socket.error:
397+
return False
398+
else:
399+
return True
400+
401+
has_network_connectivity = check_network_connection()
402+
403+
389404
class AboutFile(object):
390405
"""
391406
Represent an ABOUT file and functions to parse and validate a file.
@@ -778,27 +793,13 @@ def check_url(self, url, network_check=False):
778793
return False
779794

780795
if network_check:
781-
# FIXME: we should only check network connection ONCE per run
782-
# and cache the results, not do this here
783-
if self.check_network_connection():
796+
if has_network_connectivity:
784797
# FIXME: HEAD request DO NOT WORK for ftp://
785798
return self.check_url_reachable(netloc, path)
786799
else:
787800
print('No network connection detected.')
788801
return url_has_valid_format
789802

790-
@staticmethod
791-
def check_network_connection():
792-
"""
793-
Returns True if an HTTP connection to the live internet is possible.
794-
"""
795-
try:
796-
http_connection = httplib.HTTPConnection('dejacode.org')
797-
http_connection.connect()
798-
return True
799-
except socket.error:
800-
return False
801-
802803
@staticmethod
803804
def check_url_reachable(host, path):
804805
# FIXME: we are only checking netloc and path ... NOT the whole url

about_code_tool/tests/test_about.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def test_check_url__with_network__not_starting_with_www(self):
321321
about_file = about.AboutFile()
322322
self.assertTrue(about_file.check_url("https://nexb.com", True))
323323
self.assertTrue(about_file.check_url("http://archive.apache.org/dist/httpcomponents/commons-httpclient/2.0/source/commons-httpclient-2.0-alpha2-src.tar.gz", True))
324-
if about_file.check_network_connection():
324+
if about.check_network_connection():
325325
self.assertFalse(about_file.check_url("http://nothing_here.com", True))
326326

327327
def FAILING_test_check_url__with_network__not_starting_with_www_and_spaces(self):
@@ -337,7 +337,7 @@ def test_check_url__with_network__no_schemes(self):
337337

338338
def test_check_url__with_network__not_reachable(self):
339339
about_file = about.AboutFile()
340-
if about_file.check_network_connection():
340+
if about.check_network_connection():
341341
self.assertFalse(about_file.check_url("http://www.google", True))
342342

343343
def test_check_url__with_network__empty_URL(self):

0 commit comments

Comments
 (0)