Created
October 15, 2024 00:37
-
-
Save Fortyseven/f34d5b2413809b9ec95208fdf39c59cb to your computer and use it in GitHub Desktop.
Update script for Zen Browser (but can be adapted to any single file binary release on GitHub, like elfs or AppImages).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This script will download the latest binary/AppImage release of | |
$COMMAND from the GitHub releases page. You can use it for any | |
project that has just one binary file in the release. | |
It will download the file, make it executable and create a symlink | |
to it with the base name $COMMAND. | |
This is intended to keep a reliable link to the latest version of the | |
binary, so you can use it in your path, scripts or other automation | |
without juggling with version numbers. | |
You can do this by hand. But after the 10th time, welcome to | |
Automationville. | |
(This relies on a couple common Python libraries that you probably | |
already have installed. If not, `pip install` 'em.)` | |
""" | |
import os | |
from bs4 import BeautifulSoup | |
import requests | |
import urllib | |
from tqdm import tqdm | |
BASE_URL = "https://github.com/zen-browser/desktop/releases" | |
BASE_FILE = "zen-specific.AppImage" | |
COMMAND = "zen" | |
# https://stackoverflow.com/a/53877507 | |
class DownloadProgressBar(tqdm): | |
def update_to(self, b=1, bsize=1, tsize=None): | |
if tsize is not None: | |
self.total = tsize | |
self.update(b * bsize - self.n) | |
def getURLtoFile(url, output_path): | |
with DownloadProgressBar( | |
unit="B", unit_scale=True, miniters=1, desc=url.split("/")[-1] | |
) as t: | |
urllib.request.urlretrieve(url, filename=output_path, reporthook=t.update_to) | |
def main(): | |
# query the latest release page on Github | |
redirected_url = requests.get(f"{BASE_URL}/latest").url | |
print(f"- Found: {redirected_url}") | |
# extract version number end of the redirected url | |
# (e.g. github.com/ollama/ollama/releases/tag/v0.1.30 -> "v0.1.30") | |
release_version = redirected_url.split("/")[-1] | |
print(f"- Latest version: {release_version}") | |
# our new target file will be named after the release version so we can keep track | |
target_file = f"{BASE_FILE}-{release_version}" | |
# bail if we've already downloaded this version | |
if os.path.exists(target_file): | |
print(f"- Already latest, aborting: {target_file}") | |
exit(0) | |
# build the download url and pull it down | |
download_url = f"{BASE_URL}/download/{release_version}/{BASE_FILE}" | |
print(f"- Downloading: {download_url}") | |
getURLtoFile( | |
f"{BASE_URL}/download/{release_version}/{BASE_FILE}", | |
target_file, | |
) | |
print(f"- Wrote: {BASE_FILE}-{release_version}") | |
# update permissions +x | |
os.chmod(target_file, 0o755) | |
# update symlink $COMMAND to point to the version we just downloaded | |
if os.path.exists(COMMAND): | |
os.remove(COMMAND) | |
os.symlink(target_file, COMMAND) | |
print(f"- Updated symlink: {COMMAND} -> {target_file}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can probably pull line 22 (
from bs4 import BeautifulSoup
); that was from a prior version.