|
1 |
| -import findimports |
| 1 | +import requests |
2 | 2 | import json
|
3 | 3 | import os
|
| 4 | +import findimports |
4 | 5 |
|
5 |
| -LEARN_GUIDE_REPO = "../../Adafruit_Learning_System_Guides/" |
| 6 | +BUNDLE_DATA = "latest_bundle_data.json" |
| 7 | +BUNDLE_TAG = "latest_bundle_tag.json" |
| 8 | + |
| 9 | +LEARN_GUIDE_REPO = os.environ.get('LEARN_GUIDE_REPO', "../../Adafruit_Learning_System_Guides/") |
6 | 10 |
|
7 | 11 | SHOWN_FILETYPES = ["py", "mpy", "bmp", "pcf", "bdf", "wav", "mp3", "json", "txt"]
|
8 | 12 |
|
| 13 | +def get_bundle(tag): |
| 14 | + url = f"https://adafruit-circuit-python.s3.amazonaws.com/bundles/adafruit/adafruit-circuitpython-bundle-{tag}.json" |
| 15 | + print("get bundle metadata from {url}") |
| 16 | + r = requests.get(url) |
| 17 | + with open(BUNDLE_DATA, "wb") as f: |
| 18 | + f.write(r.content) |
| 19 | + |
| 20 | +LATEST_BUNDLE_VERSION = "" |
| 21 | +def get_latest_tag(): |
| 22 | + """ |
| 23 | + Find the value of the latest tag for the Adafruit CircuitPython library |
| 24 | + bundle. |
| 25 | + :return: The most recent tag value for the project. |
| 26 | + """ |
| 27 | + global LATEST_BUNDLE_VERSION |
| 28 | + if not LATEST_BUNDLE_VERSION: |
| 29 | + LATEST_BUNDLE_VERSION = get_latest_release_from_url( |
| 30 | + "https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest" |
| 31 | + ) |
| 32 | + return LATEST_BUNDLE_VERSION |
| 33 | + |
| 34 | +def get_latest_release_from_url(url): |
| 35 | + """ |
| 36 | + Find the tag name of the latest release by using HTTP HEAD and decoding the redirect. |
| 37 | +
|
| 38 | + :return: The most recent tag value for the release. |
| 39 | + """ |
| 40 | + |
| 41 | + print(f"Requesting redirect information: {url}") |
| 42 | + response = requests.head(url) |
| 43 | + responseurl = response.url |
| 44 | + if response.is_redirect: |
| 45 | + responseurl = response.headers["Location"] |
| 46 | + tag = responseurl.rsplit("/", 1)[-1] |
| 47 | + print(f"Tag: {tag!r}") |
| 48 | + return tag |
| 49 | + |
| 50 | + |
| 51 | +def get_latest_tag(): |
| 52 | + """ |
| 53 | + Find the value of the latest tag for the Adafruit CircuitPython library |
| 54 | + bundle. |
| 55 | + :return: The most recent tag value for the project. |
| 56 | + """ |
| 57 | + global LATEST_BUNDLE_VERSION |
| 58 | + if LATEST_BUNDLE_VERSION == "": |
| 59 | + LATEST_BUNDLE_VERSION = get_latest_release_from_url( |
| 60 | + "https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest" |
| 61 | + ) |
| 62 | + return LATEST_BUNDLE_VERSION |
| 63 | + |
| 64 | +def ensure_latest_bundle(): |
| 65 | + """ |
| 66 | + Ensure that there's a copy of the latest library bundle available so circup |
| 67 | + can check the metadata contained therein. |
| 68 | + """ |
| 69 | + print("Checking for library updates.") |
| 70 | + tag = get_latest_tag() |
| 71 | + old_tag = "0" |
| 72 | + if os.path.isfile(BUNDLE_TAG): |
| 73 | + with open(BUNDLE_TAG, encoding="utf-8") as data: |
| 74 | + try: |
| 75 | + old_tag = json.load(data)["tag"] |
| 76 | + except json.decoder.JSONDecodeError as ex: |
| 77 | + # Sometimes (why?) the JSON file becomes corrupt. In which case |
| 78 | + # log it and carry on as if setting up for first time. |
| 79 | + print(f"Could not parse {BUNDLE_TAG:r}") |
| 80 | + if tag > old_tag: |
| 81 | + print(f"New version available {tag}.") |
| 82 | + try: |
| 83 | + get_bundle(tag) |
| 84 | + with open(BUNDLE_TAG, "w", encoding="utf-8") as data: |
| 85 | + json.dump({"tag": tag}, data) |
| 86 | + except requests.exceptions.HTTPError as ex: |
| 87 | + # See #20 for reason this this |
| 88 | + print( |
| 89 | + ( |
| 90 | + "There was a problem downloading the bundle. " |
| 91 | + "Please try again in a moment." |
| 92 | + ), |
| 93 | + ) |
| 94 | + raise |
| 95 | + else: |
| 96 | + print(f"Current library bundle up to date {tag}") |
| 97 | + |
| 98 | +ensure_latest_bundle() |
| 99 | + |
9 | 100 | with open("latest_bundle_data.json", "r") as f:
|
10 | 101 | bundle_data = json.load(f)
|
11 | 102 |
|
|
0 commit comments