diff --git a/pacman_mirrors/http_conf.py b/pacman_mirrors/http_conf.py deleted file mode 100755 index 24de3be009289b319bd48056b6ec25f7ee422502..0000000000000000000000000000000000000000 --- a/pacman_mirrors/http_conf.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python3 - -"""Manjaro-Mirrors Web Configuration""" - -MIRRORS_URL = "http://repo.manjaro.org/new/manjaro-web-repo/mirrors.json" -STATUS_URL = "http://repo.manjaro.org/new/manjaro-web-repo/docs/status.json" -BRANCHES = ("stable", "testing", "unstable") -REPO_ARCH = "$repo/$arch" diff --git a/pacman_mirrors/http_fetcher.py b/pacman_mirrors/http_fetcher.py deleted file mode 100755 index 1ef0709040feed229ad1d345a34ddbf2ef9c3b4d..0000000000000000000000000000000000000000 --- a/pacman_mirrors/http_fetcher.py +++ /dev/null @@ -1,36 +0,0 @@ -#/usr/bin/env python3 -"""Manjaro-Mirrors HTTP Fetcher""" - -import json -import urllib.request -from collections import OrderedDict -from http_conf import MIRRORS_URL, STATUS_URL - - -class HttpFetcher(): - """Http Fetcher""" - - def get_mirror_list(self): - """Retrieve mirror list from manjaro.org""" - mirrors = list() - try: - with urllib.request.urlopen(MIRRORS_URL) as response: - mirrors = json.loads(response.read().decode( - "utf8"), object_pairs_hook=OrderedDict) - except urllib.error.URLError: - print("Error getting mirror list from server") - - return mirrors - - def get_mirrors_state(self): - """Retrieve state for all mirrors from manjaro.org""" - states = list() - try: - with urllib.request.urlopen(STATUS_URL) as response: - states = json.loads( - response.read().decode( - "utf8"), object_pairs_hook=OrderedDict) - except urllib.error.URLError: - print("Error getting mirrors state from server") - - return states diff --git a/pacman_mirrors/http_module.py b/pacman_mirrors/http_module.py new file mode 100755 index 0000000000000000000000000000000000000000..46b8b60a07d5e8fb9e00548afbd300c92fed371d --- /dev/null +++ b/pacman_mirrors/http_module.py @@ -0,0 +1,80 @@ +#/usr/bin/env python3 +"""Manjaro-Mirrors HTTP Module""" + +import json +import time +from http.client import HTTPException +from socket import timeout +from urllib.error import URLError +from urllib.request import urlopen +from collections import OrderedDict +from local_module import MIRRORS_FILE, STATES_FILE +from file_handler import FileHandler +from . import txt + +MIRRORS_URL = "http://repo.manjaro.org/new/manjaro-web-repo/mirrors.json" +STATES_URL = "http://repo.manjaro.org/new/manjaro-web-repo/docs/status.json" +BRANCHES = ("stable", "testing", "unstable") +REPO_ARCH = "$repo/$arch" + + +class HttpModule(): + """Http Module""" + + def fetch_mirrors_list(self): + """Retrieve mirror list from manjaro.org""" + mirrors = list() + try: + with urlopen(MIRRORS_URL) as response: + mirrors = json.loads(response.read().decode( + "utf8"), object_pairs_hook=OrderedDict) + except URLError: + print("Error getting mirror list from server") + + FileHandler.write_json(self, mirrors, MIRRORS_FILE) + + def fetch_mirrors_state(self): + """Retrieve state for all mirrors from manjaro.org""" + states = list() + try: + with urlopen(STATES_URL) as response: + states = json.loads( + response.read().decode( + "utf8"), object_pairs_hook=OrderedDict) + except URLError: + print("Error getting mirrors state from server") + + FileHandler.write_json(self, states, STATES_FILE) + + def ping_mirror(self, mirror_url, _timeout, _quiet): + """Get a mirrors response time + + :param mirror_url: + :return string: response time + """ + probe_start = time.time() + try: + urlopen(mirror_url, timeout=_timeout) + except URLError as err: + if hasattr(err, "reason") and not _quiet: + print("\n{}: {}: {}".format(txt.ERROR, + txt.ERR_SERVER_NOT_REACHABLE, + err.reason)) + elif hasattr(err, "code") and not _quiet: + print("\n{}: {}: {}".format(txt.ERROR, + txt.ERR_SERVER_REQUEST, + err.errno)) + except timeout: + if not _quiet: + print("\n{}: {}: {}".format(txt.ERROR, + txt.ERR_SERVER_NOT_AVAILABLE, + txt.TIMEOUT)) + except HTTPException: + if not _quiet: + print("\n{}: {}: {}".format(txt.ERROR, + txt.ERR_SERVER_HTTP_EXCEPTION, + txt.HTTP_EXCEPTION)) + probe_stop = time.time() + probe_time = round((probe_stop - probe_start), 3) + probe_time = format(probe_time, ".3f") + return str(probe_time) diff --git a/pacman_mirrors/local_module.py b/pacman_mirrors/local_module.py new file mode 100755 index 0000000000000000000000000000000000000000..ea0877b78b181e2d17004db6d362fba54b1b85ad --- /dev/null +++ b/pacman_mirrors/local_module.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +"""Manjaro-Mirrors Local Module""" + +import json +from collections import OrderedDict + +MIRRORS_FILE = "/etc/pacman.d/mirrors/mirrors.json" +STATES_FILE = "/etc/pacman.d/mirrors/states.json" + + +class LocalModule(): + """FileHandler class""" + + def write_json(self, data, filename): + """ + Writes a named json file + + :param data: + :param filename: + """ + try: + with open(filename, "w") as outfile: + json.dump(data, outfile) + return True + + except OSError: + return False + + def read_json(self, filename): + """ + Reads a named json file + + :param filename: + :return data: OrderedDict + """ + result = list() + try: + with open(filename, "r") as infile: + result = json.loads(infile.read().decode( + "utf8"), object_pairs_hook=OrderedDict) + except OSError: + return False + + return result diff --git a/pacman_mirrors/pacman_mirrors.py b/pacman_mirrors/pacman_mirrors.py index af1667d216e2d5c6d0ad4ae6628c7fe367c0f1b1..ff4cc27265e29942f19a8cab9f9a76eb35dd685e 100755 --- a/pacman_mirrors/pacman_mirrors.py +++ b/pacman_mirrors/pacman_mirrors.py @@ -718,7 +718,7 @@ class PacmanMirrors: """Run""" self.config = self.config_init() self.command_line_parse() - self.load_server_lists() + self.load_server_lists() if self.interactive: self.gen_mirror_list_interactive() else: