-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·83 lines (66 loc) · 2.05 KB
/
main.py
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
#!/usr/bin/python3
import toml
from pathlib import Path
def read(path):
with open(path, 'r') as f:
return f.read()
def write(path, text):
with open(path, 'w') as f:
return f.write(text)
def calculate_frequency(data, group, packages):
frequency = {}
for e in data:
for name in e.get('cargo_toml', {}).get(group, {}):
if name in packages:
continue
frequency[name] = frequency.get(name, 0) + 1
return frequency
def main():
source_dir = './../ic'
output_file = './count.csv'
# Cargo.toml files.
data = [
{
'cargo_path': path,
'cargo_toml': toml.loads(read(path)),
}
for path in Path(source_dir).rglob('Cargo.toml')
]
data = [x for x in data if len(x.get('cargo_toml', '')) > 0]
# IC packages.
packages = [
x.get('cargo_toml', {}).get('package', {}).get('name')
for x in data
]
packages = set([x for x in packages if x is not None])
# Count frequencies.
result = {}
for group in ['dependencies', 'dev-dependencies', 'build-dependencies']:
result[group] = calculate_frequency(data, group, packages)
# Generate CSV data.
csv = []
for group, frequency in result.items():
for name, count in frequency.items():
csv.append({
'group': group,
'name': name,
'count': count,
})
# Sort by name, ascending.
csv = sorted(csv, key=lambda x: x['name'], reverse=False)
# Sort by count, descending.
csv = sorted(csv, key=lambda x: x['count'], reverse=True)
# Sort by group, ascending.
csv = sorted(csv, key=lambda x: x['group'], reverse=False)
# Generate CSV table.
table = ['group,name,count']
for entry in csv:
group = entry['group']
name = entry['name']
count = entry['count']
table.append(f'{group},{name},{count}')
# Write CSV to file.
text = '\n'.join(table)
write(output_file, text)
if __name__ == '__main__':
main()