1313import requests
1414
1515
16- BUNDLE_DATA = "latest_bundle_data.json"
17- BUNDLE_TAG = "latest_bundle_tag.json"
16+ ADAFRUIT_BUNDLE_DATA = "latest_bundle_data.json"
17+ ADAFRUIT_BUNDLE_TAG = "latest_bundle_tag.json"
18+
19+ COMMUNITY_BUNDLE_DATA = "latest_community_bundle_data.json"
20+ COMMUNITY_BUNDLE_TAG = "latest_community_bundle_tag.json"
21+
22+ ADAFRUIT_BUNDLE_S3_URL = "https://adafruit-circuit-python.s3.amazonaws.com/bundles/adafruit/adafruit-circuitpython-bundle-{tag}.json" # pylint: disable=line-too-long
23+ COMMUNITY_BUNDLE_S3_URL = "https://adafruit-circuit-python.s3.amazonaws.com/bundles/community/circuitpython-community-bundle-{tag}.json" # pylint: disable=line-too-long
1824
1925SUBDIRECTORY_FILECOUNT_LIMIT = 10
2026
3844SHOWN_FILETYPES_EXAMPLE = [s for s in SHOWN_FILETYPES if s != "py" ]
3945
4046
41- def get_bundle (tag ):
42- """Download the given bundle's data to BUNDLE_DATA"""
43- url = f"https://adafruit-circuit-python.s3.amazonaws.com/bundles/adafruit/adafruit-circuitpython-bundle-{ tag } .json" # pylint: disable=line-too-long
44- print (f"get bundle metadata from { url } " )
45- r = requests .get (url )
46- with open (BUNDLE_DATA , "wb" ) as bundle_file :
47+ def get_bundle (bundle_url , bundle_data_file ):
48+ """Download the Adafruit and Community bundles data"""
49+ print (f"get bundle metadata from { bundle_url } " )
50+ r = requests .get (bundle_url )
51+ with open (bundle_data_file , "wb" ) as bundle_file :
4752 bundle_file .write (r .content )
4853
4954
50- LATEST_BUNDLE_VERSION = ""
51-
52-
5355def get_latest_release_from_url (url ):
5456 """
5557 Find the tag name of the latest release by using HTTP HEAD and decoding the redirect.
@@ -67,44 +69,40 @@ def get_latest_release_from_url(url):
6769 return tag
6870
6971
70- def get_latest_tag ():
72+ def get_latest_tag (repo_url ):
7173 """
7274 Find the value of the latest tag for the Adafruit CircuitPython library
7375 bundle.
7476 :return: The most recent tag value for the project.
7577 """
76- global LATEST_BUNDLE_VERSION # pylint: disable=global-statement
77- if LATEST_BUNDLE_VERSION == "" :
78- LATEST_BUNDLE_VERSION = get_latest_release_from_url (
79- "https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest"
80- )
81- return LATEST_BUNDLE_VERSION
8278
79+ return get_latest_release_from_url (repo_url )
8380
84- def ensure_latest_bundle ():
81+
82+ def ensure_latest_bundle (bundle_url , bundle_s3_url , bundle_tag_file , bundle_data_file ):
8583 """
8684 Ensure that there's a copy of the latest library bundle available so circup
8785 can check the metadata contained therein.
8886 """
8987 print ("Checking for library updates." )
90- tag = get_latest_tag ()
88+ tag = get_latest_tag (bundle_url )
9189 old_tag = "0"
92- if os .path .isfile (BUNDLE_TAG ):
93- with open (BUNDLE_TAG , encoding = "utf-8" ) as data :
90+ if os .path .isfile (bundle_tag_file ):
91+ with open (bundle_tag_file , encoding = "utf-8" ) as data :
9492 try :
9593 old_tag = json .load (data )["tag" ]
9694 except json .decoder .JSONDecodeError as _ :
9795 # Sometimes (why?) the JSON file becomes corrupt. In which case
9896 # log it and carry on as if setting up for first time.
99- print (f"Could not parse { BUNDLE_TAG :r} " )
97+ print (f"Could not parse { bundle_tag_file :r} " )
10098 if tag > old_tag :
10199 print (f"New version available { tag } ." )
102100 try :
103- get_bundle (tag )
104- with open (BUNDLE_TAG , "w" , encoding = "utf-8" ) as data :
101+ get_bundle (bundle_s3_url . replace ( "{ tag}" , tag ), bundle_data_file )
102+ with open (bundle_tag_file , "w" , encoding = "utf-8" ) as data :
105103 json .dump ({"tag" : tag }, data )
106104 except requests .exceptions .HTTPError as _ :
107- # See #20 for reason this this
105+ # See #20 for reason this
108106 print (
109107 (
110108 "There was a problem downloading the bundle. "
@@ -116,11 +114,25 @@ def ensure_latest_bundle():
116114 print (f"Current library bundle up to date { tag } " )
117115
118116
119- ensure_latest_bundle ()
117+ ensure_latest_bundle (
118+ "https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest" ,
119+ ADAFRUIT_BUNDLE_S3_URL ,
120+ ADAFRUIT_BUNDLE_TAG ,
121+ ADAFRUIT_BUNDLE_DATA ,
122+ )
123+ ensure_latest_bundle (
124+ "https://github.com/adafruit/CircuitPython_Community_Bundle/releases/latest" ,
125+ COMMUNITY_BUNDLE_S3_URL ,
126+ COMMUNITY_BUNDLE_TAG ,
127+ COMMUNITY_BUNDLE_DATA ,
128+ )
120129
121- with open ("latest_bundle_data.json" , "r" , encoding = "utf-8" ) as f :
130+ with open (ADAFRUIT_BUNDLE_DATA , "r" , encoding = "utf-8" ) as f :
122131 bundle_data = json .load (f )
123132
133+ with open (COMMUNITY_BUNDLE_DATA , "r" , encoding = "utf-8" ) as f :
134+ community_bundle_data = json .load (f )
135+
124136
125137def get_files_for_project (project_name ):
126138 """Get the set of files for a learn project"""
@@ -164,7 +176,7 @@ def get_libs_for_project(project_name):
164176
165177 for cur_import in found_imports :
166178 cur_lib = cur_import .name .split ("." )[0 ]
167- if cur_lib in bundle_data :
179+ if cur_lib in bundle_data or cur_lib in community_bundle_data :
168180 found_libs .add (cur_lib )
169181
170182 return found_libs
0 commit comments