-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependencies_csv.py
More file actions
106 lines (100 loc) · 3.91 KB
/
dependencies_csv.py
File metadata and controls
106 lines (100 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from urllib2 import Request, urlopen
import json
import csv
def json_load_byteified(file_handle):
return _byteify(
json.load(file_handle, object_hook=_byteify),
ignore_dicts=True
)
def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)
#function to get string without unicode u prefix
def _byteify(data, ignore_dicts = False):
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form
return data
values = """
{
"filters": {
"languages": [
"javascript",
"ruby",
"java",
"dotnet"
],
"projects": [],
"dependencies": [],
"licenses": [],
"severity": [
"high",
"medium",
"low"
],
"depStatus": ""
}
}
"""
values = """
{
"filters": {
"projects" : ["4827003f-5fdc-4f6e-aadf-03766ae9237b"]
}
}
"""
#be awere of pagination -> call in loop until no more
headers = {
'Content-Type': 'application/json',
'Authorization': 'YOUR-TOKEN'
}
request = Request('https://snyk.io/api/v1/org/4c503fed-f788-41f5-bdcb-55bb41188364/dependencies?page=1&perPage=2000', data=values, headers=headers)
response_body = urlopen(request).read()
#print response_body#
#dep = json.loads(response_body)
dep = json_loads_byteified(response_body)
ii = 0
with open('./dependencies.csv', mode='w') as dependecyFile:
dependecyFile = csv.writer(dependecyFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
dependecyFile.writerow( [ "Project ID" ,"Project Name" ,"License Id" , "License Title" , "License" , "Package Id", " Package Name", "Package Version" ,"Package Type","Copyrights"])
for deps in dep['results']:
#print (dep["results"][ii]['id']), "\t"
#print (dep['results'][ii]['id']) ,'\t', (dep['results'][ii]['name']) ,'\t', (dep['results'][ii]['version']) ,'\t', (dep['results'][ii]['type']),'\t'
depId = (dep['results'][ii]['id'])
depName = (dep['results'][ii]['name'])
depVer = (dep['results'][ii]['version'])
depType = (dep['results'][ii]['type'])
iii=0
for lics in dep['results'][ii]['licenses']:
#print (dep['results'][ii]['licenses'][iii]['id']) , '\t' ,dep['results'][ii]['licenses'][iii]['title'] ,'\t' , dep['results'][ii]['licenses'][iii]['license']
licId = (dep['results'][ii]['licenses'][iii]['id'])
licTitle = dep['results'][ii]['licenses'][iii]['title']
licLicense = dep['results'][ii]['licenses'][iii]['license']
iii=iii+1
copy=""
if 'copyright' in dep['results'][ii]:
#print (dep['results'][ii]['copyright'])
copy = (dep['results'][ii]['copyright'])
#reset iii for loop on projs
iii=0
for projs in dep['results'][ii]['projects']:
#print (dep['results'][ii]['projects'][iii]['id']) , '\t' ,dep['results'][ii]['projects'][iii]['name'] ,'\t'
projID = (dep['results'][ii]['projects'][iii]['id'])
projName = dep['results'][ii]['projects'][iii]['name']
iii=iii+1
#print projID , "t" , projName, "\t" , licId , "\t", licTitle , "\t", licLicense , "\t" , depName, "\t", depVer , "\t", copy
dependecyFile.writerow( [ projID ,projName,licId , licTitle , licLicense , depId, depName, depVer ,depType,copy])
ii= ii+1