Skip to content

Commit

Permalink
Merge pull request #55 from jblakeman/release-1.0.3
Browse files Browse the repository at this point in the history
Release 1.0.3
  • Loading branch information
jblakeman committed Dec 28, 2016
2 parents e609ed2 + 84f4a21 commit e4c5d59
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 37 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ Dependencies
------------

Python HTML parser, `BeautifulSoup <https://www.crummy.com/software/BeautifulSoup/>`_.

HTTP Requests library, `requests <http://docs.python-requests.org/en/master/>`_.
2 changes: 1 addition & 1 deletion apt_select/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.2'
__version__ = '1.0.3'
19 changes: 10 additions & 9 deletions bin/apt-select → apt_select/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#!/usr/bin/env python
"""Main apt-select script"""

from sys import exit, stderr, version_info
from os import getcwd
from apt_select.arguments import get_args
from apt_select.utils import get_html, URLGetError
from apt_select.utils import get_text, URLGetTextError
from apt_select.mirrors import Mirrors
from apt_select.apt_system import AptSources, SourcesFileError

# Support input for Python 2 and 3
get_input = input
if version_info[:2] <= (2, 7):
get_input = raw_input


def set_args():
"""Set arguments, disallow bad combination"""
Expand All @@ -32,9 +38,9 @@ def get_mirrors(mirrors_url):
"""Fetch list of Ubuntu mirrors"""
stderr.write("Getting list of mirrors...")
try:
mirrors_list = get_html(mirrors_url)
except URLGetError as err:
exit("Error getting list from %s:\n\t%s" % (mirrors_list, err))
mirrors_list = get_text(mirrors_url)
except URLGetTextError as err:
exit("Error getting list from %s:\n\t%s" % (mirrors_url, err))
stderr.write("done.\n")

return mirrors_list.splitlines()
Expand Down Expand Up @@ -216,9 +222,4 @@ def main():
stderr.write("Aborting...\n")

if __name__ == '__main__':
# Support input for both Python 2 and 3
get_input = input
if version_info[:2] <= (2, 7):
get_input = raw_input

main()
10 changes: 5 additions & 5 deletions apt_select/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from socket import (socket, AF_INET, SOCK_STREAM,
gethostbyname, error, timeout, gaierror)
from time import time
from apt_select.utils import get_html, URLGetError, progress_msg
from apt_select.utils import progress_msg, get_text, URLGetTextError
from apt_select.apt_system import AptSystem
try:
from urlparse import urlparse
Expand Down Expand Up @@ -73,8 +73,8 @@ def get_launchpad_urls(self):
"""Obtain mirrors' corresponding launchpad URLs"""
stderr.write("Getting list of launchpad URLs...")
try:
self._launchpad_html = get_html(self._launchpad_url)
except URLGetError as err:
self._launchpad_html = get_text(self._launchpad_url)
except URLGetTextError as err:
stderr.write((
"%s: %s\nUnable to retrieve list of launchpad sites\n"
"Reverting to latency only" % (self._launchpad_url, err)
Expand Down Expand Up @@ -302,8 +302,8 @@ def get_info(self):
Launchpad API doesn't support access to archivemirror statuses."""

try:
launch_html = get_html(self._launch_url)
except URLGetError as err:
launch_html = get_text(self._launch_url)
except URLGetTextError as err:
stderr.write("connection to %s: %s\n" % (self._launch_url, err))
self._data_queue.put_nowait((self._url, None))
else:
Expand Down
32 changes: 12 additions & 20 deletions apt_select/utils.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
#!/usr/bin/env python
"""Collection of module netural utility functions"""
"""Collection of module neutral utility functions"""

from sys import stderr
from ssl import SSLError
from socket import timeout
try:
from urllib.request import urlopen, HTTPError, URLError
except ImportError:
from urllib2 import urlopen, HTTPError, URLError
import requests


def utf8_decode(encoded):
return encoded.decode('utf-8')

class URLGetError(Exception):
"""Error class for retreiving and reading content from remote URL"""
pass

class URLGetTextError(Exception):
"""Error class for fetching text from a URL"""
pass

def get_html(url):
"""Retrieve and read HTML from URL"""
try:
html = urlopen(url)
except (HTTPError, URLError, SSLError, timeout) as err:
raise URLGetError(err)

def get_text(url):
"""Return text from GET request response content"""
try:
html = html.read()
except (SSLError, IOError, OSError) as err:
raise URLGetError(err)
text = requests.get(url).text
except requests.HTTPError as err:
raise URLGetTextError(err)

return utf8_decode(html)
return text


def progress_msg(processed, total):
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
],
keywords='latency status rank reporting apt configuration',
packages=find_packages(exclude=['tests']),
install_requires=['beautifulsoup4'],
scripts=['bin/apt-select']
install_requires=['requests', 'beautifulsoup4'],
entry_points = {
'console_scripts': [
'apt-select = apt_select.__main__:main'
]
}
)

0 comments on commit e4c5d59

Please sign in to comment.