Skip to content

Commit 40d755c

Browse files
author
Adafruit Adabot
committed
refactor arduino libraries to use requests instead of github api, remove some duplication, work with short results, etc etc
1 parent 62c4c36 commit 40d755c

File tree

1 file changed

+64
-69
lines changed

1 file changed

+64
-69
lines changed

adabot/arduino_libraries.py

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,27 @@
3939
verbosity = 1
4040
file_data = []
4141

42+
all_libraries = []
43+
4244
def list_repos():
4345
""" Return a list of all Adafruit repositories with 'Arduino' in either the
4446
name, description, or readme. Each list item is a dictionary of GitHub API
4547
repository state.
4648
"""
4749
repos = []
4850
result = github.get("/search/repositories",
49-
params={"q":"Arduino in:name in:description in:readme fork:true user:adafruit archived:false",
51+
params={"q":"Arduino in:name in:description in:readme fork:true user:adafruit archived:false AND NOT PCB in:name AND NOT CircuitPython in:name",
5052
"per_page": 100,
5153
"sort": "updated",
5254
"order": "asc"})
5355
while result.ok:
54-
links = result.headers["Link"]
5556
repos.extend(result.json()["items"]) # uncomment and comment below, to include all forks
5657

58+
try:
59+
links = result.headers["Link"]
60+
except KeyError:
61+
break
62+
5763
if links:
5864
next_url = None
5965
for link in links.split(","):
@@ -67,18 +73,14 @@ def list_repos():
6773
break
6874
# Subsequent links have our access token already so we use requests directly.
6975
result = requests.get(link, timeout=30)
70-
7176
return repos
7277

7378
def is_arduino_library(repo):
7479
""" Returns if the repo is an Arduino library, as determined by the existence of
7580
the 'library.properties' file.
7681
"""
77-
has_lib_prop = github.get("/repos/adafruit/" + repo["name"] + "/contents")
78-
if has_lib_prop.ok:
79-
return ("library.properties" in has_lib_prop.text)
80-
else:
81-
return False
82+
lib_prop_file = requests.get("https://raw.githubusercontent.com/adafruit/" + repo["name"] + "/master/library.properties")
83+
return lib_prop_file.ok
8284

8385
def print_list_output(title, coll):
8486
""
@@ -105,67 +107,52 @@ def validate_library_properties(repo):
105107
lib_prop_file = None
106108
lib_version = None
107109
release_tag = None
108-
has_lib_prop = github.get("/repos/adafruit/" + repo["name"] + "/contents")
109-
if has_lib_prop.ok:
110-
if "library.properties" not in has_lib_prop.text:
111-
return False
112-
for file in has_lib_prop.json():
113-
if file["name"] == "library.properties":
114-
lib_prop_file = requests.get(file["download_url"], timeout=30)
115-
if lib_prop_file.ok:
116-
lines = lib_prop_file.text.split("\n")
117-
for line in lines:
118-
if "version" in line:
119-
lib_version = line[len("version="):]
120-
break
121-
122-
get_latest_release = github.get("/repos/adafruit/" + repo["name"] + "/releases/latest")
123-
if get_latest_release.ok:
124-
response = get_latest_release.json()
125-
if "tag_name" in response:
126-
release_tag = response["tag_name"]
127-
if "message" in response:
128-
if response["message"] == "Not Found":
129-
release_tag = "None"
130-
else:
131-
release_tag = "Unknown"
132-
133-
if lib_version and release_tag:
110+
lib_prop_file = requests.get("https://raw.githubusercontent.com/adafruit/" + repo["name"] + "/master/library.properties")
111+
if not lib_prop_file.ok:
112+
print("{} skipped".format(repo["name"]))
113+
return None # no library properties file!
114+
115+
lines = lib_prop_file.text.split("\n")
116+
for line in lines:
117+
if "version" in line:
118+
lib_version = line[len("version="):]
119+
break
120+
121+
get_latest_release = github.get("/repos/adafruit/" + repo["name"] + "/releases/latest")
122+
if get_latest_release.ok:
123+
response = get_latest_release.json()
124+
if "tag_name" in response:
125+
release_tag = response["tag_name"]
126+
if "message" in response:
127+
if response["message"] == "Not Found":
128+
release_tag = "None"
129+
else:
130+
release_tag = "Unknown"
131+
132+
if lib_version and release_tag:
134133
return [release_tag, lib_version]
135134

136-
#print("{} skipped".format(repo["name"]))
137-
return
135+
return None
138136

139137
def validate_release_state(repo):
140138
"""Validate if a repo 1) has a release, and 2) if there have been commits
141139
since the last release. Returns a list of string error messages for the
142140
repository.
143141
"""
144-
145142
if not is_arduino_library(repo):
146143
return
147-
repo_last_release = github.get("/repos/" + repo["full_name"] + "/releases/latest")
148-
if not repo_last_release.ok:
149-
return
150-
repo_release_json = repo_last_release.json()
151-
if "tag_name" in repo_release_json:
152-
tag_name = repo_release_json["tag_name"]
153-
elif "message" in repo_release_json:
154-
output_handler("Error: retrieving latest release information failed on '{0}'. Information Received: {1}".format(
155-
repo["name"], repo_release_json["message"]))
156-
return
157144

158-
compare_tags = github.get("/repos/" + repo["full_name"] + "/compare/master..." + tag_name)
145+
compare_tags = github.get("/repos/" + repo["full_name"] + "/compare/master..." + repo['tag_name'])
159146
if not compare_tags.ok:
160-
output_handler("Error: failed to compare {0} 'master' to tag '{1}'".format(repo["name"], tag_name))
147+
output_handler("Error: failed to compare {0} 'master' to tag '{1}'".format(repo["name"], repo['tag_name']))
161148
return
162149
compare_tags_json = compare_tags.json()
163150
if "status" in compare_tags_json:
164151
if compare_tags.json()["status"] != "identical":
165152
#print("Compare {4} status: {0} \n Ahead: {1} \t Behind: {2} \t Commits: {3}".format(
166153
# compare_tags_json["status"], compare_tags_json["ahead_by"],
167154
# compare_tags_json["behind_by"], compare_tags_json["total_commits"], repo["full_name"]))
168-
return [tag_name, compare_tags_json["behind_by"]]
155+
return [repo['tag_name'], compare_tags_json["behind_by"]]
169156
elif "errors" in compare_tags_json:
170157
output_handler("Error: comparing latest release to 'master' failed on '{0}'. Error Message: {1}".format(
171158
repo["name"], compare_tags_json["message"]))
@@ -175,18 +162,14 @@ def validate_release_state(repo):
175162
def validate_travis(repo):
176163
"""Validate if a repo has .travis.yml.
177164
"""
178-
repo_has_travis = github.get("/repos/" + repo["full_name"] + "/contents/.travis.yml")
179-
if repo_has_travis.ok:
180-
return True
165+
repo_has_travis = requests.get("https://raw.githubusercontent.com/adafruit/" + repo["name"] + "/master/.travis.yml")
166+
return repo_has_travis.ok
181167

182168
def validate_example(repo):
183169
"""Validate if a repo has any files in examples directory
184170
"""
185171
repo_has_ino = github.get("/repos/adafruit/" + repo["name"] + "/contents/examples")
186-
if repo_has_ino.ok and len(repo_has_ino.json()):
187-
return True
188-
else:
189-
return False
172+
return repo_has_ino.ok and len(repo_has_ino.json())
190173

191174
def run_arduino_lib_checks():
192175
output_handler("Running Arduino Library Checks")
@@ -200,24 +183,36 @@ def run_arduino_lib_checks():
200183
missing_library_properties_list = [[" Repo"], [" ----"]]
201184

202185
for repo in repo_list:
186+
have_examples = validate_example(repo)
187+
if not have_examples:
188+
# not a library
189+
continue
190+
191+
entry = {'name': repo["name"]}
192+
203193
lib_check = validate_library_properties(repo)
204-
if lib_check:
205-
if lib_check[0] != lib_check[1]:
206-
failed_lib_prop.append([" " + str(repo["name"]), lib_check[0], lib_check[1]])
194+
if not lib_check:
195+
missing_library_properties_list.append([" " + str(repo["name"])])
196+
continue
207197

208-
needs_release = validate_release_state(repo)
209-
missing_travis = not validate_travis(repo)
210-
have_ino = validate_example(repo)
211-
missing_library_properties = lib_check == False
198+
entry['release'] = lib_check[0]
199+
entry['version'] = lib_check[1]
200+
repo['tag_name'] = lib_check[0]
212201

202+
needs_release = validate_release_state(repo)
203+
entry['needs_release'] = needs_release
213204
if needs_release:
214205
needs_release_list.append([" " + str(repo["name"]), needs_release[0], needs_release[1]])
215206

216-
if missing_travis and have_ino:
207+
missing_travis = not validate_travis(repo)
208+
entry['needs_travis'] = missing_travis
209+
if missing_travis:
217210
missing_travis_list.append([" " + str(repo["name"])])
218211

219-
if missing_library_properties and have_ino:
220-
missing_library_properties_list.append([" " + str(repo["name"])])
212+
all_libraries.append(entry)
213+
214+
for entry in all_libraries:
215+
print(entry)
221216

222217
if len(failed_lib_prop) > 2:
223218
print_list_output("Libraries Have Mismatched Release Tag and library.properties Version: ({})", failed_lib_prop)
@@ -226,10 +221,10 @@ def run_arduino_lib_checks():
226221
print_list_output("Libraries have commits since last release: ({})", needs_release_list);
227222

228223
if len(missing_travis_list) > 2:
229-
print_list_output("Libraries that is not configured with Travis (but have *.ino files): ({})", missing_travis_list)
224+
print_list_output("Libraries that is not configured with Travis: ({})", missing_travis_list)
230225

231226
if len(missing_library_properties_list) > 2:
232-
print_list_output("Libraries that is missing library.properties file (but have *.ino files): ({})", missing_library_properties_list)
227+
print_list_output("Libraries that is missing library.properties file: ({})", missing_library_properties_list)
233228

234229

235230
if __name__ == "__main__":

0 commit comments

Comments
 (0)