Skip to content

Commit 3e5009e

Browse files
committed
Improve function signatures, formatting and docstrings #16
* use global URL as a default for a json_data() url arg * use base_release as an arg for extract_data() with default to jessie * do not use fragile chained get in extract_data() and check for possible empty values ahead of use * use more descriptive variable names * improve docstrings * organize imports and improve code formatting Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent acac302 commit 3e5009e

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

scraper/debian.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,47 @@
2121
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
2222
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
2323

24-
from urllib.request import urlopen
2524
import json
25+
from urllib.request import urlopen
2626

27-
DEBIAN_ROOT_URL = "https://security-tracker.debian.org/tracker/data/json"
2827

28+
DEBIAN_TRACKER_URL = 'https://security-tracker.debian.org/tracker/data/json'
2929

30-
def json_data():
30+
31+
def json_data(url=DEBIAN_TRACKER_URL):
3132
"""
32-
Return JSON of Debian vulnerability data.
33+
Return Debian vulnerabilities data fetched from `url`.
3334
"""
34-
debian_data = urlopen(DEBIAN_ROOT_URL).read()
35-
json_data = json.loads(debian_data)
36-
37-
return json_data
35+
debian_data = urlopen(url).read()
36+
return json.loads(debian_data)
3837

3938

40-
def extract_data(data):
39+
def extract_data(debian_data, base_release='jessie'):
4140
"""
42-
Return a dictionary of package names and vulnerability details.
43-
Accepts `JSON` as input.
41+
Return a sequence of mappings for each existing combination of
42+
package and vulnerability from a mapping of Debian vulnerabilities
43+
data.
4444
"""
45-
final_data = []
45+
package_vulns = []
4646

47-
for package_name, vulnerabilities in data.items():
48-
for vulnerability, details in vulnerabilities.items():
49-
release_data = details.get('releases').get('jessie')
47+
for package_name, vulnerabilities in debian_data.items():
48+
if not vulnerabilities or not package_name:
49+
continue
5050

51-
if release_data is None:
51+
for vulnerability, details in vulnerabilities.items():
52+
releases = details.get('releases')
53+
if not releases:
5254
continue
5355

54-
final_data.append({
55-
"package_name": package_name,
56-
"vulnerability_id": vulnerability,
57-
"status": release_data.get("status"),
58-
"urgency": release_data.get("urgency"),
59-
"fixed_version": release_data.get("fixed_version")
60-
}
61-
)
56+
release = releases.get(base_release)
57+
if not release:
58+
continue
6259

63-
return final_data
60+
package_vulns.append({
61+
'package_name': package_name,
62+
'vulnerability_id': vulnerability,
63+
'status': release.get('status'),
64+
'urgency': release.get('urgency'),
65+
'fixed_version': release.get('fixed_version')
66+
})
67+
return package_vulns

0 commit comments

Comments
 (0)