Skip to content

Commit

Permalink
Merge pull request #43 from stampery/bug/bash-script-fails#42
Browse files Browse the repository at this point in the history
Fix urllib and urllib2 compatibility, closes #41
  • Loading branch information
elecay authored Oct 1, 2019
2 parents c03c68a + 02ce880 commit bc7d75d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions mongoaudit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import sys
try:
# For Python 3.0 and later
from urllib.request import urlopen
from urllib import parse
from urllib.request import urlopen, HTTPError, URLError, Request
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
from urllib2 import urlopen, HTTPError, URLError, Request
import pkg_resources


Expand Down Expand Up @@ -85,10 +86,15 @@ def send_result(email, result, title, urn):
'Accept': 'application/json'}
values = {'email': email, 'result': result, 'title': title, 'urn': urn, 'date': get_date()}
try:
req = urllib2.Request(url, json.dumps(values), headers)
response = urllib2.urlopen(req)
try:
req = Request(url, json.dumps(values), headers)
response = urlopen(req)
except TypeError:
# Python 3 compatibility
req = Request(url, json.dumps(values).encode('utf-8'), headers)
response = urlopen(req)
return response.read()
except (urllib2.HTTPError, urllib2.URLError) as exc:
except (HTTPError, URLError) as exc:
return "Sadly enough, we are having technical difficulties at the moment, " \
"please try again later.\n\n%s" % str(exc)

Expand Down Expand Up @@ -118,15 +124,15 @@ def check_version(version):
if getattr(sys, 'frozen', False):
try:
url = "https://api.github.com/repos/stampery/mongoaudit/releases/latest"
req = urllib2.urlopen(url)
req = urlopen(url)
releases = json.loads(req.read())
latest = releases["tag_name"]
if version < latest:
print("mongoaudit version " + version)
print("There's a new version " + latest)
_upgrade(releases)

except (urllib2.HTTPError, urllib2.URLError):
except (HTTPError, URLError):
print("Couldn't check for upgrades")
except os.error:
print("Couldn't write mongoaudit binary")
Expand Down Expand Up @@ -159,7 +165,7 @@ def _clean_upgrade(binary_ok, binary_path, path, temp_path):


def _download_binary(release, temp_path):
req = urllib2.urlopen(release["binary"])
req = urlopen(release["binary"])
binary_ok = False
attempts = 0
while not binary_ok and attempts < 3:
Expand Down Expand Up @@ -188,7 +194,7 @@ def _upgrade(releases):


def _get_md5(link, uname):
md5 = urllib2.urlopen(link).read().split("\n")
md5 = urlopen(link).read().split("\n")
for line in md5:
if uname in line:
return line.split()[0]
Expand All @@ -214,5 +220,3 @@ def get_platform():
import platform
platform_system = platform.system().lower()
return "macosx" if platform_system == "darwin" else platform_system


2 changes: 1 addition & 1 deletion mongoaudit/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.0'
__version__ = '0.1.1'

0 comments on commit bc7d75d

Please sign in to comment.