Skip to content

Commit ed6615a

Browse files
committed
get_imports: Make it fetch the latest bundle data when run
.. and also optionally get the learn guide repo's location from the environment The bundle-data-fetching code is adapted from Circup.
1 parent 05cd77d commit ed6615a

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

python_generator/get_imports.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,102 @@
1-
import findimports
1+
import requests
22
import json
33
import os
4+
import findimports
45

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/")
610

711
SHOWN_FILETYPES = ["py", "mpy", "bmp", "pcf", "bdf", "wav", "mp3", "json", "txt"]
812

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+
9100
with open("latest_bundle_data.json", "r") as f:
10101
bundle_data = json.load(f)
11102

0 commit comments

Comments
 (0)