Skip to content

Commit

Permalink
Multiple tries the get repository
Browse files Browse the repository at this point in the history
* Also try a alternative (mirrored) repository when contacting the default one fails for some reason
  • Loading branch information
ueffel committed Mar 24, 2019
1 parent 2881944 commit 47d6b58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ All commands are prefixed with `PackageControl:`.
* Open the `Keypirinha: Console`
* Enter the following:
```python
import keypirinha as kp,keypirinha_net as kpn,os;p="PackageControl.keypirinha-package";d=kpn.build_urllib_opener().open("https://github.com/ueffel/Keypirinha-PackageControl/releases/download/0.3/"+p);pb=d.read();d.close();f=open(os.path.join(kp.installed_package_dir(),p),"wb");f.write(pb);f.close()
import keypirinha as kp,keypirinha_net as kpn,os;p="PackageControl.keypirinha-package";d=kpn.build_urllib_opener().open("https://github.com/ueffel/Keypirinha-PackageControl/releases/download/1.0.0/"+p);pb=d.read();d.close();f=open(os.path.join(kp.installed_package_dir(),p),"wb");f.write(pb);f.close()
```

### Manually
Expand Down
10 changes: 9 additions & 1 deletion packagecontrol.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[main]
# Decides which packages are available to install
# Decides which package repository to use
# and in turn which packages are available to install
#
# (if you change this you may also wanna change the
# "alternative_repository" setting)
# Default: https://ue.spdns.de/packagecontrol/packages.json
#repository = https://ue.spdns.de/packagecontrol/packages.json

# Mirror of the repository in case the normal repository is not available
# Default: https://ueffel.pythonanywhere.com/packages.json
#alternative_repository = https://ueffel.pythonanywhere.com/packages.json

# List of the installed packages
# this list is automatically updated, no need to add anything here in the file directly
# installed packages are checked on startup, if anything isn't present, it will be installed
Expand Down
46 changes: 37 additions & 9 deletions packagecontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PackageControl(kp.Plugin):
"""Package that provides a means to install, update and remove keypirinha packages
"""
DEFAULT_REPO = "https://ue.spdns.de/packagecontrol/packages.json"
DEFAULT_ALT_REPO = "https://ueffel.pythonanywhere.com/packages.json"
DEFAULT_AUTOUPDATE = True
DEFAULT_UPDATE_INTERVAL = 12
COMMAND_INSTALL = "install"
Expand All @@ -36,6 +37,7 @@ def __init__(self):
self._untracked_packages = []
self._available_packages = []
self._repo_url = self.DEFAULT_REPO
self._alt_repo_url = self.DEFAULT_ALT_REPO
self._autoupdate = self.DEFAULT_AUTOUPDATE
self._update_interval = self.DEFAULT_UPDATE_INTERVAL
self._urlopener = kpn.build_urllib_opener(extra_handlers=[RedirectorHandler()])
Expand All @@ -54,7 +56,7 @@ def on_events(self, flags):

if flags & kp.Events.NETOPTIONS:
self.dbg("Network settings changed: rebuilding urlopener")
self._urlopener = kpn.build_urllib_opener()
self._urlopener = kpn.build_urllib_opener(extra_handlers=[RedirectorHandler()])

def on_start(self):
"""Reads config, checks packages and installs missing packages
Expand Down Expand Up @@ -284,6 +286,10 @@ def _read_config(self):
old_repo_url = self._repo_url
self._repo_url = settings.get("repository", "main", self.DEFAULT_REPO)
self.dbg("repo_url:", self._repo_url)

self._alt_repo_url = settings.get("alternative_repository", "main", self.DEFAULT_ALT_REPO)
self.dbg("alt_repo_url:", self._alt_repo_url)

if old_repo_url != self._repo_url:
self._get_available_packages(True)

Expand All @@ -310,6 +316,10 @@ def _save_settings(self):

if "repository" in config["main"] and config["main"]["repository"] != self._repo_url:
config["main"]["repository"] = self._repo_url

if "alternative_repository" in config["main"] and config["main"]["alternative_repository"] != self._alt_repo_url:
config["main"]["alternative_repository"] = self._alt_repo_url

config["main"]["installed_packages"] = "\n{}".format("\n".join(self._installed_packages))

with open(save_path, "w") as ini_file:
Expand Down Expand Up @@ -399,14 +409,32 @@ def _get_available_packages(self, force=False):
len(repo["packages"])))

if force or not repo:
self.dbg("No available packages cached or its time to update, getting list from", self._repo_url)
req = urllib.request.Request(self._repo_url)
with self._urlopener.open(req) as response:
repo = json.loads(response.read().decode())
if hasattr(req, "redirect"):
self.info("Request permanently redirected. Changing repository url to:", req.redirect)
self._repo_url = req.redirect
self._save_settings()
self.dbg("No available packages cached or its time to update, getting list from the net")
tries = 4
repos = [self._repo_url, self._alt_repo_url]
while tries > 0:
try:
repo = repos[tries % 2]
self.dbg("Try to get list from", repo)
req = urllib.request.Request(repo)
with self._urlopener.open(req) as response:
repo = json.loads(response.read().decode())
tries = 0
if hasattr(req, "redirect"):
self.info("Request permanently redirected. Changing repository url to:", req.redirect)
if tries % 2 == 0:
self._repo_url = req.redirect
else:
self._alt_repo_url = req.redirect
self._save_settings()
except Exception as ex:
tries -= 1
if tries > 0:
self.dbg("Error while obtaining the packages trying again...:", traceback.format_exc())
self.err("Error while obtaining the packages trying again...")
else:
raise ex

write_cache = True
self.info("Package list loaded from '{}' ({} packages)".format(repo["name"], len(repo["packages"])))

Expand Down

0 comments on commit 47d6b58

Please sign in to comment.