13
13
import requests
14
14
15
15
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
24
+
25
+ SUBDIRECTORY_FILECOUNT_LIMIT = 10
18
26
19
27
LEARN_GUIDE_REPO = os .environ .get (
20
28
"LEARN_GUIDE_REPO" , "../Adafruit_Learning_System_Guides/"
36
44
SHOWN_FILETYPES_EXAMPLE = [s for s in SHOWN_FILETYPES if s != "py" ]
37
45
38
46
39
- def get_bundle (tag ):
40
- """Download the given bundle's data to BUNDLE_DATA"""
41
- url = f"https://adafruit-circuit-python.s3.amazonaws.com/bundles/adafruit/adafruit-circuitpython-bundle-{ tag } .json" # pylint: disable=line-too-long
42
- print (f"get bundle metadata from { url } " )
43
- r = requests .get (url )
44
- 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 :
45
52
bundle_file .write (r .content )
46
53
47
54
48
- LATEST_BUNDLE_VERSION = ""
49
-
50
-
51
55
def get_latest_release_from_url (url ):
52
56
"""
53
57
Find the tag name of the latest release by using HTTP HEAD and decoding the redirect.
@@ -65,44 +69,40 @@ def get_latest_release_from_url(url):
65
69
return tag
66
70
67
71
68
- def get_latest_tag ():
72
+ def get_latest_tag (repo_url ):
69
73
"""
70
74
Find the value of the latest tag for the Adafruit CircuitPython library
71
75
bundle.
72
76
:return: The most recent tag value for the project.
73
77
"""
74
- global LATEST_BUNDLE_VERSION # pylint: disable=global-statement
75
- if LATEST_BUNDLE_VERSION == "" :
76
- LATEST_BUNDLE_VERSION = get_latest_release_from_url (
77
- "https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest"
78
- )
79
- return LATEST_BUNDLE_VERSION
78
+
79
+ return get_latest_release_from_url (repo_url )
80
80
81
81
82
- def ensure_latest_bundle ():
82
+ def ensure_latest_bundle (bundle_url , bundle_s3_url , bundle_tag_file , bundle_data_file ):
83
83
"""
84
84
Ensure that there's a copy of the latest library bundle available so circup
85
85
can check the metadata contained therein.
86
86
"""
87
87
print ("Checking for library updates." )
88
- tag = get_latest_tag ()
88
+ tag = get_latest_tag (bundle_url )
89
89
old_tag = "0"
90
- if os .path .isfile (BUNDLE_TAG ):
91
- 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 :
92
92
try :
93
93
old_tag = json .load (data )["tag" ]
94
94
except json .decoder .JSONDecodeError as _ :
95
95
# Sometimes (why?) the JSON file becomes corrupt. In which case
96
96
# log it and carry on as if setting up for first time.
97
- print (f"Could not parse { BUNDLE_TAG :r} " )
97
+ print (f"Could not parse { bundle_tag_file :r} " )
98
98
if tag > old_tag :
99
99
print (f"New version available { tag } ." )
100
100
try :
101
- get_bundle (tag )
102
- 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 :
103
103
json .dump ({"tag" : tag }, data )
104
104
except requests .exceptions .HTTPError as _ :
105
- # See #20 for reason this this
105
+ # See #20 for reason this
106
106
print (
107
107
(
108
108
"There was a problem downloading the bundle. "
@@ -114,11 +114,25 @@ def ensure_latest_bundle():
114
114
print (f"Current library bundle up to date { tag } " )
115
115
116
116
117
- 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
+ )
118
129
119
- with open ("latest_bundle_data.json" , "r" , encoding = "utf-8" ) as f :
130
+ with open (ADAFRUIT_BUNDLE_DATA , "r" , encoding = "utf-8" ) as f :
120
131
bundle_data = json .load (f )
121
132
133
+ with open (COMMUNITY_BUNDLE_DATA , "r" , encoding = "utf-8" ) as f :
134
+ community_bundle_data = json .load (f )
135
+
122
136
123
137
def get_files_for_project (project_name ):
124
138
"""Get the set of files for a learn project"""
@@ -141,8 +155,9 @@ def get_files_for_project(project_name):
141
155
if cur_tuple [0 ].split ("/" )[- 1 ] == _dir :
142
156
for _sub_dir in cur_tuple [1 ]:
143
157
dir_tuple = (dir_tuple [0 ], dir_tuple [1 ] + (_sub_dir ,))
144
- for _sub_file in cur_tuple [2 ]:
145
- dir_tuple = (dir_tuple [0 ], dir_tuple [1 ] + (_sub_file ,))
158
+ if len (cur_tuple [2 ]) < SUBDIRECTORY_FILECOUNT_LIMIT :
159
+ for _sub_file in cur_tuple [2 ]:
160
+ dir_tuple = (dir_tuple [0 ], dir_tuple [1 ] + (_sub_file ,))
146
161
147
162
# e.g. ("dir_name", ("file_1.txt", "file_2.txt"))
148
163
found_files .add (dir_tuple )
@@ -161,7 +176,7 @@ def get_libs_for_project(project_name):
161
176
162
177
for cur_import in found_imports :
163
178
cur_lib = cur_import .name .split ("." )[0 ]
164
- if cur_lib in bundle_data :
179
+ if cur_lib in bundle_data or cur_lib in community_bundle_data :
165
180
found_libs .add (cur_lib )
166
181
167
182
return found_libs
0 commit comments